Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.18
      1 /*	$NetBSD: fd.c,v 1.18 1998/06/30 11:59:10 msaitoh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
      5  * Copyright (c) 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Don Ahn.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/conf.h>
     46 #include <sys/file.h>
     47 #include <sys/stat.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/malloc.h>
     50 #include <sys/device.h>
     51 #include <sys/disklabel.h>
     52 #include <sys/dkstat.h>
     53 #include <sys/disk.h>
     54 #include <sys/buf.h>
     55 #include <sys/uio.h>
     56 #include <sys/syslog.h>
     57 #include <sys/queue.h>
     58 
     59 #include <machine/cpu.h>
     60 
     61 #include <x68k/x68k/iodevice.h>
     62 #include <x68k/dev/dmavar.h>
     63 #include <x68k/dev/fdreg.h>
     64 #include <x68k/dev/opmreg.h>
     65 
     66 #include "locators.h"
     67 
     68 #define infdc   (IODEVbase->io_fdc)
     69 
     70 #ifdef DEBUG
     71 #define DPRINTF(x)      if (fddebug) printf x
     72 int     fddebug = 0;
     73 #else
     74 #define DPRINTF(x)
     75 #endif
     76 
     77 #define FDUNIT(dev)	(minor(dev) / 8)
     78 #define FDTYPE(dev)	(minor(dev) % 8)
     79 
     80 #define b_cylin b_resid
     81 
     82 enum fdc_state {
     83 	DEVIDLE = 0,
     84 	MOTORWAIT,
     85 	DOSEEK,
     86 	SEEKWAIT,
     87 	SEEKTIMEDOUT,
     88 	SEEKCOMPLETE,
     89 	DOIO,
     90 	IOCOMPLETE,
     91 	IOTIMEDOUT,
     92 	DORESET,
     93 	RESETCOMPLETE,
     94 	RESETTIMEDOUT,
     95 	DORECAL,
     96 	RECALWAIT,
     97 	RECALTIMEDOUT,
     98 	RECALCOMPLETE,
     99 	DOCOPY,
    100 	DOIOHALF,
    101 	COPYCOMPLETE,
    102 };
    103 
    104 /* software state, per controller */
    105 struct fdc_softc {
    106 	struct device sc_dev;		/* boilerplate */
    107 	u_char	sc_flags;
    108 
    109 	struct fd_softc *sc_fd[4];	/* pointers to children */
    110 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    111 	enum fdc_state sc_state;
    112 	int sc_errors;			/* number of retries so far */
    113 	u_char sc_status[7];		/* copy of registers */
    114 } fdc_softc;
    115 
    116 bdev_decl(fd);
    117 cdev_decl(fd);
    118 
    119 int fdcintr __P((void));
    120 void fdcreset __P((void));
    121 
    122 /* controller driver configuration */
    123 int fdcprobe __P((struct device *, void *, void *));
    124 void fdcattach __P((struct device *, struct device *, void *));
    125 int fdprint __P((void *, const char *));
    126 
    127 struct cfattach fdc_ca = {
    128 	sizeof(struct fdc_softc), fdcprobe, fdcattach
    129 };
    130 
    131 extern struct cfdriver fdc_cd;
    132 
    133 /*
    134  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    135  * we tell them apart.
    136  */
    137 struct fd_type {
    138 	int	sectrac;	/* sectors per track */
    139 	int	heads;		/* number of heads */
    140 	int	seccyl;		/* sectors per cylinder */
    141 	int	secsize;	/* size code for sectors */
    142 	int	datalen;	/* data len when secsize = 0 */
    143 	int	steprate;	/* step rate and head unload time */
    144 	int	gap1;		/* gap len between sectors */
    145 	int	gap2;		/* formatting gap */
    146 	int	tracks;		/* total num of tracks */
    147 	int	size;		/* size of disk in sectors */
    148 	int	step;		/* steps per cylinder */
    149 	int	rate;		/* transfer speed code */
    150 	char	*name;
    151 };
    152 
    153 /* The order of entries in the following table is important -- BEWARE! */
    154 struct fd_type fd_types[] = {
    155         {  8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS, "1.2MB/[1024bytes/sector]"    }, /* 1.2 MB japanese format */
    156         { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    }, /* 1.44MB diskette */
    157         { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB"    }, /* 1.2 MB AT-diskettes */
    158         {  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" }, /* 360kB in 1.2MB drive */
    159         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" }, /* 360kB PC diskettes */
    160         {  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB"    }, /* 3.5" 720kB diskette */
    161         {  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x"  }, /* 720kB in 1.2MB drive */
    162         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x"  }, /* 360kB in 720kB drive */
    163 };
    164 
    165 /* software state, per disk (with up to 4 disks per ctlr) */
    166 struct fd_softc {
    167 	struct device sc_dev;
    168 	struct disk sc_dk;
    169 
    170 	struct fd_type *sc_deftype;	/* default type descriptor */
    171 	struct fd_type *sc_type;	/* current type descriptor */
    172 
    173 	daddr_t	sc_blkno;	/* starting block number */
    174 	int sc_bcount;		/* byte count left */
    175 	int sc_skip;		/* bytes already transferred */
    176 	int sc_nblks;		/* number of blocks currently tranferring */
    177 	int sc_nbytes;		/* number of bytes currently tranferring */
    178 
    179 	int sc_drive;		/* physical unit number */
    180 	int sc_flags;
    181 #define	FD_BOPEN	0x01		/* it's open */
    182 #define	FD_COPEN	0x02		/* it's open */
    183 #define	FD_OPEN		(FD_BOPEN|FD_COPEN)	/* it's open */
    184 #define	FD_MOTOR	0x04		/* motor should be on */
    185 #define	FD_MOTOR_WAIT	0x08		/* motor coming up */
    186 #define	FD_ALIVE	0x10		/* alive */
    187 	int sc_cylin;		/* where we think the head is */
    188 
    189 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    190 	int sc_ops;		/* I/O ops since last switch */
    191 	struct buf sc_q;	/* head of buf chain */
    192 	u_char *sc_copybuf;	/* for secsize >=3 */
    193 	u_char sc_part;		/* for secsize >=3 */
    194 #define	SEC_P10	0x02		/* first part */
    195 #define	SEC_P01	0x01		/* second part */
    196 #define	SEC_P11	0x03		/* both part */
    197 };
    198 
    199 /* floppy driver configuration */
    200 int fdprobe __P((struct device *, void *, void *));
    201 void fdattach __P((struct device *, struct device *, void *));
    202 
    203 struct cfattach fd_ca = {
    204 	sizeof(struct fd_softc), fdprobe, fdattach
    205 };
    206 
    207 extern struct cfdriver fd_cd;
    208 
    209 void fdstrategy __P((struct buf *));
    210 void fdstart __P((struct fd_softc *fd));
    211 
    212 struct dkdriver fddkdriver = { fdstrategy };
    213 
    214 void fd_set_motor __P((struct fdc_softc *fdc, int reset));
    215 void fd_motor_off __P((void *arg));
    216 void fd_motor_on __P((void *arg));
    217 int fdcresult __P((struct fdc_softc *fdc));
    218 int out_fdc __P((u_char x));
    219 void fdcstart __P((struct fdc_softc *fdc));
    220 void fdcstatus __P((struct device *dv, int n, char *s));
    221 void fdctimeout __P((void *arg));
    222 void fdcpseudointr __P((void *arg));
    223 void fdcretry __P((struct fdc_softc *fdc));
    224 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
    225 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
    226 static int fdcpoll __P((struct fdc_softc *));
    227 static int fdgetdisklabel __P((struct fd_softc *, dev_t));
    228 static void fd_do_eject __P((int));
    229 
    230 void fd_mountroot_hook __P((struct device *));
    231 
    232 /* dma transfer routines */
    233 __inline static void fdc_dmastart __P((int, caddr_t, int));
    234 void fdcdmaintr __P((void));
    235 void fdcdmaerrintr __P((void));
    236 
    237 #define FDDI_EN	0x02
    238 #define FDCI_EN	0x04
    239 #define	FDD_INT	0x40
    240 #define	FDC_INT	0x80
    241 
    242 #define DMA_BRD	0x01
    243 #define	DMA_BWR	0x02
    244 
    245 #define DRQ 0
    246 
    247 static u_char *fdc_dmabuf;
    248 
    249 __inline static void
    250 fdc_dmastart(read, addr, count)
    251 	int read;
    252 	caddr_t addr;
    253 	int count;
    254 {
    255 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
    256 
    257 	DPRINTF(("fdc_dmastart: (%s, addr = %p, count = %d\n",
    258 		 read ? "read" : "write", addr, count));
    259 	if (dmarangecheck((vm_offset_t)addr, count)) {
    260 		dma_bouncebytes[DRQ] = count;
    261 		dma_dataaddr[DRQ] = addr;
    262 		if (!(read)) {
    263 			bcopy(addr, dma_bouncebuf[DRQ], count);
    264 			dma_bounced[DRQ] = DMA_BWR;
    265 		} else {
    266 			dma_bounced[DRQ] = DMA_BRD;
    267 		}
    268 		addr = dma_bouncebuf[DRQ];
    269 	} else {
    270 		dma_bounced[DRQ] = 0;
    271 	}
    272 
    273 	dmac->csr = 0xff;
    274 	dmac->ocr = read ? 0xb2 : 0x32;
    275 	dmac->mtc = (unsigned short)count;
    276 	asm("nop");
    277 	asm("nop");
    278 	dmac->mar = (unsigned long)kvtop(addr);
    279 #if defined(M68040) || defined(M68060)
    280 		/*
    281 		 * Push back dirty cache lines
    282 		 */
    283 		if (mmutype == MMU_68040)
    284 			DCFP(kvtop(addr));
    285 #endif
    286 	dmac->ccr = 0x88;
    287 }
    288 
    289 void
    290 fdcdmaintr()
    291 {
    292 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
    293 	dmac->csr = 0xff;
    294 	PCIA(); /* XXX? by oki */
    295 	if (dma_bounced[DRQ] == DMA_BRD) {
    296 		bcopy(dma_bouncebuf[DRQ], dma_dataaddr[DRQ], dma_bouncebytes[DRQ]);
    297 	}
    298 	dma_bounced[DRQ] = 0;
    299 }
    300 
    301 void
    302 fdcdmaerrintr()
    303 {
    304 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
    305 	printf("fdcdmaerrintr: csr=%x, cer=%x\n", dmac->csr, dmac->cer);
    306 	dmac->csr = 0xff;
    307 }
    308 
    309 int
    310 fdcprobe(parent, match, aux)
    311 	struct device *parent;
    312 	void *match, *aux;
    313 {
    314 	if (strcmp("fdc", aux) != 0)
    315 		return 0;
    316 	return 1;
    317 }
    318 
    319 /*
    320  * Arguments passed between fdcattach and fdprobe.
    321  */
    322 struct fdc_attach_args {
    323 	int fa_drive;
    324 	struct fd_type *fa_deftype;
    325 };
    326 
    327 /*
    328  * Print the location of a disk drive (called just before attaching the
    329  * the drive).  If `fdc' is not NULL, the drive was found but was not
    330  * in the system config file; print the drive name as well.
    331  * Return QUIET (config_find ignores this if the device was configured) to
    332  * avoid printing `fdN not configured' messages.
    333  */
    334 int
    335 fdprint(aux, fdc)
    336 	void *aux;
    337 	const char *fdc;
    338 {
    339 	register struct fdc_attach_args *fa = aux;
    340 
    341 	if (!fdc)
    342 		printf(" drive %d", fa->fa_drive);
    343 	return QUIET;
    344 }
    345 
    346 void
    347 fdcattach(parent, self, aux)
    348 	struct device *parent, *self;
    349 	void *aux;
    350 {
    351 	struct fdc_softc *fdc = (void *)self;
    352 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
    353 	struct fdc_attach_args fa;
    354 
    355 	fdc->sc_state = DEVIDLE;
    356 	TAILQ_INIT(&fdc->sc_drives);
    357 
    358 	fdc->sc_flags  = 0;
    359 
    360 	/* reset */
    361 	ioctlr.intr &= (~FDDI_EN);
    362 	ioctlr.intr |= FDCI_EN;
    363 	fdcresult(fdc);
    364 	fdcreset();
    365 
    366 	/* Initialize DMAC channel */
    367 	dmac->dcr = 0x80;
    368 	dmac->scr = 0x04;
    369 	dmac->csr = 0xff;
    370 	dmac->cpr = 0x00;
    371 	dmac->dar = (unsigned long) kvtop((void *)&infdc.data);
    372 	dmac->mfc = 0x05;
    373 	dmac->dfc = 0x05;
    374 	dmac->bfc = 0x05;
    375 	dmac->niv = 0x64;
    376 	dmac->eiv = 0x65;
    377 
    378 	printf(": uPD72065 FDC\n");
    379 	out_fdc(NE7CMD_SPECIFY);/* specify command */
    380 	out_fdc(0xd0);
    381 	out_fdc(0x10);
    382 
    383 	fdc_dmabuf = (u_char *)malloc(NBPG, M_DEVBUF, M_WAITOK);
    384 	if (fdc_dmabuf == 0)
    385 		printf("fdcattach: WARNING!! malloc() failed.\n");
    386 	dma_bouncebuf[DRQ] = fdc_dmabuf;
    387 
    388 	/* physical limit: four drives per controller. */
    389 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    390 		(void)config_found(self, (void *)&fa, fdprint);
    391 	}
    392 }
    393 
    394 void
    395 fdcreset()
    396 {
    397 	infdc.stat = FDC_RESET;
    398 }
    399 
    400 static int
    401 fdcpoll(fdc)
    402 	struct fdc_softc *fdc;
    403 {
    404 	int i = 25000;
    405 	while (--i > 0) {
    406 		if ((ioctlr.intr & 0x80)) {
    407 			out_fdc(NE7CMD_SENSEI);
    408 			fdcresult(fdc);
    409 			break;
    410 		}
    411 		DELAY(100);
    412 	}
    413 	return i;
    414 }
    415 
    416 int
    417 fdprobe(parent, match, aux)
    418 	struct device *parent;
    419 	void *match, *aux;
    420 {
    421 	struct fdc_softc *fdc = (void *)parent;
    422 	struct cfdata *cf = match;
    423 	struct fd_type *type;
    424 	int drive = cf->cf_unit;
    425 	int n;
    426 	int found = 0;
    427 	int i;
    428 
    429 	if (cf->cf_loc[FDCCF_UNIT] != FDCCF_UNIT_DEFAULT &&
    430 	    cf->cf_loc[FDCCF_UNIT] != drive)
    431 		return 0;
    432 
    433 	type = &fd_types[0];	/* XXX 1.2MB */
    434 
    435 	ioctlr.intr &= (~FDCI_EN);
    436 
    437 	/* select drive and turn on motor */
    438 	infdc.select = 0x80 | (type->rate << 4)| drive;
    439 	fdc_force_ready(FDCRDY);
    440 	fdcpoll(fdc);
    441 
    442 retry:
    443 	out_fdc(NE7CMD_RECAL);
    444 	out_fdc(drive);
    445 
    446 	i = 25000;
    447 	while (--i > 0) {
    448 		if ((ioctlr.intr & 0x80)) {
    449 			out_fdc(NE7CMD_SENSEI);
    450 			n = fdcresult(fdc);
    451 			break;
    452 		}
    453 		DELAY(100);
    454 	}
    455 
    456 #ifdef FDDEBUG
    457 	{
    458 		int i;
    459 		printf("fdprobe: status");
    460 		for (i = 0; i < n; i++)
    461 			printf(" %x", fdc->sc_status[i]);
    462 		printf("\n");
    463 	}
    464 #endif
    465 
    466 	if (n == 2) {
    467 		if ((fdc->sc_status[0] & 0xf0) == 0x20) {
    468 			found = 1;
    469 		} else if ((fdc->sc_status[0] & 0xf0) == 0xc0) {
    470 			goto retry;
    471 		}
    472 	}
    473 
    474 	/* turn off motor */
    475 	infdc.select = (type->rate << 4)| drive;
    476 	fdc_force_ready(FDCSTBY);
    477 	if (!found) {
    478 		ioctlr.intr |= FDCI_EN;
    479 		return 0;
    480 	}
    481 
    482 	return 1;
    483 }
    484 
    485 void
    486 fdattach(parent, self, aux)
    487 	struct device *parent;
    488 	struct device *self;
    489 	void *aux;
    490 {
    491 	struct fdc_softc *fdc = (void *)parent;
    492 	register struct fd_softc *fd = (void *)self;
    493 	struct fdc_attach_args *fa = aux;
    494 	int drive = fa->fa_drive;
    495 	struct fd_type *type = &fd_types[0];	/* XXX 1.2MB */
    496 
    497 	fd->sc_flags = 0;
    498 
    499 	ioctlr.intr |= FDCI_EN;
    500 
    501 	if (type)
    502 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    503 			type->tracks, type->heads, type->sectrac);
    504 	else
    505 		printf(": density unknown\n");
    506 
    507 	fd->sc_cylin = -1;
    508 	fd->sc_drive = drive;
    509 	fd->sc_deftype = type;
    510 	fdc->sc_fd[drive] = fd;
    511 
    512 	fd->sc_copybuf = (u_char *)malloc(NBPG, M_DEVBUF, M_WAITOK);
    513 	if (fd->sc_copybuf == 0)
    514 		printf("fdprobe: WARNING!! malloc() failed.\n");
    515 	fd->sc_flags |= FD_ALIVE;
    516 
    517 	/*
    518 	 * Initialize and attach the disk structure.
    519 	 */
    520 	fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
    521 	fd->sc_dk.dk_driver = &fddkdriver;
    522 	disk_attach(&fd->sc_dk);
    523 
    524 	/*
    525 	 * Establish a mountroot_hook anyway in case we booted
    526 	 * with RB_ASKNAME and get selected as the boot device.
    527 	 */
    528 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
    529 }
    530 
    531 __inline struct fd_type *
    532 fd_dev_to_type(fd, dev)
    533 	struct fd_softc *fd;
    534 	dev_t dev;
    535 {
    536 	int type = FDTYPE(dev);
    537 
    538 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    539 		return NULL;
    540 	return &fd_types[type];
    541 }
    542 
    543 void
    544 fdstrategy(bp)
    545 	register struct buf *bp;	/* IO operation to perform */
    546 {
    547 	struct fd_softc *fd;
    548 	int unit = FDUNIT(bp->b_dev);
    549 	int sz;
    550  	int s;
    551 
    552 	if (unit >= fd_cd.cd_ndevs ||
    553 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
    554 	    bp->b_blkno < 0 ||
    555 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    556 #ifdef FDDEBUG
    557 		printf("fdstrategy: unit=%d, blkno=%d, bcount=%d\n", unit,
    558 		       bp->b_blkno, bp->b_bcount);
    559 #endif
    560 		bp->b_error = EINVAL;
    561 		goto bad;
    562 	}
    563 
    564 	/* If it's a null transfer, return immediately. */
    565 	if (bp->b_bcount == 0)
    566 		goto done;
    567 
    568 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    569 
    570 	if (bp->b_blkno + sz > (fd->sc_type->size << (fd->sc_type->secsize - 2))) {
    571 		sz = (fd->sc_type->size << (fd->sc_type->secsize - 2)) - bp->b_blkno;
    572 		if (sz == 0) {
    573 			/* If exactly at end of disk, return EOF. */
    574 			bp->b_resid = bp->b_bcount;
    575 			goto done;
    576 		}
    577 		if (sz < 0) {
    578 			/* If past end of disk, return EINVAL. */
    579 			bp->b_error = EINVAL;
    580 			goto bad;
    581 		}
    582 		/* Otherwise, truncate request. */
    583 		bp->b_bcount = sz << DEV_BSHIFT;
    584 	}
    585 
    586  	bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE)
    587 		/ (fd->sc_type->seccyl * (1 << (fd->sc_type->secsize - 2)));
    588 
    589 	DPRINTF(("fdstrategy: %s b_blkno %d b_bcount %ld cylin %ld\n",
    590 		 bp->b_flags & B_READ ? "read" : "write",
    591 		 bp->b_blkno, bp->b_bcount, bp->b_cylin));
    592 	/* Queue transfer on drive, activate drive and controller if idle. */
    593 	s = splbio();
    594 	disksort(&fd->sc_q, bp);
    595 	untimeout(fd_motor_off, fd); /* a good idea */
    596 	if (!fd->sc_q.b_active)
    597 		fdstart(fd);
    598 #ifdef DIAGNOSTIC
    599 	else {
    600 		struct fdc_softc *fdc = fdc_cd.cd_devs[0];	/* XXX */
    601 		if (fdc->sc_state == DEVIDLE) {
    602 			printf("fdstrategy: controller inactive\n");
    603 			fdcstart(fdc);
    604 		}
    605 	}
    606 #endif
    607 	splx(s);
    608 	return;
    609 
    610 bad:
    611 	bp->b_flags |= B_ERROR;
    612 done:
    613 	/* Toss transfer; we're done early. */
    614 	biodone(bp);
    615 }
    616 
    617 void
    618 fdstart(fd)
    619 	struct fd_softc *fd;
    620 {
    621 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    622 	int active = fdc->sc_drives.tqh_first != 0;
    623 
    624 	/* Link into controller queue. */
    625 	fd->sc_q.b_active = 1;
    626 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    627 
    628 	/* If controller not already active, start it. */
    629 	if (!active)
    630 		fdcstart(fdc);
    631 }
    632 
    633 void
    634 fdfinish(fd, bp)
    635 	struct fd_softc *fd;
    636 	struct buf *bp;
    637 {
    638 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    639 
    640 	/*
    641 	 * Move this drive to the end of the queue to give others a `fair'
    642 	 * chance.  We only force a switch if N operations are completed while
    643 	 * another drive is waiting to be serviced, since there is a long motor
    644 	 * startup delay whenever we switch.
    645 	 */
    646 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    647 		fd->sc_ops = 0;
    648 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    649 		if (bp->b_actf) {
    650 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    651 		} else
    652 			fd->sc_q.b_active = 0;
    653 	}
    654 	bp->b_resid = fd->sc_bcount;
    655 	fd->sc_skip = 0;
    656 	fd->sc_q.b_actf = bp->b_actf;
    657 	biodone(bp);
    658 	/* turn off motor 5s from now */
    659 	timeout(fd_motor_off, fd, 5 * hz);
    660 	fdc->sc_state = DEVIDLE;
    661 }
    662 
    663 int
    664 fdread(dev, uio, flags)
    665 	dev_t dev;
    666 	struct uio *uio;
    667 	int flags;
    668 {
    669 
    670 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    671 }
    672 
    673 int
    674 fdwrite(dev, uio, flags)
    675 	dev_t dev;
    676 	struct uio *uio;
    677 	int flags;
    678 {
    679 
    680 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    681 }
    682 
    683 void
    684 fd_set_motor(fdc, reset)
    685 	struct fdc_softc *fdc;
    686 	int reset;
    687 {
    688 	struct fd_softc *fd;
    689 	int n;
    690 
    691 	DPRINTF(("fd_set_motor:\n"));
    692 	for (n = 0; n < 4; n++)
    693 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) {
    694 			infdc.select = 0x80 | (fd->sc_type->rate << 4)| n;
    695 		}
    696 }
    697 
    698 void
    699 fd_motor_off(arg)
    700 	void *arg;
    701 {
    702 	struct fd_softc *fd = arg;
    703 	int s;
    704 
    705 	DPRINTF(("fd_motor_off:\n"));
    706 
    707 	s = splbio();
    708 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    709 	infdc.select = (fd->sc_type->rate << 4) | fd->sc_drive;
    710 #if 0
    711 	fd_set_motor((struct fdc_softc *)&fdc_softc[0], 0); /* XXX */
    712 #endif
    713 	splx(s);
    714 }
    715 
    716 void
    717 fd_motor_on(arg)
    718 	void *arg;
    719 {
    720 	struct fd_softc *fd = arg;
    721 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    722 	int s;
    723 
    724 	DPRINTF(("fd_motor_on:\n"));
    725 
    726 	s = splbio();
    727 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    728 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    729 		(void) fdcintr();
    730 	splx(s);
    731 }
    732 
    733 int
    734 fdcresult(fdc)
    735 	struct fdc_softc *fdc;
    736 {
    737 	u_char i;
    738 	int j = 100000,
    739 	    n = 0;
    740 
    741 	for (; j; j--) {
    742 
    743 		i = infdc.stat & (NE7_DIO | NE7_RQM | NE7_CB);
    744 
    745 
    746 		if (i == NE7_RQM)
    747 			return n;
    748 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    749 			if (n >= sizeof(fdc->sc_status)) {
    750 				log(LOG_ERR, "fdcresult: overrun\n");
    751 				return -1;
    752 			}
    753 			fdc->sc_status[n++] = infdc.data;
    754 		}
    755 	}
    756 	log(LOG_ERR, "fdcresult: timeout\n");
    757 	return -1;
    758 }
    759 
    760 int
    761 out_fdc(x)
    762 	u_char x;
    763 {
    764 	int i = 100000;
    765 
    766 	while ((infdc.stat & NE7_DIO) && i-- > 0);
    767 	if (i <= 0)
    768 		return -1;
    769 	while ((infdc.stat & NE7_RQM) == 0 && i-- > 0);
    770 	if (i <= 0)
    771 		return -1;
    772 
    773 	infdc.data = x;
    774 
    775 	return 0;
    776 }
    777 
    778 int
    779 fdopen(dev, flags, mode, p)
    780 	dev_t dev;
    781 	int flags, mode;
    782 	struct proc *p;
    783 {
    784  	int unit;
    785 	struct fd_softc *fd;
    786 	struct fd_type *type;
    787 
    788 	unit = FDUNIT(dev);
    789 	if (unit >= fd_cd.cd_ndevs)
    790 		return ENXIO;
    791 	fd = fd_cd.cd_devs[unit];
    792 	if (fd == 0)
    793 		return ENXIO;
    794 	type = fd_dev_to_type(fd, dev);
    795 	if (type == NULL)
    796 		return ENXIO;
    797 
    798 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    799 	    fd->sc_type != type)
    800 		return EBUSY;
    801 
    802 	if ((fd->sc_flags & FD_OPEN) == 0) {
    803 		/* Lock eject button */
    804 		infdc.drvstat = 0x40 | ( 1 << unit);
    805 		infdc.drvstat = 0x40;
    806 	}
    807 
    808 	fd->sc_type = type;
    809 	fd->sc_cylin = -1;
    810 
    811 	switch (mode) {
    812 	case S_IFCHR:
    813 		fd->sc_flags |= FD_COPEN;
    814 		break;
    815 	case S_IFBLK:
    816 		fd->sc_flags |= FD_BOPEN;
    817 		break;
    818 	}
    819 
    820 	fdgetdisklabel(fd, dev);
    821 
    822 	return 0;
    823 }
    824 
    825 int
    826 fdclose(dev, flags, mode, p)
    827 	dev_t dev;
    828 	int flags, mode;
    829 	struct proc *p;
    830 {
    831  	int unit = FDUNIT(dev);
    832 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
    833 
    834 	DPRINTF(("fdclose %d\n", unit));
    835 
    836 	switch (mode) {
    837 	case S_IFCHR:
    838 		fd->sc_flags &= ~FD_COPEN;
    839 		break;
    840 	case S_IFBLK:
    841 		fd->sc_flags &= ~FD_BOPEN;
    842 		break;
    843 	}
    844 
    845 	if ((fd->sc_flags & FD_OPEN) == 0) {
    846 		infdc.drvstat = ( 1 << unit);
    847 		infdc.drvstat = 0x00;
    848 	}
    849 	return 0;
    850 }
    851 
    852 void
    853 fdcstart(fdc)
    854 	struct fdc_softc *fdc;
    855 {
    856 
    857 #ifdef DIAGNOSTIC
    858 	/* only got here if controller's drive queue was inactive; should
    859 	   be in idle state */
    860 	if (fdc->sc_state != DEVIDLE) {
    861 		printf("fdcstart: not idle\n");
    862 		return;
    863 	}
    864 #endif
    865 	(void) fdcintr();
    866 }
    867 
    868 void
    869 fdcstatus(dv, n, s)
    870 	struct device *dv;
    871 	int n;
    872 	char *s;
    873 {
    874 	struct fdc_softc *fdc = (void *)dv->dv_parent;
    875 	char bits[64];
    876 
    877 	if (n == 0) {
    878 		out_fdc(NE7CMD_SENSEI);
    879 		(void) fdcresult(fdc);
    880 		n = 2;
    881 	}
    882 
    883 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
    884 
    885 	switch (n) {
    886 	case 0:
    887 		printf("\n");
    888 		break;
    889 	case 2:
    890 		printf(" (st0 %s cyl %d)\n",
    891 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
    892 		    bits, sizeof(bits)), fdc->sc_status[1]);
    893 		break;
    894 	case 7:
    895 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
    896 		    NE7_ST0BITS, bits, sizeof(bits)));
    897 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
    898 		    NE7_ST1BITS, bits, sizeof(bits)));
    899 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
    900 		    NE7_ST2BITS, bits, sizeof(bits)));
    901 		printf(" cyl %d head %d sec %d)\n",
    902 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    903 		break;
    904 #ifdef DIAGNOSTIC
    905 	default:
    906 		printf(" fdcstatus: weird size: %d\n", n);
    907 		break;
    908 #endif
    909 	}
    910 }
    911 
    912 void
    913 fdctimeout(arg)
    914 	void *arg;
    915 {
    916 	struct fdc_softc *fdc = arg;
    917 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
    918 	int s;
    919 
    920 	s = splbio();
    921 	fdcstatus(&fd->sc_dev, 0, "timeout");
    922 
    923 	if (fd->sc_q.b_actf)
    924 		fdc->sc_state++;
    925 	else
    926 		fdc->sc_state = DEVIDLE;
    927 
    928 	(void) fdcintr();
    929 	splx(s);
    930 }
    931 
    932 void
    933 fdcpseudointr(arg)
    934 	void *arg;
    935 {
    936 	int s;
    937 
    938 	/* just ensure it has the right spl */
    939 	s = splbio();
    940 	(void) fdcintr();
    941 	splx(s);
    942 }
    943 
    944 int
    945 fdcintr()
    946 {
    947 	struct fdc_softc *fdc = fdc_cd.cd_devs[0];	/* XXX */
    948 #define	st0	fdc->sc_status[0]
    949 #define	cyl	fdc->sc_status[1]
    950 	struct fd_softc *fd;
    951 	struct buf *bp;
    952 	int read, head, sec, pos, i, sectrac, nblks;
    953 	int	tmp;
    954 	struct fd_type *type;
    955 
    956 loop:
    957 	fd = fdc->sc_drives.tqh_first;
    958 	if (fd == NULL) {
    959 		DPRINTF(("fdcintr: set DEVIDLE\n"));
    960 		if (fdc->sc_state == DEVIDLE) {
    961 			if ((ioctlr.intr & 0x80)) {
    962 				out_fdc(NE7CMD_SENSEI);
    963 				if ((tmp = fdcresult(fdc)) != 2 || (st0 & 0xf8) != 0x20) {
    964 					goto loop;
    965 				}
    966 			}
    967 		}
    968 		/* no drives waiting; end */
    969 		fdc->sc_state = DEVIDLE;
    970  		return 1;
    971 	}
    972 
    973 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    974 	bp = fd->sc_q.b_actf;
    975 	if (bp == NULL) {
    976 		fd->sc_ops = 0;
    977 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    978 		fd->sc_q.b_active = 0;
    979 		goto loop;
    980 	}
    981 
    982 	switch (fdc->sc_state) {
    983 	case DEVIDLE:
    984 		DPRINTF(("fdcintr: in DEVIDLE\n"));
    985 		fdc->sc_errors = 0;
    986 		fd->sc_skip = 0;
    987 		fd->sc_bcount = bp->b_bcount;
    988 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
    989 		untimeout(fd_motor_off, fd);
    990 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
    991 			fdc->sc_state = MOTORWAIT;
    992 			return 1;
    993 		}
    994 		if ((fd->sc_flags & FD_MOTOR) == 0) {
    995 			/* Turn on the motor */
    996 			/* being careful about other drives. */
    997 			for (i = 0; i < 4; i++) {
    998 				struct fd_softc *ofd = fdc->sc_fd[i];
    999 				if (ofd && ofd->sc_flags & FD_MOTOR) {
   1000 					untimeout(fd_motor_off, ofd);
   1001 					ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1002 					break;
   1003 				}
   1004 			}
   1005 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1006 			fd_set_motor(fdc, 0);
   1007 			fdc->sc_state = MOTORWAIT;
   1008 			/* allow .5s for motor to stabilize */
   1009 			timeout(fd_motor_on, fd, hz / 2);
   1010 			return 1;
   1011 		}
   1012 		/* Make sure the right drive is selected. */
   1013 		fd_set_motor(fdc, 0);
   1014 
   1015 		/* fall through */
   1016 	case DOSEEK:
   1017 	doseek:
   1018 		DPRINTF(("fdcintr: in DOSEEK\n"));
   1019 		if (fd->sc_cylin == bp->b_cylin)
   1020 			goto doio;
   1021 
   1022 		out_fdc(NE7CMD_SPECIFY);/* specify command */
   1023 		out_fdc(0xd0);		/* XXX const */
   1024 		out_fdc(0x10);
   1025 
   1026 		out_fdc(NE7CMD_SEEK);	/* seek function */
   1027 		out_fdc(fd->sc_drive);	/* drive number */
   1028 		out_fdc(bp->b_cylin * fd->sc_type->step);
   1029 
   1030 		fd->sc_cylin = -1;
   1031 		fdc->sc_state = SEEKWAIT;
   1032 
   1033 		fd->sc_dk.dk_seek++;
   1034 		disk_busy(&fd->sc_dk);
   1035 
   1036 		timeout(fdctimeout, fdc, 4 * hz);
   1037 		return 1;
   1038 
   1039 	case DOIO:
   1040 	doio:
   1041 		DPRINTF(("fdcintr: DOIO: "));
   1042 		type = fd->sc_type;
   1043 		sectrac = type->sectrac;
   1044 		pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
   1045 		sec = pos / (1 << (type->secsize - 2));
   1046 		if (type->secsize == 2) {
   1047 			fd->sc_part = SEC_P11;
   1048 			nblks = (sectrac - sec) << (type->secsize - 2);
   1049 			nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1050 			DPRINTF(("nblks(0)"));
   1051 		} else if ((fd->sc_blkno % 2) == 0) {
   1052 			if (fd->sc_bcount & 0x00000200) {
   1053 				if (fd->sc_bcount == FDC_BSIZE) {
   1054 					fd->sc_part = SEC_P10;
   1055 					nblks = 1;
   1056 					DPRINTF(("nblks(1)"));
   1057 				} else {
   1058 					fd->sc_part = SEC_P11;
   1059 					nblks = (sectrac - sec) * 2;
   1060 					nblks = min(nblks, fd->sc_bcount
   1061 						    / FDC_BSIZE - 1);
   1062 					DPRINTF(("nblks(2)"));
   1063 				}
   1064 			} else {
   1065 				fd->sc_part = SEC_P11;
   1066 				nblks = (sectrac - sec)
   1067 					<< (type->secsize - 2);
   1068 				nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1069 				DPRINTF(("nblks(3)"));
   1070 			}
   1071 		} else {
   1072 			fd->sc_part = SEC_P01;
   1073 			nblks = 1;
   1074 			DPRINTF(("nblks(4)"));
   1075 		}
   1076 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
   1077 		DPRINTF((" %d\n", nblks));
   1078 		fd->sc_nblks = nblks;
   1079 		fd->sc_nbytes = nblks * FDC_BSIZE;
   1080 		head = (fd->sc_blkno
   1081 			% (type->seccyl * (1 << (type->secsize - 2))))
   1082 			 / (type->sectrac * (1 << (type->secsize - 2)));
   1083 
   1084 #ifdef DIAGNOSTIC
   1085 		{int block;
   1086 		 block = ((fd->sc_cylin * type->heads + head) * type->sectrac
   1087 			  + sec) * (1 << (type->secsize - 2));
   1088 		 block += (fd->sc_part == SEC_P01) ? 1 : 0;
   1089 		 if (block != fd->sc_blkno) {
   1090 			 printf("C H R N: %d %d %d %d\n", fd->sc_cylin, head, sec, type->secsize);
   1091 			 printf("fdcintr: doio: block %d != blkno %d\n", block, fd->sc_blkno);
   1092 #ifdef DDB
   1093 			 Debugger();
   1094 #endif
   1095 		 }}
   1096 #endif
   1097 		read = bp->b_flags & B_READ;
   1098 		DPRINTF(("fdcintr: %s drive %d track %d head %d sec %d nblks %d, skip %d\n",
   1099 			 read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
   1100 			 head, sec, nblks, fd->sc_skip));
   1101 		DPRINTF(("C H R N: %d %d %d %d\n", fd->sc_cylin, head, sec,
   1102 			 type->secsize));
   1103 
   1104 		if (fd->sc_part != SEC_P11)
   1105 			goto docopy;
   1106 
   1107 		fdc_dmastart(read, bp->b_data + fd->sc_skip, fd->sc_nbytes);
   1108 		if (read)
   1109 			out_fdc(NE7CMD_READ);	/* READ */
   1110 		else
   1111 			out_fdc(NE7CMD_WRITE);	/* WRITE */
   1112 		out_fdc((head << 2) | fd->sc_drive);
   1113 		out_fdc(bp->b_cylin);		/* cylinder */
   1114 		out_fdc(head);
   1115 		out_fdc(sec + 1);		/* sector +1 */
   1116 		out_fdc(type->secsize);		/* sector size */
   1117 		out_fdc(type->sectrac);		/* sectors/track */
   1118 		out_fdc(type->gap1);		/* gap1 size */
   1119 		out_fdc(type->datalen);		/* data length */
   1120 		fdc->sc_state = IOCOMPLETE;
   1121 
   1122 		disk_busy(&fd->sc_dk);
   1123 
   1124 		/* allow 2 seconds for operation */
   1125 		timeout(fdctimeout, fdc, 2 * hz);
   1126 		return 1;				/* will return later */
   1127 
   1128 	case DOCOPY:
   1129 	docopy:
   1130 		DPRINTF(("fdcintr: DOCOPY:\n"));
   1131 		fdc_dmastart(B_READ, fd->sc_copybuf, 1024);
   1132 		out_fdc(NE7CMD_READ);	/* READ */
   1133 		out_fdc((head << 2) | fd->sc_drive);
   1134 		out_fdc(bp->b_cylin);		/* cylinder */
   1135 		out_fdc(head);
   1136 		out_fdc(sec + 1);		/* sector +1 */
   1137 		out_fdc(type->secsize);		/* sector size */
   1138 		out_fdc(type->sectrac);		/* sectors/track */
   1139 		out_fdc(type->gap1);		/* gap1 size */
   1140 		out_fdc(type->datalen);		/* data length */
   1141 		fdc->sc_state = COPYCOMPLETE;
   1142 		/* allow 2 seconds for operation */
   1143 		timeout(fdctimeout, fdc, 2 * hz);
   1144 		return 1;				/* will return later */
   1145 
   1146 	case DOIOHALF:
   1147 	doiohalf:
   1148 		DPRINTF((" DOIOHALF:\n"));
   1149 
   1150 #ifdef DIAGNOSTIC
   1151 		type = fd->sc_type;
   1152 		sectrac = type->sectrac;
   1153 		pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
   1154 		sec = pos / (1 << (type->secsize - 2));
   1155 		head = (fd->sc_blkno
   1156 			% (type->seccyl * (1 << (type->secsize - 2))))
   1157 			 / (type->sectrac * (1 << (type->secsize - 2)));
   1158 		{int block;
   1159 		 block = ((fd->sc_cylin * type->heads + head) * type->sectrac + sec)
   1160 			 * (1 << (type->secsize - 2));
   1161 		 block += (fd->sc_part == SEC_P01) ? 1 : 0;
   1162 		 if (block != fd->sc_blkno) {
   1163 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
   1164 #ifdef DDB
   1165 			 Debugger();
   1166 #endif
   1167 		 }}
   1168 #endif
   1169 		if (read = bp->b_flags & B_READ) {
   1170 			bcopy(fd->sc_copybuf
   1171 			      + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
   1172 			      bp->b_data + fd->sc_skip,
   1173 			      FDC_BSIZE);
   1174 			fdc->sc_state = IOCOMPLETE;
   1175 			goto iocomplete2;
   1176 		} else {
   1177 			bcopy(bp->b_data + fd->sc_skip,
   1178 			      fd->sc_copybuf
   1179 			      + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
   1180 			      FDC_BSIZE);
   1181 			fdc_dmastart(read, fd->sc_copybuf, 1024);
   1182 		}
   1183 		out_fdc(NE7CMD_WRITE);	/* WRITE */
   1184 		out_fdc((head << 2) | fd->sc_drive);
   1185 		out_fdc(bp->b_cylin);		/* cylinder */
   1186 		out_fdc(head);
   1187 		out_fdc(sec + 1);		/* sector +1 */
   1188 		out_fdc(fd->sc_type->secsize);		/* sector size */
   1189 		out_fdc(sectrac);		/* sectors/track */
   1190 		out_fdc(fd->sc_type->gap1);		/* gap1 size */
   1191 		out_fdc(fd->sc_type->datalen);		/* data length */
   1192 		fdc->sc_state = IOCOMPLETE;
   1193 		/* allow 2 seconds for operation */
   1194 		timeout(fdctimeout, fdc, 2 * hz);
   1195 		return 1;				/* will return later */
   1196 
   1197 	case SEEKWAIT:
   1198 		untimeout(fdctimeout, fdc);
   1199 		fdc->sc_state = SEEKCOMPLETE;
   1200 		/* allow 1/50 second for heads to settle */
   1201 /*		timeout(fdcpseudointr, fdc, hz / 50);*/
   1202 		return 1;
   1203 
   1204 	case SEEKCOMPLETE:
   1205 		/* Make sure seek really happened */
   1206 		DPRINTF(("fdcintr: SEEKCOMPLETE: FDC status = %x\n",
   1207 			 infdc.stat));
   1208 		out_fdc(NE7CMD_SENSEI);
   1209 		tmp = fdcresult(fdc);
   1210 		if ((st0 & 0xf8) == 0xc0) {
   1211 			DPRINTF(("fdcintr: first seek!\n"));
   1212 			fdc->sc_state = DORECAL;
   1213 			goto loop;
   1214 		} else if (tmp != 2 || (st0 & 0xf8) != 0x20 || cyl != bp->b_cylin) {
   1215 #ifdef FDDEBUG
   1216 			fdcstatus(&fd->sc_dev, 2, "seek failed");
   1217 #endif
   1218 			fdcretry(fdc);
   1219 			goto loop;
   1220 		}
   1221 		fd->sc_cylin = bp->b_cylin;
   1222 		goto doio;
   1223 
   1224 	case IOTIMEDOUT:
   1225 #if 0
   1226 		isa_dmaabort(fdc->sc_drq);
   1227 #endif
   1228 	case SEEKTIMEDOUT:
   1229 	case RECALTIMEDOUT:
   1230 	case RESETTIMEDOUT:
   1231 		fdcretry(fdc);
   1232 		goto loop;
   1233 
   1234 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1235 		untimeout(fdctimeout, fdc);
   1236 		DPRINTF(("fdcintr: in IOCOMPLETE\n"));
   1237 		if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
   1238 			printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
   1239 #if 0
   1240 			isa_dmaabort(fdc->sc_drq);
   1241 #endif
   1242 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1243 				  "read failed" : "write failed");
   1244 			printf("blkno %d nblks %d\n",
   1245 			    fd->sc_blkno, fd->sc_nblks);
   1246 			fdcretry(fdc);
   1247 			goto loop;
   1248 		}
   1249 #if 0
   1250 		isa_dmadone(bp->b_flags & B_READ, bp->b_data + fd->sc_skip,
   1251 		    nblks * FDC_BSIZE, fdc->sc_drq);
   1252 #endif
   1253 	iocomplete2:
   1254 		if (fdc->sc_errors) {
   1255 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1256 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1257 			printf("\n");
   1258 			fdc->sc_errors = 0;
   1259 		}
   1260 		fd->sc_blkno += fd->sc_nblks;
   1261 		fd->sc_skip += fd->sc_nbytes;
   1262 		fd->sc_bcount -= fd->sc_nbytes;
   1263 		DPRINTF(("fd->sc_bcount = %d\n", fd->sc_bcount));
   1264 		if (fd->sc_bcount > 0) {
   1265 			bp->b_cylin = fd->sc_blkno
   1266 				/ (fd->sc_type->seccyl
   1267 				   * (1 << (fd->sc_type->secsize - 2)));
   1268 			goto doseek;
   1269 		}
   1270 		fdfinish(fd, bp);
   1271 		goto loop;
   1272 
   1273 	case COPYCOMPLETE: /* IO DONE, post-analyze */
   1274 		DPRINTF(("fdcintr: COPYCOMPLETE:"));
   1275 		untimeout(fdctimeout, fdc);
   1276 		if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
   1277 			printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
   1278 #if 0
   1279 			isa_dmaabort(fdc->sc_drq);
   1280 #endif
   1281 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1282 				  "read failed" : "write failed");
   1283 			printf("blkno %d nblks %d\n",
   1284 			    fd->sc_blkno, fd->sc_nblks);
   1285 			fdcretry(fdc);
   1286 			goto loop;
   1287 		}
   1288 		goto doiohalf;
   1289 
   1290 	case DORESET:
   1291 		DPRINTF(("fdcintr: in DORESET\n"));
   1292 		/* try a reset, keep motor on */
   1293 		fd_set_motor(fdc, 1);
   1294 		DELAY(100);
   1295 		fd_set_motor(fdc, 0);
   1296 		fdc->sc_state = RESETCOMPLETE;
   1297 		timeout(fdctimeout, fdc, hz / 2);
   1298 		return 1;			/* will return later */
   1299 
   1300 	case RESETCOMPLETE:
   1301 		DPRINTF(("fdcintr: in RESETCOMPLETE\n"));
   1302 		untimeout(fdctimeout, fdc);
   1303 		/* clear the controller output buffer */
   1304 		for (i = 0; i < 4; i++) {
   1305 			out_fdc(NE7CMD_SENSEI);
   1306 			(void) fdcresult(fdc);
   1307 		}
   1308 
   1309 		/* fall through */
   1310 	case DORECAL:
   1311 		DPRINTF(("fdcintr: in DORECAL\n"));
   1312 		out_fdc(NE7CMD_RECAL);	/* recalibrate function */
   1313 		out_fdc(fd->sc_drive);
   1314 		fdc->sc_state = RECALWAIT;
   1315 		timeout(fdctimeout, fdc, 5 * hz);
   1316 		return 1;			/* will return later */
   1317 
   1318 	case RECALWAIT:
   1319 		DPRINTF(("fdcintr: in RECALWAIT\n"));
   1320 		untimeout(fdctimeout, fdc);
   1321 		fdc->sc_state = RECALCOMPLETE;
   1322 		/* allow 1/30 second for heads to settle */
   1323 /*		timeout(fdcpseudointr, fdc, hz / 30);*/
   1324 		return 1;			/* will return later */
   1325 
   1326 	case RECALCOMPLETE:
   1327 		DPRINTF(("fdcintr: in RECALCOMPLETE\n"));
   1328 		out_fdc(NE7CMD_SENSEI);
   1329 		tmp = fdcresult(fdc);
   1330 		if ((st0 & 0xf8) == 0xc0) {
   1331 			DPRINTF(("fdcintr: first seek!\n"));
   1332 			fdc->sc_state = DORECAL;
   1333 			goto loop;
   1334 		} else if (tmp != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1335 #ifdef FDDEBUG
   1336 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
   1337 #endif
   1338 			fdcretry(fdc);
   1339 			goto loop;
   1340 		}
   1341 		fd->sc_cylin = 0;
   1342 		goto doseek;
   1343 
   1344 	case MOTORWAIT:
   1345 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1346 			return 1;		/* time's not up yet */
   1347 		goto doseek;
   1348 
   1349 	default:
   1350 		fdcstatus(&fd->sc_dev, 0, "stray interrupt");
   1351 		return 1;
   1352 	}
   1353 #ifdef DIAGNOSTIC
   1354 	panic("fdcintr: impossible");
   1355 #endif
   1356 #undef	st0
   1357 #undef	cyl
   1358 }
   1359 
   1360 void
   1361 fdcretry(fdc)
   1362 	struct fdc_softc *fdc;
   1363 {
   1364 	struct fd_softc *fd;
   1365 	struct buf *bp;
   1366 	char bits[64];
   1367 
   1368 	DPRINTF(("fdcretry:\n"));
   1369 	fd = fdc->sc_drives.tqh_first;
   1370 	bp = fd->sc_q.b_actf;
   1371 
   1372 	switch (fdc->sc_errors) {
   1373 	case 0:
   1374 		/* try again */
   1375 		fdc->sc_state = SEEKCOMPLETE;
   1376 		break;
   1377 
   1378 	case 1: case 2: case 3:
   1379 		/* didn't work; try recalibrating */
   1380 		fdc->sc_state = DORECAL;
   1381 		break;
   1382 
   1383 	case 4:
   1384 		/* still no go; reset the bastard */
   1385 		fdc->sc_state = DORESET;
   1386 		break;
   1387 
   1388 	default:
   1389 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1390 			fd->sc_skip, (struct disklabel *)NULL);
   1391 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
   1392 						    NE7_ST0BITS, bits,
   1393 						    sizeof(bits)));
   1394 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
   1395 						   NE7_ST1BITS, bits,
   1396 						   sizeof(bits)));
   1397 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
   1398 						   NE7_ST2BITS, bits,
   1399 						   sizeof(bits)));
   1400 		printf(" cyl %d head %d sec %d)\n",
   1401 		       fdc->sc_status[3],
   1402 		       fdc->sc_status[4],
   1403 		       fdc->sc_status[5]);
   1404 
   1405 		bp->b_flags |= B_ERROR;
   1406 		bp->b_error = EIO;
   1407 		fdfinish(fd, bp);
   1408 	}
   1409 	fdc->sc_errors++;
   1410 }
   1411 
   1412 int
   1413 fdsize(dev)
   1414 	dev_t dev;
   1415 {
   1416 
   1417 	/* Swapping to floppies would not make sense. */
   1418 	return -1;
   1419 }
   1420 
   1421 int
   1422 fddump(dev, blkno, va, size)
   1423 	dev_t dev;
   1424 	daddr_t blkno;
   1425 	caddr_t va;
   1426 	size_t size;
   1427 {
   1428 
   1429 	/* Not implemented. */
   1430 	return ENXIO;
   1431 }
   1432 
   1433 int
   1434 fdioctl(dev, cmd, addr, flag, p)
   1435 	dev_t dev;
   1436 	u_long cmd;
   1437 	caddr_t addr;
   1438 	int flag;
   1439 	struct proc *p;
   1440 {
   1441 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   1442 	int unit = FDUNIT(dev);
   1443 	struct disklabel buffer;
   1444 	int error;
   1445 
   1446 	DPRINTF(("fdioctl:\n"));
   1447 	switch (cmd) {
   1448 	case DIOCGDINFO:
   1449 #if 1
   1450 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
   1451 		return(0);
   1452 #else
   1453 		bzero(&buffer, sizeof(buffer));
   1454 
   1455 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1456 		buffer.d_type = DTYPE_FLOPPY;
   1457 		buffer.d_secsize = 128 << fd->sc_type->secsize;
   1458 
   1459 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1460 			return EINVAL;
   1461 
   1462 		*(struct disklabel *)addr = buffer;
   1463 		return 0;
   1464 #endif
   1465 
   1466 	case DIOCGPART:
   1467 		((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
   1468 		((struct partinfo *)addr)->part =
   1469 		    &fd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
   1470 		return(0);
   1471 
   1472 	case DIOCWLABEL:
   1473 		if ((flag & FWRITE) == 0)
   1474 			return EBADF;
   1475 		/* XXX do something */
   1476 		return 0;
   1477 
   1478 	case DIOCWDINFO:
   1479 		if ((flag & FWRITE) == 0)
   1480 			return EBADF;
   1481 
   1482 		error = setdisklabel(&buffer, (struct disklabel *)addr, 0, NULL);
   1483 		if (error)
   1484 			return error;
   1485 
   1486 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1487 		return error;
   1488 
   1489 	case DIOCLOCK:
   1490 		/*
   1491 		 * Nothing to do here, really.
   1492 		 */
   1493 		return 0; /* XXX */
   1494 
   1495 	case DIOCEJECT:
   1496 		fd_do_eject(unit);
   1497 		return 0;
   1498 
   1499 	default:
   1500 		return ENOTTY;
   1501 	}
   1502 
   1503 #ifdef DIAGNOSTIC
   1504 	panic("fdioctl: impossible");
   1505 #endif
   1506 }
   1507 
   1508 void
   1509 fd_do_eject(unit)
   1510 	int unit;
   1511 {
   1512 	infdc.drvstat = 0x20 | ( 1 << unit);
   1513 	DELAY(1); /* XXX */
   1514 	infdc.drvstat = 0x20;
   1515 }
   1516 
   1517 /*
   1518  * Build disk label. For now we only create a label from what we know
   1519  * from 'sc'.
   1520  */
   1521 static int
   1522 fdgetdisklabel(sc, dev)
   1523 	struct fd_softc *sc;
   1524 	dev_t dev;
   1525 {
   1526 	struct disklabel *lp;
   1527 	int part;
   1528 
   1529 #ifdef FDDEBUG
   1530 	printf("fdgetdisklabel()\n");
   1531 #endif
   1532 
   1533 	part = DISKPART(dev);
   1534 	lp = sc->sc_dk.dk_label;
   1535 	bzero(lp, sizeof(struct disklabel));
   1536 
   1537 	lp->d_secsize     = 128 << sc->sc_type->secsize;
   1538 	lp->d_ntracks     = sc->sc_type->heads;
   1539 	lp->d_nsectors    = sc->sc_type->sectrac;
   1540 	lp->d_secpercyl   = lp->d_ntracks * lp->d_nsectors;
   1541 	lp->d_ncylinders  = sc->sc_type->size / lp->d_secpercyl;
   1542 	lp->d_secperunit  = sc->sc_type->size;
   1543 
   1544 	lp->d_type        = DTYPE_FLOPPY;
   1545 	lp->d_rpm         = 300; 	/* XXX */
   1546 	lp->d_interleave  = 1;		/* FIXME: is this OK?		*/
   1547 	lp->d_bbsize      = 0;
   1548 	lp->d_sbsize      = 0;
   1549 	lp->d_npartitions = part + 1;
   1550 #define STEP_DELAY	6000	/* 6ms (6000us) delay after stepping	*/
   1551 	lp->d_trkseek     = STEP_DELAY; /* XXX */
   1552 	lp->d_magic       = DISKMAGIC;
   1553 	lp->d_magic2      = DISKMAGIC;
   1554 	lp->d_checksum    = dkcksum(lp);
   1555 	lp->d_partitions[part].p_size   = lp->d_secperunit;
   1556 	lp->d_partitions[part].p_fstype = FS_UNUSED;
   1557 	lp->d_partitions[part].p_fsize  = 1024;
   1558 	lp->d_partitions[part].p_frag   = 8;
   1559 
   1560 	return(0);
   1561 }
   1562 
   1563 /*
   1564  * Mountroot hook: prompt the user to enter the root file system
   1565  * floppy.
   1566  */
   1567 void
   1568 fd_mountroot_hook(dev)
   1569 	struct device *dev;
   1570 {
   1571 	int c;
   1572 
   1573 	fd_do_eject(dev->dv_unit);
   1574 	printf("Insert filesystem floppy and press return.");
   1575 	for (;;) {
   1576 		c = cngetc();
   1577 		if ((c == '\r') || (c == '\n')) {
   1578 			printf("\n");
   1579 			break;
   1580 		}
   1581 	}
   1582 }
   1583