Home | History | Annotate | Line # | Download | only in jazz
fd.c revision 1.52
      1 /*	$NetBSD: fd.c,v 1.52 2023/08/29 21:55:11 andvar Exp $	*/
      2 /*	$OpenBSD: fd.c,v 1.6 1998/10/03 21:18:57 millert Exp $	*/
      3 /*	NetBSD: fd.c,v 1.78 1995/07/04 07:23:09 mycroft Exp 	*/
      4 
      5 /*-
      6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Charles M. Hannum.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*-
     35  * Copyright (c) 1990 The Regents of the University of California.
     36  * All rights reserved.
     37  *
     38  * This code is derived from software contributed to Berkeley by
     39  * Don Ahn.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.52 2023/08/29 21:55:11 andvar Exp $");
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/callout.h>
     74 #include <sys/kernel.h>
     75 #include <sys/conf.h>
     76 #include <sys/file.h>
     77 #include <sys/ioctl.h>
     78 #include <sys/device.h>
     79 #include <sys/disklabel.h>
     80 #include <sys/disk.h>
     81 #include <sys/buf.h>
     82 #include <sys/bufq.h>
     83 #include <sys/uio.h>
     84 #include <sys/syslog.h>
     85 #include <sys/queue.h>
     86 
     87 #include <uvm/uvm_extern.h>
     88 
     89 #include <dev/cons.h>
     90 
     91 #include <sys/bus.h>
     92 #include <machine/cpu.h>
     93 
     94 #include <arc/jazz/fdreg.h>
     95 #include <arc/jazz/fdcvar.h>
     96 
     97 #include "ioconf.h"
     98 #include "locators.h"
     99 
    100 #define FDUNIT(dev)	DISKUNIT(dev)
    101 #define FDTYPE(dev)	DISKPART(dev)
    102 
    103 /* controller driver configuration */
    104 static int fdprint(void *, const char *);
    105 
    106 /*
    107  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    108  * we tell them apart.
    109  */
    110 struct fd_type {
    111 	int	sectrac;	/* sectors per track */
    112 	int	heads;		/* number of heads */
    113 	int	seccyl;		/* sectors per cylinder */
    114 	int	secsize;	/* size code for sectors */
    115 	int	datalen;	/* data len when secsize = 0 */
    116 	int	steprate;	/* step rate and head unload time */
    117 	int	gap1;		/* gap len between sectors */
    118 	int	gap2;		/* formatting gap */
    119 	int	cyls;		/* total num of cylinders */
    120 	int	size;		/* size of disk in sectors */
    121 	int	step;		/* steps per cylinder */
    122 	int	rate;		/* transfer speed code */
    123 	const char *name;
    124 };
    125 
    126 /* The order of entries in the following table is important -- BEWARE! */
    127 const static struct fd_type fd_types[] = {
    128 	/* 1.44MB diskette */
    129 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    },
    130 	/* 1.2 MB AT-diskettes */
    131 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB"    },
    132 	/* 360kB in 1.2MB drive */
    133 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" },
    134 	/* 360kB PC diskettes */
    135 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" },
    136 	/* 3.5" 720kB diskette */
    137 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB"    },
    138 	/* 720kB in 1.2MB drive */
    139 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x"  },
    140 	/* 360kB in 720kB drive */
    141 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x"  },
    142 };
    143 
    144 /* software state, per disk (with up to 4 disks per ctlr) */
    145 struct fd_softc {
    146 	device_t sc_dev;
    147 	struct disk sc_dk;
    148 
    149 	const struct fd_type *sc_deftype; /* default type descriptor */
    150 	struct fd_type *sc_type;	/* current type descriptor */
    151 	struct fd_type sc_type_copy;	/* copy for fiddling when formatting */
    152 
    153 	struct callout sc_motoron_ch;
    154 	struct callout sc_motoroff_ch;
    155 
    156 	daddr_t	sc_blkno;	/* starting block number */
    157 	int sc_bcount;		/* byte count left */
    158 	int sc_opts;		/* user-set options */
    159 	int sc_skip;		/* bytes already transferred */
    160 	int sc_nblks;		/* number of blocks currently transferring */
    161 	int sc_nbytes;		/* number of bytes currently transferring */
    162 
    163 	int sc_drive;		/* physical unit number */
    164 	int sc_flags;
    165 #define	FD_OPEN		0x01		/* it's open */
    166 #define	FD_MOTOR	0x02		/* motor should be on */
    167 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    168 	int sc_cylin;		/* where we think the head is */
    169 
    170 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    171 	int sc_ops;		/* I/O ops since last switch */
    172 	struct bufq_state *sc_q;/* pending I/O requests */
    173 	int sc_active;		/* number of active I/O operations */
    174 };
    175 
    176 /* floppy driver configuration */
    177 static int fdprobe(device_t, cfdata_t, void *);
    178 static void fdattach(device_t, device_t, void *);
    179 
    180 CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc), fdprobe, fdattach, NULL, NULL);
    181 
    182 dev_type_open(fdopen);
    183 dev_type_close(fdclose);
    184 dev_type_read(fdread);
    185 dev_type_write(fdwrite);
    186 dev_type_ioctl(fdioctl);
    187 dev_type_strategy(fdstrategy);
    188 
    189 const struct bdevsw fd_bdevsw = {
    190 	.d_open = fdopen,
    191 	.d_close = fdclose,
    192 	.d_strategy = fdstrategy,
    193 	.d_ioctl = fdioctl,
    194 	.d_dump = nodump,
    195 	.d_psize = nosize,
    196 	.d_discard = nodiscard,
    197 	.d_flag = D_DISK
    198 };
    199 
    200 const struct cdevsw fd_cdevsw = {
    201 	.d_open = fdopen,
    202 	.d_close = fdclose,
    203 	.d_read = fdread,
    204 	.d_write = fdwrite,
    205 	.d_ioctl = fdioctl,
    206 	.d_stop = nostop,
    207 	.d_tty = notty,
    208 	.d_poll = nopoll,
    209 	.d_mmap = nommap,
    210 	.d_kqfilter = nokqfilter,
    211 	.d_discard = nodiscard,
    212 	.d_flag = D_DISK
    213 };
    214 
    215 static void fdstart(struct fd_softc *);
    216 
    217 struct dkdriver fddkdriver = {
    218 	.d_strategy = fdstrategy
    219 };
    220 
    221 static bool fd_shutdown(device_t, int);
    222 #if 0
    223 static const struct fd_type *fd_nvtotype(char *, int, int);
    224 #endif
    225 static void fd_set_motor(struct fdc_softc *, int);
    226 static void fd_motor_off(void *);
    227 static void fd_motor_on(void *);
    228 static int fdcresult(struct fdc_softc *);
    229 static void fdcstart(struct fdc_softc *);
    230 static void fdcstatus(device_t, int, const char *);
    231 static void fdctimeout(void *);
    232 static void fdcpseudointr(void *);
    233 static void fdcretry(struct fdc_softc *);
    234 static void fdfinish(struct fd_softc *, struct buf *);
    235 static const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
    236 static void fd_mountroot_hook(device_t);
    237 
    238 /*
    239  * Arguments passed between fdcattach and fdprobe.
    240  */
    241 struct fdc_attach_args {
    242 	int fa_drive;
    243 	const struct fd_type *fa_deftype;
    244 };
    245 
    246 /*
    247  * Print the location of a disk drive (called just before attaching the
    248  * the drive).  If `fdc' is not NULL, the drive was found but was not
    249  * in the system config file; print the drive name as well.
    250  * Return QUIET (config_find ignores this if the device was configured) to
    251  * avoid printing `fdN not configured' messages.
    252  */
    253 static int
    254 fdprint(void *aux, const char *fdc)
    255 {
    256 	struct fdc_attach_args *fa = aux;
    257 
    258 	if (fdc == NULL)
    259 		aprint_normal(" drive %d", fa->fa_drive);
    260 	return QUIET;
    261 }
    262 
    263 void
    264 fdcattach(struct fdc_softc *fdc)
    265 {
    266 	struct fdc_attach_args fa;
    267 	int type;
    268 
    269 	callout_init(&fdc->sc_timo_ch, 0);
    270 	callout_init(&fdc->sc_intr_ch, 0);
    271 
    272 	fdc->sc_state = DEVIDLE;
    273 	TAILQ_INIT(&fdc->sc_drives);
    274 
    275 	/*
    276 	 * No way yet to determine default disk types.
    277 	 * we assume 1.44 3.5" type for the moment.
    278 	 */
    279 	type = 0;
    280 
    281 	/* physical limit: two drives per controller. */
    282 	for (fa.fa_drive = 0; fa.fa_drive < 2; fa.fa_drive++) {
    283 		fa.fa_deftype = &fd_types[type];
    284 		(void)config_found(fdc->sc_dev, (void *)&fa, fdprint,
    285 		    CFARGS_NONE);
    286 	}
    287 }
    288 
    289 static int
    290 fdprobe(device_t parent, cfdata_t cf , void *aux)
    291 {
    292 	struct fdc_softc *fdc = device_private(parent);
    293 	struct fdc_attach_args *fa = aux;
    294 	int drive = fa->fa_drive;
    295 	bus_space_tag_t iot = fdc->sc_iot;
    296 	bus_space_handle_t ioh = fdc->sc_ioh;
    297 	int n;
    298 
    299 	if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
    300 	    cf->cf_loc[FDCCF_DRIVE] != drive)
    301 		return 0;
    302 
    303 	/* select drive and turn on motor */
    304 	bus_space_write_1(iot, ioh, FDOUT, drive | FDO_FRST | FDO_MOEN(drive));
    305 	/* wait for motor to spin up */
    306 	delay(250000);
    307 	out_fdc(iot, ioh, NE7CMD_RECAL);
    308 	out_fdc(iot, ioh, drive);
    309 	/* wait for recalibrate */
    310 	delay(2000000);
    311 	out_fdc(iot, ioh, NE7CMD_SENSEI);
    312 	n = fdcresult(fdc);
    313 #ifdef FD_DEBUG
    314 	{
    315 		int i;
    316 		aprint_debug("%s: status", __func__);
    317 		for (i = 0; i < n; i++)
    318 			aprint_debug(" %x", fdc->sc_status[i]);
    319 		aprint_debug("\n");
    320 	}
    321 #endif
    322 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    323 		return 0;
    324 	/* turn off motor */
    325 	bus_space_write_1(iot, ioh, FDOUT, FDO_FRST);
    326 
    327 	return 1;
    328 }
    329 
    330 /*
    331  * Controller is working, and drive responded.  Attach it.
    332  */
    333 void
    334 fdattach(device_t parent, device_t self, void *aux)
    335 {
    336 	struct fdc_softc *fdc = device_private(parent);
    337 	struct fd_softc *fd = device_private(self);
    338 	struct fdc_attach_args *fa = aux;
    339 	const struct fd_type *type = fa->fa_deftype;
    340 	int drive = fa->fa_drive;
    341 
    342 	fd->sc_dev = self;
    343 
    344 	callout_init(&fd->sc_motoron_ch, 0);
    345 	callout_init(&fd->sc_motoroff_ch, 0);
    346 
    347 	/* XXX Allow `flags' to override device type? */
    348 
    349 	if (type)
    350 		printf(": %s, %d cyl, %d head, %d sec\n", type->name,
    351 		    type->cyls, type->heads, type->sectrac);
    352 	else
    353 		printf(": density unknown\n");
    354 
    355 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    356 	fd->sc_cylin = -1;
    357 	fd->sc_drive = drive;
    358 	fd->sc_deftype = type;
    359 	fdc->sc_fd[drive] = fd;
    360 
    361 	/*
    362 	 * Initialize and attach the disk structure.
    363 	 */
    364 	disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
    365 	disk_attach(&fd->sc_dk);
    366 
    367 	/* Establish a mountroot hook. */
    368 	mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
    369 
    370 	/* Needed to power off if the motor is on when we halt. */
    371 	if (!pmf_device_register1(self, NULL, NULL, fd_shutdown))
    372 		aprint_error_dev(self, "couldn't establish power handler\n");
    373 }
    374 
    375 bool
    376 fd_shutdown(device_t self, int howto)
    377 {
    378 	struct fd_softc *fd;
    379 
    380 	fd = device_private(self);
    381 	fd_motor_off(fd);
    382 
    383 	return true;
    384 }
    385 
    386 #if 0
    387 /*
    388  * Translate nvram type into internal data structure.  Return NULL for
    389  * none/unknown/unusable.
    390  */
    391 static const struct fd_type *
    392 fd_nvtotype(char *fdc, int nvraminfo, int drive)
    393 {
    394 	int type;
    395 
    396 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
    397 #if 0
    398 	switch (type) {
    399 	case NVRAM_DISKETTE_NONE:
    400 		return NULL;
    401 	case NVRAM_DISKETTE_12M:
    402 		return &fd_types[1];
    403 	case NVRAM_DISKETTE_TYPE5:
    404 	case NVRAM_DISKETTE_TYPE6:
    405 		/* XXX We really ought to handle 2.88MB format. */
    406 	case NVRAM_DISKETTE_144M:
    407 		return &fd_types[0];
    408 	case NVRAM_DISKETTE_360K:
    409 		return &fd_types[3];
    410 	case NVRAM_DISKETTE_720K:
    411 		return &fd_types[4];
    412 	default:
    413 		printf("%s: drive %d: unknown device type 0x%x\n",
    414 		    fdc, drive, type);
    415 		return NULL;
    416 	}
    417 #else
    418 	return &fd_types[0]; /* Use only 1.44 for now */
    419 #endif
    420 }
    421 #endif
    422 
    423 static const struct fd_type *
    424 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
    425 {
    426 	int type = FDTYPE(dev);
    427 
    428 	if (type > __arraycount(fd_types))
    429 		return NULL;
    430 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    431 }
    432 
    433 void
    434 fdstrategy(struct buf *bp)
    435 {
    436 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
    437 	int sz;
    438 	int s;
    439 
    440 	/* Valid unit, controller, and request? */
    441 	if (bp->b_blkno < 0 ||
    442 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    443 		bp->b_error = EINVAL;
    444 		goto done;
    445 	}
    446 
    447 	/* If it's a null transfer, return immediately. */
    448 	if (bp->b_bcount == 0)
    449 		goto done;
    450 
    451 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    452 
    453 	if (bp->b_blkno + sz > fd->sc_type->size) {
    454 		sz = fd->sc_type->size - bp->b_blkno;
    455 		if (sz == 0) {
    456 			/* If exactly at end of disk, return EOF. */
    457 			goto done;
    458 		}
    459 		if (sz < 0) {
    460 			/* If past end of disk, return EINVAL. */
    461 			bp->b_error = EINVAL;
    462 			goto done;
    463 		}
    464 		/* Otherwise, truncate request. */
    465 		bp->b_bcount = sz << DEV_BSHIFT;
    466 	}
    467 
    468 	bp->b_rawblkno = bp->b_blkno;
    469 	bp->b_cylinder =
    470 	    bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    471 
    472 #ifdef FD_DEBUG
    473 	printf("%s: b_blkno %" PRId64 " b_bcount %d blkno %" PRId64
    474 	    " cylin %d sz %d\n", __func__,
    475 	    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
    476 #endif
    477 
    478 	/* Queue transfer on drive, activate drive and controller if idle. */
    479 	s = splbio();
    480 	bufq_put(fd->sc_q, bp);
    481 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
    482 	if (fd->sc_active == 0)
    483 		fdstart(fd);
    484 #ifdef DIAGNOSTIC
    485 	else {
    486 		struct fdc_softc *fdc =
    487 		    device_private(device_parent(fd->sc_dev));
    488 		if (fdc->sc_state == DEVIDLE) {
    489 			printf("%s: controller inactive\n", __func__);
    490 			fdcstart(fdc);
    491 		}
    492 	}
    493 #endif
    494 	splx(s);
    495 	return;
    496 
    497  done:
    498 	/* Toss transfer; we're done early. */
    499 	bp->b_resid = bp->b_bcount;
    500 	biodone(bp);
    501 }
    502 
    503 void
    504 fdstart(struct fd_softc *fd)
    505 {
    506 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    507 	int active = TAILQ_FIRST(&fdc->sc_drives) != 0;
    508 
    509 	/* Link into controller queue. */
    510 	fd->sc_active = 1;
    511 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    512 
    513 	/* If controller not already active, start it. */
    514 	if (!active)
    515 		fdcstart(fdc);
    516 }
    517 
    518 void
    519 fdfinish(struct fd_softc *fd, struct buf *bp)
    520 {
    521 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    522 
    523 	/*
    524 	 * Move this drive to the end of the queue to give others a `fair'
    525 	 * chance.  We only force a switch if N operations are completed while
    526 	 * another drive is waiting to be serviced, since there is a long motor
    527 	 * startup delay whenever we switch.
    528 	 */
    529 	(void)bufq_get(fd->sc_q);
    530 	if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
    531 		fd->sc_ops = 0;
    532 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    533 		if (bufq_peek(fd->sc_q) != NULL)
    534 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    535 		else
    536 			fd->sc_active = 0;
    537 	}
    538 	bp->b_resid = fd->sc_bcount;
    539 	fd->sc_skip = 0;
    540 	biodone(bp);
    541 	/* turn off motor 5s from now */
    542 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
    543 	fdc->sc_state = DEVIDLE;
    544 }
    545 
    546 int
    547 fdread(dev_t dev, struct uio *uio, int flags)
    548 {
    549 
    550 	return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
    551 }
    552 
    553 int
    554 fdwrite(dev_t dev, struct uio *uio, int flags)
    555 {
    556 
    557 	return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
    558 }
    559 
    560 void
    561 fd_set_motor(struct fdc_softc *fdc, int reset)
    562 {
    563 	struct fd_softc *fd;
    564 	u_char status;
    565 	int n;
    566 
    567 	if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
    568 		status = fd->sc_drive;
    569 	else
    570 		status = 0;
    571 	if (!reset)
    572 		status |= FDO_FRST | FDO_FDMAEN;
    573 	for (n = 0; n < 4; n++)
    574 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    575 			status |= FDO_MOEN(n);
    576 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, FDOUT, status);
    577 }
    578 
    579 void
    580 fd_motor_off(void *arg)
    581 {
    582 	struct fd_softc *fd = arg;
    583 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    584 	int s;
    585 
    586 	s = splbio();
    587 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    588 	fd_set_motor(fdc, 0);
    589 	splx(s);
    590 }
    591 
    592 void
    593 fd_motor_on(void *arg)
    594 {
    595 	struct fd_softc *fd = arg;
    596 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    597 	int s;
    598 
    599 	s = splbio();
    600 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    601 	if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&
    602 	    (fdc->sc_state == MOTORWAIT))
    603 		(void)fdcintr(fdc);
    604 	splx(s);
    605 }
    606 
    607 int
    608 fdcresult(struct fdc_softc *fdc)
    609 {
    610 	bus_space_tag_t iot = fdc->sc_iot;
    611 	bus_space_handle_t ioh = fdc->sc_ioh;
    612 	u_char i;
    613 	int j, n = 0;
    614 
    615 	for (j = 100000; j; j--) {
    616 		i = bus_space_read_1(iot, ioh, FDSTS) &
    617 		    (NE7_DIO | NE7_RQM | NE7_CB);
    618 		if (i == NE7_RQM)
    619 			return n;
    620 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    621 			if (n >= sizeof(fdc->sc_status)) {
    622 				log(LOG_ERR, "%s: overrun\n", __func__);
    623 				return -1;
    624 			}
    625 			fdc->sc_status[n++] =
    626 			    bus_space_read_1(iot, ioh, FDDATA);
    627 		}
    628 		delay(10);
    629 	}
    630 	log(LOG_ERR, "%s: timeout\n", __func__);
    631 	return -1;
    632 }
    633 
    634 int
    635 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t x)
    636 {
    637 	int i = 100000;
    638 
    639 	while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_DIO) && i-- > 0);
    640 	if (i <= 0)
    641 		return -1;
    642 	while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_RQM) == 0 && i-- > 0);
    643 	if (i <= 0)
    644 		return -1;
    645 	bus_space_write_1(iot, ioh, FDDATA, x);
    646 	return 0;
    647 }
    648 
    649 int
    650 fdopen(dev_t dev, int flags, int mode, struct lwp *l)
    651 {
    652 	struct fd_softc *fd;
    653 	const struct fd_type *type;
    654 
    655 	fd = device_lookup_private(&fd_cd, FDUNIT(dev));
    656 	if (fd == NULL)
    657 		return ENXIO;
    658 
    659 	type = fd_dev_to_type(fd, dev);
    660 	if (type == NULL)
    661 		return ENXIO;
    662 
    663 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    664 	    memcmp(fd->sc_type, type, sizeof(*type)))
    665 		return EBUSY;
    666 
    667 	fd->sc_type_copy = *type;
    668 	fd->sc_type = &fd->sc_type_copy;
    669 	fd->sc_cylin = -1;
    670 	fd->sc_flags |= FD_OPEN;
    671 
    672 	return 0;
    673 }
    674 
    675 int
    676 fdclose(dev_t dev, int flags, int mode, struct lwp *l)
    677 {
    678 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
    679 
    680 	fd->sc_flags &= ~FD_OPEN;
    681 	return 0;
    682 }
    683 
    684 void
    685 fdcstart(struct fdc_softc *fdc)
    686 {
    687 
    688 #ifdef DIAGNOSTIC
    689 	/* only got here if controller's drive queue was inactive; should
    690 	   be in idle state */
    691 	if (fdc->sc_state != DEVIDLE) {
    692 		printf("%s: not idle\n", __func__);
    693 		return;
    694 	}
    695 #endif
    696 	(void)fdcintr(fdc);
    697 }
    698 
    699 static void
    700 fdcpstatus(int n, struct fdc_softc *fdc)
    701 {
    702 	char bits[64];
    703 
    704 	switch (n) {
    705 	case 0:
    706 		printf("\n");
    707 		break;
    708 	case 2:
    709 		snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
    710 		printf(" (st0 %s cyl %d)\n", bits, fdc->sc_status[1]);
    711 		break;
    712 	case 7:
    713 		snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
    714 		printf(" (st0 %s", bits);
    715 		snprintb(bits, sizeof(bits), NE7_ST1BITS, fdc->sc_status[1]);
    716 		printf(" st1 %s", bits);
    717 		snprintb(bits, sizeof(bits), NE7_ST2BITS, fdc->sc_status[2]);
    718 		printf(" st2 %s", bits);
    719 		printf(" cyl %d head %d sec %d)\n",
    720 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    721 		break;
    722 #ifdef DIAGNOSTIC
    723 	default:
    724 		printf("\nfdcstatus: weird size");
    725 		break;
    726 #endif
    727 	}
    728 }
    729 
    730 void
    731 fdcstatus(device_t dev, int n, const char *s)
    732 {
    733 	struct fdc_softc *fdc = device_private(device_parent(dev));
    734 
    735 	if (n == 0) {
    736 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
    737 		(void)fdcresult(fdc);
    738 		n = 2;
    739 	}
    740 
    741 	printf("%s: %s", device_xname(dev), s);
    742 	fdcpstatus(n, fdc);
    743 }
    744 
    745 void
    746 fdctimeout(void *arg)
    747 {
    748 	struct fdc_softc *fdc = arg;
    749 	struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
    750 	int s;
    751 
    752 	s = splbio();
    753 #ifdef DEBUG
    754 	log(LOG_ERR, "%s: state %d\n", __func__, fdc->sc_state);
    755 #endif
    756 	fdcstatus(fd->sc_dev, 0, "timeout");
    757 
    758 	if (bufq_peek(fd->sc_q) != NULL)
    759 		fdc->sc_state++;
    760 	else
    761 		fdc->sc_state = DEVIDLE;
    762 
    763 	(void)fdcintr(fdc);
    764 	splx(s);
    765 }
    766 
    767 void
    768 fdcpseudointr(void *arg)
    769 {
    770 	int s;
    771 
    772 	/* Just ensure it has the right spl. */
    773 	s = splbio();
    774 	(void)fdcintr(arg);
    775 	splx(s);
    776 }
    777 
    778 int
    779 fdcintr(void *arg)
    780 {
    781 	struct fdc_softc *fdc = arg;
    782 #define	st0	fdc->sc_status[0]
    783 #define	cyl	fdc->sc_status[1]
    784 	struct fd_softc *fd;
    785 	struct buf *bp;
    786 	bus_space_tag_t iot = fdc->sc_iot;
    787 	bus_space_handle_t ioh = fdc->sc_ioh;
    788 	int read, head, sec, i, nblks;
    789 	struct fd_type *type;
    790 
    791  loop:
    792 	/* Is there a drive for the controller to do a transfer with? */
    793 	fd = TAILQ_FIRST(&fdc->sc_drives);
    794 	if (fd == NULL) {
    795 		fdc->sc_state = DEVIDLE;
    796 		return 1;
    797 	}
    798 
    799 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    800 	bp = bufq_peek(fd->sc_q);
    801 	if (bp == NULL) {
    802 		fd->sc_ops = 0;
    803 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    804 		fd->sc_active = 0;
    805 		goto loop;
    806 	}
    807 
    808 	switch (fdc->sc_state) {
    809 	case DEVIDLE:
    810 		fdc->sc_errors = 0;
    811 		fd->sc_skip = 0;
    812 		fd->sc_bcount = bp->b_bcount;
    813 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
    814 		callout_stop(&fd->sc_motoroff_ch);
    815 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
    816 			fdc->sc_state = MOTORWAIT;
    817 			return 1;
    818 		}
    819 		if ((fd->sc_flags & FD_MOTOR) == 0) {
    820 			/* Turn on the motor, being careful about pairing. */
    821 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
    822 			if (ofd && ofd->sc_flags & FD_MOTOR) {
    823 				callout_stop(&ofd->sc_motoroff_ch);
    824 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    825 			}
    826 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
    827 			fd_set_motor(fdc, 0);
    828 			fdc->sc_state = MOTORWAIT;
    829 			/* Allow .25s for motor to stabilize. */
    830 			callout_reset(&fd->sc_motoron_ch, hz / 4,
    831 			    fd_motor_on, fd);
    832 			return 1;
    833 		}
    834 		/* Make sure the right drive is selected. */
    835 		fd_set_motor(fdc, 0);
    836 
    837 		/* fall through */
    838 	case DOSEEK:
    839  doseek:
    840 		if (fd->sc_cylin == bp->b_cylinder)
    841 			goto doio;
    842 
    843 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
    844 		out_fdc(iot, ioh, fd->sc_type->steprate);
    845 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
    846 
    847 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
    848 		out_fdc(iot, ioh, fd->sc_drive); /* drive number */
    849 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
    850 
    851 		fd->sc_cylin = -1;
    852 		fdc->sc_state = SEEKWAIT;
    853 
    854 		iostat_seek(fd->sc_dk.dk_stats);
    855 		disk_busy(&fd->sc_dk);
    856 
    857 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
    858 		return 1;
    859 
    860 	case DOIO:
    861  doio:
    862 		type = fd->sc_type;
    863 		sec = fd->sc_blkno % type->seccyl;
    864 		nblks = type->seccyl - sec;
    865 		nblks = uimin(nblks, fd->sc_bcount / FDC_BSIZE);
    866 		nblks = uimin(nblks, fdc->sc_maxiosize / FDC_BSIZE);
    867 		fd->sc_nblks = nblks;
    868 		fd->sc_nbytes = nblks * FDC_BSIZE;
    869 		head = sec / type->sectrac;
    870 		sec -= head * type->sectrac;
    871 #ifdef DIAGNOSTIC
    872 		{
    873 			int block;
    874 			block = (fd->sc_cylin * type->heads + head) *
    875 			    type->sectrac + sec;
    876 			if (block != fd->sc_blkno) {
    877 				printf("%s: block %d != blkno %" PRId64
    878 				    "\n", __func__, block, fd->sc_blkno);
    879 #ifdef DDB
    880 				 Debugger();
    881 #endif
    882 			}
    883 		}
    884 #endif
    885 		read = (bp->b_flags & B_READ) != 0;
    886 		FDCDMA_START(fdc, (uint8_t *)bp->b_data + fd->sc_skip,
    887 		    fd->sc_nbytes, read);
    888 		bus_space_write_1(iot, ioh, FDCTL, type->rate);
    889 #ifdef FD_DEBUG
    890 		printf("%s: %s drive %d track %d head %d sec %d nblks %d\n",
    891 		    __func__, read ? "read" : "write", fd->sc_drive,
    892 		    fd->sc_cylin, head, sec, nblks);
    893 #endif
    894 		if (read)
    895 			out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
    896 		else
    897 			out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
    898 		out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
    899 		out_fdc(iot, ioh, fd->sc_cylin);	/* track */
    900 		out_fdc(iot, ioh, head);
    901 		out_fdc(iot, ioh, sec + 1);		/* sector + 1 */
    902 		out_fdc(iot, ioh, type->secsize);	/* sector size */
    903 		out_fdc(iot, ioh, type->sectrac);	/* sectors/track */
    904 		out_fdc(iot, ioh, type->gap1);		/* gap1 size */
    905 		out_fdc(iot, ioh, type->datalen);	/* data length */
    906 		fdc->sc_state = IOCOMPLETE;
    907 
    908 		disk_busy(&fd->sc_dk);
    909 
    910 		/* allow 2 seconds for operation */
    911 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
    912 		return 1;				/* will return later */
    913 
    914 	case SEEKWAIT:
    915 		callout_stop(&fdc->sc_timo_ch);
    916 		fdc->sc_state = SEEKCOMPLETE;
    917 		/* allow 1/50 second for heads to settle */
    918 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
    919 		return 1;
    920 
    921 	case SEEKCOMPLETE:
    922 		disk_unbusy(&fd->sc_dk, 0, 0);
    923 
    924 		/* Make sure seek really happened. */
    925 		out_fdc(iot, ioh, NE7CMD_SENSEI);
    926 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
    927 		    cyl != bp->b_cylinder * fd->sc_type->step) {
    928 #ifdef FD_DEBUG
    929 			fdcstatus(fd->sc_dev, 2, "seek failed");
    930 #endif
    931 			fdcretry(fdc);
    932 			goto loop;
    933 		}
    934 		fd->sc_cylin = bp->b_cylinder;
    935 		goto doio;
    936 
    937 	case IOTIMEDOUT:
    938 		FDCDMA_ABORT(fdc);
    939 
    940 	case SEEKTIMEDOUT:
    941 	case RECALTIMEDOUT:
    942 	case RESETTIMEDOUT:
    943 		fdcretry(fdc);
    944 		goto loop;
    945 
    946 	case IOCOMPLETE: /* IO DONE, post-analyze */
    947 		callout_stop(&fdc->sc_timo_ch);
    948 
    949 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
    950 		    (bp->b_flags & B_READ));
    951 
    952 		i = fdcresult(fdc);
    953 		if (i != 7 || (st0 & 0xf8) != 0) {
    954 			FDCDMA_ABORT(fdc);
    955 #ifdef FD_DEBUG
    956 			fdcstatus(fd->sc_dev, 7, bp->b_flags & B_READ ?
    957 			    "read failed" : "write failed");
    958 			printf("blkno %" PRId64 " nblks %d\n",
    959 			    fd->sc_blkno, fd->sc_nblks);
    960 #endif
    961 			fdcretry(fdc);
    962 			goto loop;
    963 		}
    964 		FDCDMA_DONE(fdc);
    965 		if (fdc->sc_errors) {
    966 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
    967 			    fd->sc_skip / FDC_BSIZE, NULL);
    968 			printf("\n");
    969 			fdc->sc_errors = 0;
    970 		}
    971 		fd->sc_blkno += fd->sc_nblks;
    972 		fd->sc_skip += fd->sc_nbytes;
    973 		fd->sc_bcount -= fd->sc_nbytes;
    974 		if (fd->sc_bcount > 0) {
    975 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
    976 			goto doseek;
    977 		}
    978 		fdfinish(fd, bp);
    979 		goto loop;
    980 
    981 	case DORESET:
    982 		/* try a reset, keep motor on */
    983 		fd_set_motor(fdc, 1);
    984 		delay(100);
    985 		fd_set_motor(fdc, 0);
    986 		fdc->sc_state = RESETCOMPLETE;
    987 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
    988 		return 1;			/* will return later */
    989 
    990 	case RESETCOMPLETE:
    991 		callout_stop(&fdc->sc_timo_ch);
    992 		/* clear the controller output buffer */
    993 		for (i = 0; i < 4; i++) {
    994 			out_fdc(iot, ioh, NE7CMD_SENSEI);
    995 			(void)fdcresult(fdc);
    996 		}
    997 
    998 		/* fall through */
    999 	case DORECAL:
   1000 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
   1001 		out_fdc(iot, ioh, fd->sc_drive);
   1002 		fdc->sc_state = RECALWAIT;
   1003 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
   1004 		return 1;			/* will return later */
   1005 
   1006 	case RECALWAIT:
   1007 		callout_stop(&fdc->sc_timo_ch);
   1008 		fdc->sc_state = RECALCOMPLETE;
   1009 		/* allow 1/30 second for heads to settle */
   1010 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
   1011 		return 1;			/* will return later */
   1012 
   1013 	case RECALCOMPLETE:
   1014 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1015 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1016 #ifdef FD_DEBUG
   1017 			fdcstatus(fd->sc_dev, 2, "recalibrate failed");
   1018 #endif
   1019 			fdcretry(fdc);
   1020 			goto loop;
   1021 		}
   1022 		fd->sc_cylin = 0;
   1023 		goto doseek;
   1024 
   1025 	case MOTORWAIT:
   1026 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1027 			return 1;		/* time's not up yet */
   1028 		goto doseek;
   1029 
   1030 	default:
   1031 		fdcstatus(fd->sc_dev, 0, "stray interrupt");
   1032 		return 1;
   1033 	}
   1034 #ifdef DIAGNOSTIC
   1035 	panic("%s: impossible", __func__);
   1036 #endif
   1037 #undef	st0
   1038 #undef	cyl
   1039 }
   1040 
   1041 void
   1042 fdcretry(struct fdc_softc *fdc)
   1043 {
   1044 	struct fd_softc *fd;
   1045 	struct buf *bp;
   1046 
   1047 	fd = TAILQ_FIRST(&fdc->sc_drives);
   1048 	bp = bufq_peek(fd->sc_q);
   1049 
   1050 	switch (fdc->sc_errors) {
   1051 	case 0:
   1052 		/* try again */
   1053 		fdc->sc_state = DOSEEK;
   1054 		break;
   1055 
   1056 	case 1:
   1057 	case 2:
   1058 	case 3:
   1059 		/* didn't work; try recalibrating */
   1060 		fdc->sc_state = DORECAL;
   1061 		break;
   1062 
   1063 	case 4:
   1064 		/* still no go; reset the bastard */
   1065 		fdc->sc_state = DORESET;
   1066 		break;
   1067 
   1068 	default:
   1069 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1070 		    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1071 
   1072 		fdcpstatus(7, fdc);
   1073 		bp->b_error = EIO;
   1074 		fdfinish(fd, bp);
   1075 	}
   1076 	fdc->sc_errors++;
   1077 }
   1078 
   1079 int
   1080 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1081 {
   1082 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
   1083 	struct disklabel buffer;
   1084 	int error;
   1085 
   1086 	switch (cmd) {
   1087 	case DIOCGDINFO:
   1088 		memset(&buffer, 0, sizeof(buffer));
   1089 
   1090 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1091 		buffer.d_type = DKTYPE_FLOPPY;
   1092 		buffer.d_secsize = FDC_BSIZE;
   1093 
   1094 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1095 			return EINVAL;
   1096 
   1097 		*(struct disklabel *)addr = buffer;
   1098 		return 0;
   1099 
   1100 	case DIOCWLABEL:
   1101 		if ((flag & FWRITE) == 0)
   1102 			return EBADF;
   1103 		/* XXX do something */
   1104 		return 0;
   1105 
   1106 	case DIOCWDINFO:
   1107 		if ((flag & FWRITE) == 0)
   1108 			return EBADF;
   1109 
   1110 		error = setdisklabel(&buffer, (struct disklabel *)addr,
   1111 		    0, NULL);
   1112 		if (error)
   1113 			return error;
   1114 
   1115 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1116 		return error;
   1117 
   1118 	default:
   1119 		return ENOTTY;
   1120 	}
   1121 
   1122 #ifdef DIAGNOSTIC
   1123 	panic("%s: impossible", __func__);
   1124 #endif
   1125 }
   1126 
   1127 /*
   1128  * Mountroot hook: prompt the user to enter the root file system floppy.
   1129  */
   1130 void
   1131 fd_mountroot_hook(device_t dev)
   1132 {
   1133 	int c;
   1134 
   1135 	printf("Insert filesystem floppy and press return.");
   1136 	cnpollc(1);
   1137 	for (;;) {
   1138 		c = cngetc();
   1139 		if ((c == '\r') || (c == '\n')) {
   1140 			printf("\n");
   1141 			break;
   1142 		}
   1143 	}
   1144 	cnpollc(0);
   1145 }
   1146