Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.13
      1 /*	$NetBSD: fd.c,v 1.13 1995/10/09 22:33:07 pk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
      5  * Copyright (c) 1995 Paul Kranenburg.
      6  * Copyright (c) 1990 The Regents of the University of California.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * Don Ahn.
     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  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/conf.h>
     47 #include <sys/file.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/device.h>
     50 #include <sys/disklabel.h>
     51 #include <sys/dkstat.h>
     52 #include <sys/disk.h>
     53 #include <sys/buf.h>
     54 #include <sys/uio.h>
     55 #include <sys/syslog.h>
     56 #include <sys/queue.h>
     57 
     58 #include <machine/cpu.h>
     59 #include <machine/autoconf.h>
     60 #include <sparc/sparc/auxreg.h>
     61 #include <sparc/dev/fdreg.h>
     62 #include <sparc/dev/fdvar.h>
     63 
     64 #define FDUNIT(dev)	(minor(dev) / 8)
     65 #define FDTYPE(dev)	(minor(dev) % 8)
     66 
     67 #define b_cylin b_resid
     68 
     69 #define FD_DEBUG
     70 #ifdef FD_DEBUG
     71 int	fdc_debug = 0;
     72 #endif
     73 
     74 enum fdc_state {
     75 	DEVIDLE = 0,
     76 	MOTORWAIT,
     77 	DOSEEK,
     78 	SEEKWAIT,
     79 	SEEKTIMEDOUT,
     80 	SEEKCOMPLETE,
     81 	DOIO,
     82 	IOCOMPLETE,
     83 	IOTIMEDOUT,
     84 	DORESET,
     85 	RESETCOMPLETE,
     86 	RESETTIMEDOUT,
     87 	DORECAL,
     88 	RECALWAIT,
     89 	RECALTIMEDOUT,
     90 	RECALCOMPLETE,
     91 };
     92 
     93 /* software state, per controller */
     94 struct fdc_softc {
     95 	struct dkdevice sc_dk;		/* boilerplate */
     96 	struct intrhand sc_sih;
     97 	struct intrhand sc_hih;
     98 	caddr_t		sc_reg;
     99 	struct fd_softc *sc_fd[4];	/* pointers to children */
    100 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    101 	enum fdc_state	sc_state;
    102 	int		sc_flags;
    103 #define FDC_82077		0x01
    104 #define FDC_NEEDHEADSETTLE	0x02
    105 #define FDC_EIS			0x04
    106 	int		sc_errors;		/* number of retries so far */
    107 	int		sc_overruns;		/* number of DMA overruns */
    108 	int		sc_cfg;			/* current configuration */
    109 	struct fdcio	sc_io;
    110 #define sc_reg_msr	sc_io.fdcio_reg_msr
    111 #define sc_reg_fifo	sc_io.fdcio_reg_fifo
    112 #define sc_reg_dor	sc_io.fdcio_reg_dor
    113 #define sc_reg_drs	sc_io.fdcio_reg_msr
    114 #define sc_istate	sc_io.fdcio_istate
    115 #define sc_data		sc_io.fdcio_data
    116 #define sc_tc		sc_io.fdcio_tc
    117 #define sc_nstat	sc_io.fdcio_nstat
    118 #define sc_status	sc_io.fdcio_status
    119 #define sc_intrcnt	sc_io.fdcio_intrcnt
    120 };
    121 
    122 #ifndef FDC_C_HANDLER
    123 extern	struct fdcio	*fdciop;
    124 #endif
    125 
    126 /* controller driver configuration */
    127 int	fdcmatch __P((struct device *, void *, void *));
    128 void	fdcattach __P((struct device *, struct device *, void *));
    129 
    130 struct cfdriver fdccd = {
    131 	NULL, "fdc", fdcmatch, fdcattach, DV_DULL, sizeof(struct fdc_softc)
    132 };
    133 
    134 /*
    135  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    136  * we tell them apart.
    137  */
    138 struct fd_type {
    139 	int	sectrac;	/* sectors per track */
    140 	int	heads;		/* number of heads */
    141 	int	seccyl;		/* sectors per cylinder */
    142 	int	secsize;	/* size code for sectors */
    143 	int	datalen;	/* data len when secsize = 0 */
    144 	int	steprate;	/* step rate and head unload time */
    145 	int	gap1;		/* gap len between sectors */
    146 	int	gap2;		/* formatting gap */
    147 	int	tracks;		/* total num of tracks */
    148 	int	size;		/* size of disk in sectors */
    149 	int	step;		/* steps per cylinder */
    150 	int	rate;		/* transfer speed code */
    151 	char	*name;
    152 };
    153 
    154 /* The order of entries in the following table is important -- BEWARE! */
    155 struct fd_type fd_types[] = {
    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 dkdevice sc_dk;
    168 
    169 	struct fd_type *sc_deftype;	/* default type descriptor */
    170 	struct fd_type *sc_type;	/* current type descriptor */
    171 
    172 	daddr_t	sc_blkno;	/* starting block number */
    173 	int sc_bcount;		/* byte count left */
    174 	int sc_skip;		/* bytes already transferred */
    175 	int sc_nblks;		/* number of blocks currently tranferring */
    176 	int sc_nbytes;		/* number of bytes currently tranferring */
    177 
    178 	int sc_drive;		/* physical unit number */
    179 	int sc_flags;
    180 #define	FD_OPEN		0x01		/* it's open */
    181 #define	FD_MOTOR	0x02		/* motor should be on */
    182 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    183 	int sc_cylin;		/* where we think the head is */
    184 
    185 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    186 	int sc_ops;		/* I/O ops since last switch */
    187 	struct buf sc_q;	/* head of buf chain */
    188 };
    189 
    190 /* floppy driver configuration */
    191 int	fdmatch __P((struct device *, void *, void *));
    192 void	fdattach __P((struct device *, struct device *, void *));
    193 
    194 struct cfdriver fdcd = {
    195 	NULL, "fd", fdmatch, fdattach, DV_DISK, sizeof(struct fd_softc)
    196 };
    197 
    198 void fdgetdisklabel __P((struct fd_softc *));
    199 int fd_get_parms __P((struct fd_softc *));
    200 void fdstrategy __P((struct buf *));
    201 void fdstart __P((struct fd_softc *));
    202 
    203 struct dkdriver fddkdriver = { fdstrategy };
    204 
    205 struct	fd_type *fd_nvtotype __P((char *, int, int));
    206 void	fd_set_motor __P((struct fdc_softc *fdc));
    207 void	fd_motor_off __P((void *arg));
    208 void	fd_motor_on __P((void *arg));
    209 int	fdcresult __P((struct fdc_softc *fdc));
    210 int	out_fdc __P((struct fdc_softc *fdc, u_char x));
    211 void	fdcstart __P((struct fdc_softc *fdc));
    212 void	fdcstatus __P((struct device *dv, int n, char *s));
    213 void	fdc_reset __P((struct fdc_softc *fdc));
    214 void	fdctimeout __P((void *arg));
    215 void	fdcpseudointr __P((void *arg));
    216 #ifdef FDC_C_HANDLER
    217 int	fdchwintr __P((struct fdc_softc *));
    218 #else
    219 void	fdchwintr __P((void));
    220 #endif
    221 int	fdcswintr __P((struct fdc_softc *));
    222 void	fdcretry __P((struct fdc_softc *fdc));
    223 void	fdfinish __P((struct fd_softc *fd, struct buf *bp));
    224 
    225 #if PIL_FDSOFT == 4
    226 #define IE_FDSOFT	IE_L4
    227 #else
    228 #error 4
    229 #endif
    230 
    231 #define OBP_FDNAME	(cputyp == CPU_SUN4M ? "SUNW,fdtwo" : "fd")
    232 
    233 int
    234 fdcmatch(parent, match, aux)
    235 	struct device *parent;
    236 	void *match, *aux;
    237 {
    238 	struct cfdata *cf = match;
    239 	register struct confargs *ca = aux;
    240 	register struct romaux *ra = &ca->ca_ra;
    241 
    242 	/* Sun PROMs call the controller an "fd" or "SUNW,fdtwo" */
    243 	if (strcmp(OBP_FDNAME, ra->ra_name))
    244 		return (0);
    245 	if (ca->ca_bustype == BUS_MAIN) {
    246 		if (ca->ca_ra.ra_vaddr &&
    247 		    probeget(ca->ca_ra.ra_vaddr, 1) == -1) {
    248 			return (0);
    249 		}
    250 		return (1);
    251 	}
    252 
    253 	return (0);
    254 }
    255 
    256 /*
    257  * Arguments passed between fdcattach and fdprobe.
    258  */
    259 struct fdc_attach_args {
    260 	int fa_drive;
    261 	int fa_bootdev;
    262 	struct fd_type *fa_deftype;
    263 };
    264 
    265 /*
    266  * Print the location of a disk drive (called just before attaching the
    267  * the drive).  If `fdc' is not NULL, the drive was found but was not
    268  * in the system config file; print the drive name as well.
    269  * Return QUIET (config_find ignores this if the device was configured) to
    270  * avoid printing `fdN not configured' messages.
    271  */
    272 int
    273 fdprint(aux, fdc)
    274 	void *aux;
    275 	char *fdc;
    276 {
    277 	register struct fdc_attach_args *fa = aux;
    278 
    279 	if (!fdc)
    280 		printf(" drive %d", fa->fa_drive);
    281 	return QUIET;
    282 }
    283 
    284 static void
    285 fdconf(fdc)
    286 	struct fdc_softc *fdc;
    287 {
    288 	int	vroom;
    289 
    290 	if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
    291 		return;
    292 
    293 	/*
    294 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
    295 	 * the PROM thinks is appropriate.
    296 	 */
    297 	if ((vroom = fdc->sc_status[7]) == 0)
    298 		vroom = 0x64;
    299 
    300 	/* Configure controller to use FIFO and Implied Seek */
    301 	out_fdc(fdc, NE7CMD_CFG);
    302 	out_fdc(fdc, vroom);
    303 	out_fdc(fdc, fdc->sc_cfg);
    304 	out_fdc(fdc, 0); /* PRETRK */
    305 	/* No result phase */
    306 }
    307 
    308 void
    309 fdcattach(parent, self, aux)
    310 	struct device *parent, *self;
    311 	void *aux;
    312 {
    313 	register struct confargs *ca = aux;
    314 	struct fdc_softc *fdc = (void *)self;
    315 	struct fdc_attach_args fa;
    316 	int n, pri;
    317 	char code;
    318 
    319 	if (ca->ca_ra.ra_vaddr)
    320 		fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr;
    321 	else
    322 		fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_paddr,
    323 						ca->ca_ra.ra_len,
    324 						ca->ca_bustype);
    325 
    326 	fdc->sc_state = DEVIDLE;
    327 	fdc->sc_istate = ISTATE_IDLE;
    328 	fdc->sc_flags |= FDC_EIS;
    329 	TAILQ_INIT(&fdc->sc_drives);
    330 
    331 	pri = ca->ca_ra.ra_intr[0].int_pri;
    332 #ifdef FDC_C_HANDLER
    333 	fdc->sc_hih.ih_fun = (void *)fdchwintr;
    334 	fdc->sc_hih.ih_arg = fdc;
    335 	intr_establish(pri, &fdc->sc_hih);
    336 #else
    337 	fdciop = &fdc->sc_io;
    338 	intr_fasttrap(pri, fdchwintr);
    339 #endif
    340 	fdc->sc_sih.ih_fun = (void *)fdcswintr;
    341 	fdc->sc_sih.ih_arg = fdc;
    342 	intr_establish(PIL_FDSOFT, &fdc->sc_sih);
    343 
    344 	/* Assume a 82077 */
    345 	fdc->sc_reg_msr = &((struct fdreg_77 *)fdc->sc_reg)->fd_msr;
    346 	fdc->sc_reg_fifo = &((struct fdreg_77 *)fdc->sc_reg)->fd_fifo;
    347 	fdc->sc_reg_dor = &((struct fdreg_77 *)fdc->sc_reg)->fd_dor;
    348 
    349 	code = '7';
    350 	if (*fdc->sc_reg_dor == NE7_RQM) {
    351 		/*
    352 		 * This hack from Chris Torek: apparently DOR really
    353 		 * addresses MSR/DRS on a 82072.
    354 		 * We used to rely on the VERSION command to tell the
    355 		 * difference (which did not work).
    356 		 */
    357 		*fdc->sc_reg_dor = FDC_250KBPS;
    358 		if (*fdc->sc_reg_dor == NE7_RQM)
    359 			code = '2';
    360 	}
    361 	if (code == '7') {
    362 		fdc->sc_flags |= FDC_82077;
    363 	} else {
    364 		fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr;
    365 		fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo;
    366 		fdc->sc_reg_dor = 0;
    367 	}
    368 
    369 #ifdef FD_DEBUG
    370 	if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
    371 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
    372 		if (fdc_debug)
    373 			printf("[version cmd]");
    374 	}
    375 #endif
    376 
    377 	/*
    378 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
    379 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
    380 	 */
    381 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
    382 	fdconf(fdc);
    383 
    384 	if (fdc->sc_flags & FDC_82077) {
    385 		/* Lock configuration across soft resets. */
    386 		out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
    387 		if (fdcresult(fdc) != 1)
    388 			printf(" CFGLOCK: unexpected response");
    389 	}
    390 
    391 	evcnt_attach(&fdc->sc_dk.dk_dev, "intr", &fdc->sc_intrcnt);
    392 
    393 	printf(" pri %d, softpri %d: chip 8207%c\n", pri, PIL_FDSOFT, code);
    394 
    395 	/*
    396 	 * Controller and drives are represented by one and the same
    397 	 * Openprom node, so we can as well check for the floppy boots here.
    398 	 */
    399 	if (ca->ca_ra.ra_bp &&
    400 	    strcmp(ca->ca_ra.ra_bp->name, OBP_FDNAME) == 0 &&
    401 	    ca->ca_ra.ra_bp->val[0] == 0 &&
    402 	    ca->ca_ra.ra_bp->val[1] == 0)
    403 		fa.fa_bootdev = 1;
    404 	else
    405 		fa.fa_bootdev = 0;
    406 
    407 	/* physical limit: four drives per controller. */
    408 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    409 		fa.fa_deftype = NULL;		/* unknown */
    410 	fa.fa_deftype = &fd_types[0];		/* XXX */
    411 		(void)config_found(self, (void *)&fa, fdprint);
    412 	}
    413 }
    414 
    415 int
    416 fdmatch(parent, match, aux)
    417 	struct device *parent;
    418 	void *match, *aux;
    419 {
    420 	struct fdc_softc *fdc = (void *)parent;
    421 	struct cfdata *cf = match;
    422 	struct fdc_attach_args *fa = aux;
    423 	int drive = fa->fa_drive;
    424 	int n;
    425 
    426 	if (drive > 0)
    427 		/* XXX - for now, punt > 1 drives */
    428 		return 0;
    429 
    430 	if (fdc->sc_flags & FDC_82077) {
    431 		/* select drive and turn on motor */
    432 		*fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
    433 		/* wait for motor to spin up */
    434 		delay(250000);
    435 	} else {
    436 		auxregbisc(AUXIO_FDS, 0);
    437 	}
    438 	fdc->sc_nstat = 0;
    439 	out_fdc(fdc, NE7CMD_RECAL);
    440 	out_fdc(fdc, drive);
    441 	/* wait for recalibrate */
    442 	for (n = 0; n < 100000; n++) {
    443 		delay(10);
    444 		if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
    445 			/* wait a bit longer till device *really* is ready */
    446 			delay(100000);
    447 			if (out_fdc(fdc, NE7CMD_SENSEI))
    448 				break;
    449 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
    450 				/*
    451 				 * Got `invalid command'; we interpret it
    452 				 * to mean that the re-calibrate hasn't in
    453 				 * fact finished yet
    454 				 */
    455 				continue;
    456 			break;
    457 		}
    458 	}
    459 	n = fdc->sc_nstat;
    460 #ifdef FD_DEBUG
    461 	if (fdc_debug) {
    462 		int i;
    463 		printf("fdprobe: %d stati:", n);
    464 		for (i = 0; i < n; i++)
    465 			printf(" %x", fdc->sc_status[i]);
    466 		printf("\n");
    467 	}
    468 #endif
    469 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    470 		return 0;
    471 	/* turn off motor */
    472 	if (fdc->sc_flags & FDC_82077) {
    473 		/* select drive and turn on motor */
    474 		*fdc->sc_reg_dor = FDO_FRST;
    475 	} else {
    476 		auxregbisc(0, AUXIO_FDS);
    477 	}
    478 
    479 	return 1;
    480 }
    481 
    482 /*
    483  * Controller is working, and drive responded.  Attach it.
    484  */
    485 void
    486 fdattach(parent, self, aux)
    487 	struct device *parent, *self;
    488 	void *aux;
    489 {
    490 	struct fdc_softc *fdc = (void *)parent;
    491 	struct fd_softc *fd = (void *)self;
    492 	struct fdc_attach_args *fa = aux;
    493 	struct fd_type *type = fa->fa_deftype;
    494 	int drive = fa->fa_drive;
    495 
    496 	/* XXX Allow `flags' to override device type? */
    497 
    498 	if (type)
    499 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    500 		    type->tracks, type->heads, type->sectrac);
    501 	else
    502 		printf(": density unknown\n");
    503 
    504 	fd->sc_cylin = -1;
    505 	fd->sc_drive = drive;
    506 	fd->sc_deftype = type;
    507 	fdc->sc_fd[drive] = fd;
    508 	fd->sc_dk.dk_driver = &fddkdriver;
    509 
    510 	/*
    511 	 * We're told if we're the boot device in fdcattach().
    512 	 */
    513 	if (fa->fa_bootdev)
    514 		bootdv = &fd->sc_dk.dk_dev;
    515 
    516 	/* XXX Need to do some more fiddling with sc_dk. */
    517 	dk_establish(&fd->sc_dk, &fd->sc_dk.dk_dev);
    518 }
    519 
    520 inline struct fd_type *
    521 fd_dev_to_type(fd, dev)
    522 	struct fd_softc *fd;
    523 	dev_t dev;
    524 {
    525 	int type = FDTYPE(dev);
    526 
    527 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    528 		return NULL;
    529 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    530 }
    531 
    532 void
    533 fdstrategy(bp)
    534 	register struct buf *bp;	/* IO operation to perform */
    535 {
    536 	struct fd_softc *fd;
    537 	int unit = FDUNIT(bp->b_dev);
    538 	int sz;
    539  	int s;
    540 
    541 	/* Valid unit, controller, and request? */
    542 	if (unit >= fdcd.cd_ndevs ||
    543 	    (fd = fdcd.cd_devs[unit]) == 0 ||
    544 	    bp->b_blkno < 0 ||
    545 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    546 		bp->b_error = EINVAL;
    547 		goto bad;
    548 	}
    549 
    550 	/* If it's a null transfer, return immediately. */
    551 	if (bp->b_bcount == 0)
    552 		goto done;
    553 
    554 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    555 
    556 	if (bp->b_blkno + sz > fd->sc_type->size) {
    557 		sz = fd->sc_type->size - bp->b_blkno;
    558 		if (sz == 0) {
    559 			/* If exactly at end of disk, return EOF. */
    560 			bp->b_resid = bp->b_bcount;
    561 			goto done;
    562 		}
    563 		if (sz < 0) {
    564 			/* If past end of disk, return EINVAL. */
    565 			bp->b_error = EINVAL;
    566 			goto bad;
    567 		}
    568 		/* Otherwise, truncate request. */
    569 		bp->b_bcount = sz << DEV_BSHIFT;
    570 	}
    571 
    572  	bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    573 
    574 #ifdef FD_DEBUG
    575 	if (fdc_debug > 1)
    576 		printf("fdstrategy: b_blkno %d b_bcount %d blkno %d cylin %d\n",
    577 		    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
    578 #endif
    579 
    580 	/* Queue transfer on drive, activate drive and controller if idle. */
    581 	s = splbio();
    582 	disksort(&fd->sc_q, bp);
    583 	untimeout(fd_motor_off, fd); /* a good idea */
    584 	if (!fd->sc_q.b_active)
    585 		fdstart(fd);
    586 #ifdef DIAGNOSTIC
    587 	else {
    588 		struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    589 		if (fdc->sc_state == DEVIDLE) {
    590 			printf("fdstrategy: controller inactive\n");
    591 			fdcstart(fdc);
    592 		}
    593 	}
    594 #endif
    595 	splx(s);
    596 	return;
    597 
    598 bad:
    599 	bp->b_flags |= B_ERROR;
    600 done:
    601 	/* Toss transfer; we're done early. */
    602 	biodone(bp);
    603 }
    604 
    605 void
    606 fdstart(fd)
    607 	struct fd_softc *fd;
    608 {
    609 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    610 	int active = fdc->sc_drives.tqh_first != 0;
    611 
    612 	/* Link into controller queue. */
    613 	fd->sc_q.b_active = 1;
    614 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    615 
    616 	/* If controller not already active, start it. */
    617 	if (!active)
    618 		fdcstart(fdc);
    619 }
    620 
    621 void
    622 fdfinish(fd, bp)
    623 	struct fd_softc *fd;
    624 	struct buf *bp;
    625 {
    626 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    627 
    628 	/*
    629 	 * Move this drive to the end of the queue to give others a `fair'
    630 	 * chance.  We only force a switch if N operations are completed while
    631 	 * another drive is waiting to be serviced, since there is a long motor
    632 	 * startup delay whenever we switch.
    633 	 */
    634 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    635 		fd->sc_ops = 0;
    636 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    637 		if (bp->b_actf) {
    638 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    639 		} else
    640 			fd->sc_q.b_active = 0;
    641 	}
    642 	bp->b_resid = fd->sc_bcount;
    643 	fd->sc_skip = 0;
    644 	fd->sc_q.b_actf = bp->b_actf;
    645 	biodone(bp);
    646 	/* turn off motor 5s from now */
    647 	timeout(fd_motor_off, fd, 5 * hz);
    648 	fdc->sc_state = DEVIDLE;
    649 }
    650 
    651 void
    652 fdc_reset(fdc)
    653 	struct fdc_softc *fdc;
    654 {
    655 	if (fdc->sc_flags & FDC_82077) {
    656 		*fdc->sc_reg_dor = FDO_MOEN(0);
    657 	}
    658 
    659 	*fdc->sc_reg_drs = DRS_RESET;
    660 	delay(10);
    661 	*fdc->sc_reg_drs = 0;
    662 #ifdef FD_DEBUG
    663 	if (fdc_debug)
    664 		printf("fdc reset\n");
    665 #endif
    666 }
    667 
    668 void
    669 fd_set_motor(fdc)
    670 	struct fdc_softc *fdc;
    671 {
    672 	struct fd_softc *fd;
    673 	u_char status;
    674 	int n;
    675 
    676 	if (fdc->sc_flags & FDC_82077) {
    677 		status = FDO_FRST | FDO_FDMAEN;
    678 		if (fd = fdc->sc_drives.tqh_first)
    679 			status |= fd->sc_drive;
    680 
    681 		for (n = 0; n < 4; n++)
    682 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    683 				status |= FDO_MOEN(n);
    684 		*fdc->sc_reg_dor = status;
    685 	} else {
    686 		int on = 0;
    687 
    688 		for (n = 0; n < 4; n++)
    689 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    690 				on = 1;
    691 		if (on) {
    692 			auxregbisc(AUXIO_FDS, 0);
    693 		} else {
    694 			auxregbisc(0, AUXIO_FDS);
    695 		}
    696 	}
    697 }
    698 
    699 void
    700 fd_motor_off(arg)
    701 	void *arg;
    702 {
    703 	struct fd_softc *fd = arg;
    704 	int s;
    705 
    706 	s = splbio();
    707 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    708 	fd_set_motor((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent);
    709 	splx(s);
    710 }
    711 
    712 void
    713 fd_motor_on(arg)
    714 	void *arg;
    715 {
    716 	struct fd_softc *fd = arg;
    717 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    718 	int s;
    719 
    720 	s = splbio();
    721 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    722 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    723 		(void) fdcswintr(fdc);
    724 	splx(s);
    725 }
    726 
    727 int
    728 fdcresult(fdc)
    729 	struct fdc_softc *fdc;
    730 {
    731 	u_char i;
    732 	int j = 100000,
    733 	    n = 0;
    734 
    735 	for (; j; j--) {
    736 		i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
    737 		if (i == NE7_RQM)
    738 			return (fdc->sc_nstat = n);
    739 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    740 			if (n >= sizeof(fdc->sc_status)) {
    741 				log(LOG_ERR, "fdcresult: overrun\n");
    742 				return -1;
    743 			}
    744 			fdc->sc_status[n++] = *fdc->sc_reg_fifo;
    745 		}
    746 	}
    747 	log(LOG_ERR, "fdcresult: timeout\n");
    748 	return (fdc->sc_nstat = -1);
    749 }
    750 
    751 int
    752 out_fdc(fdc, x)
    753 	struct fdc_softc *fdc;
    754 	u_char x;
    755 {
    756 	int i = 100000;
    757 
    758 	while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0);
    759 	if (i <= 0)
    760 		return -1;
    761 
    762 	*fdc->sc_reg_fifo = x;
    763 	return 0;
    764 }
    765 
    766 int
    767 Fdopen(dev, flags)
    768 	dev_t dev;
    769 	int flags;
    770 {
    771  	int unit;
    772 	struct fd_softc *fd;
    773 	struct fd_type *type;
    774 
    775 	unit = FDUNIT(dev);
    776 	if (unit >= fdcd.cd_ndevs)
    777 		return ENXIO;
    778 	fd = fdcd.cd_devs[unit];
    779 	if (fd == 0)
    780 		return ENXIO;
    781 	type = fd_dev_to_type(fd, dev);
    782 	if (type == NULL)
    783 		return ENXIO;
    784 
    785 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    786 	    fd->sc_type != type)
    787 		return EBUSY;
    788 
    789 	fd->sc_type = type;
    790 	fd->sc_cylin = -1;
    791 	fd->sc_flags |= FD_OPEN;
    792 
    793 	return 0;
    794 }
    795 
    796 int
    797 fdclose(dev, flags)
    798 	dev_t dev;
    799 	int flags;
    800 {
    801 	struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
    802 
    803 	fd->sc_flags &= ~FD_OPEN;
    804 	return 0;
    805 }
    806 
    807 int
    808 fdread(dev, uio)
    809         dev_t dev;
    810         struct uio *uio;
    811 {
    812 
    813         return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    814 }
    815 
    816 int
    817 fdwrite(dev, uio)
    818         dev_t dev;
    819         struct uio *uio;
    820 {
    821 
    822         return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    823 }
    824 
    825 void
    826 fdcstart(fdc)
    827 	struct fdc_softc *fdc;
    828 {
    829 
    830 #ifdef DIAGNOSTIC
    831 	/* only got here if controller's drive queue was inactive; should
    832 	   be in idle state */
    833 	if (fdc->sc_state != DEVIDLE) {
    834 		printf("fdcstart: not idle\n");
    835 		return;
    836 	}
    837 #endif
    838 	(void) fdcswintr(fdc);
    839 }
    840 
    841 void
    842 fdcstatus(dv, n, s)
    843 	struct device *dv;
    844 	int n;
    845 	char *s;
    846 {
    847 	struct fdc_softc *fdc = (void *)dv->dv_parent;
    848 
    849 #if 0
    850 	/*
    851 	 * A 82072 seems to return <invalid command> on
    852 	 * gratuitous Sense Interrupt commands.
    853 	 */
    854 	if (n == 0 && (fdc->sc_flags & FDC_82077)) {
    855 		out_fdc(fdc, NE7CMD_SENSEI);
    856 		(void) fdcresult(fdc);
    857 		n = 2;
    858 	}
    859 #endif
    860 
    861 	/* Just print last status */
    862 	n = fdc->sc_nstat;
    863 
    864 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
    865 
    866 	switch (n) {
    867 	case 0:
    868 		printf("\n");
    869 		break;
    870 	case 2:
    871 		printf(" (st0 %b cyl %d)\n",
    872 		    fdc->sc_status[0], NE7_ST0BITS,
    873 		    fdc->sc_status[1]);
    874 		break;
    875 	case 7:
    876 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
    877 		    fdc->sc_status[0], NE7_ST0BITS,
    878 		    fdc->sc_status[1], NE7_ST1BITS,
    879 		    fdc->sc_status[2], NE7_ST2BITS,
    880 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    881 		break;
    882 #ifdef DIAGNOSTIC
    883 	default:
    884 		printf(" fdcstatus: weird size: %d\n", n);
    885 		break;
    886 #endif
    887 	}
    888 }
    889 
    890 void
    891 fdctimeout(arg)
    892 	void *arg;
    893 {
    894 	struct fdc_softc *fdc = arg;
    895 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
    896 	int s;
    897 
    898 	s = splbio();
    899 	fdcstatus(&fd->sc_dk.dk_dev, 0, "timeout");
    900 
    901 	if (fd->sc_q.b_actf)
    902 		fdc->sc_state++;
    903 	else
    904 		fdc->sc_state = DEVIDLE;
    905 
    906 	(void) fdcswintr(fdc);
    907 	splx(s);
    908 }
    909 
    910 void
    911 fdcpseudointr(arg)
    912 	void *arg;
    913 {
    914 	struct fdc_softc *fdc = arg;
    915 	int s;
    916 
    917 	/* Just ensure it has the right spl. */
    918 	s = splbio();
    919 	(void) fdcswintr(fdc);
    920 	splx(s);
    921 }
    922 
    923 
    924 #ifdef FDC_C_HANDLER
    925 /*
    926  * hardware interrupt entry point: must be converted to `fast'
    927  * (in-window) handler.
    928  */
    929 int
    930 fdchwintr(fdc)
    931 	struct fdc_softc *fdc;
    932 {
    933 	struct buf *bp;
    934 	int read;
    935 
    936 	switch (fdc->sc_istate) {
    937 	case ISTATE_SENSEI:
    938 		out_fdc(fdc, NE7CMD_SENSEI);
    939 		fdcresult(fdc);
    940 		fdc->sc_istate = ISTATE_IDLE;
    941 		ienab_bis(IE_FDSOFT);
    942 		return 1;
    943 	case ISTATE_IDLE:
    944 	case ISTATE_SPURIOUS:
    945 		auxregbisc(0, AUXIO_FDS);	/* Does this help? */
    946 		fdcresult(fdc);
    947 		fdc->sc_istate = ISTATE_SPURIOUS;
    948 		printf("fdc: stray hard interrupt... ");
    949 		ienab_bis(IE_FDSOFT);
    950 		return 1;
    951 	case ISTATE_DMA:
    952 		break;
    953 	default:
    954 		printf("fdc: goofed ...\n");
    955 		return 1;
    956 	}
    957 
    958 	read = bp->b_flags & B_READ;
    959 	for (;;) {
    960 		register int msr;
    961 
    962 		msr = *fdc->sc_reg_msr;
    963 
    964 		if ((msr & NE7_RQM) == 0)
    965 			break;
    966 
    967 		if ((msr & NE7_NDM) == 0) {
    968 			fdcresult(fdc);
    969 			fdc->sc_istate = ISTATE_IDLE;
    970 			ienab_bis(IE_FDSOFT);
    971 			printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
    972 			break;
    973 		}
    974 
    975 		if (msr & NE7_DIO) {
    976 #ifdef DIAGNOSTIC
    977 			if (!read)
    978 				printf("fdxfer: false read\n");
    979 #endif
    980 			*fdc->sc_data++ = *fdc->sc_reg_fifo;
    981 		} else {
    982 #ifdef DIAGNOSTIC
    983 			if (read)
    984 				printf("fdxfer: false write\n");
    985 #endif
    986 			*fdc->sc_reg_fifo = *fdc->sc_data++;
    987 		}
    988 		if (--fdc->sc_tc == 0) {
    989 			auxregbisc(AUXIO_FTC, 0);
    990 			fdc->sc_istate = ISTATE_IDLE;
    991 			delay(10);
    992 			auxregbisc(0, AUXIO_FTC);
    993 			fdcresult(fdc);
    994 			ienab_bis(IE_FDSOFT);
    995 			break;
    996 		}
    997 	}
    998 	return 1;
    999 }
   1000 #endif
   1001 
   1002 int
   1003 fdcswintr(fdc)
   1004 	struct fdc_softc *fdc;
   1005 {
   1006 #define	st0	fdc->sc_status[0]
   1007 #define	st1	fdc->sc_status[1]
   1008 #define	cyl	fdc->sc_status[1]
   1009 #define OUT_FDC(fdc, c, s) \
   1010     do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
   1011 
   1012 	struct fd_softc *fd;
   1013 	struct buf *bp;
   1014 	int read, head, trac, sec, i, s, nblks;
   1015 	struct fd_type *type;
   1016 
   1017 
   1018 	if (fdc->sc_istate != ISTATE_IDLE) {
   1019 		/* Trouble... */
   1020 		printf("fdc: spurious interrupt: state %d, istate=%d\n",
   1021 			fdc->sc_state, fdc->sc_istate);
   1022 		fdc->sc_istate = ISTATE_IDLE;
   1023 		if (fdc->sc_state == RESETCOMPLETE ||
   1024 		    fdc->sc_state == RESETTIMEDOUT) {
   1025 			panic("fdcintr: spurious interrupt can't be cleared");
   1026 		}
   1027 		goto doreset;
   1028 	}
   1029 
   1030 loop:
   1031 	/* Is there a drive for the controller to do a transfer with? */
   1032 	fd = fdc->sc_drives.tqh_first;
   1033 	if (fd == NULL) {
   1034 		fdc->sc_state = DEVIDLE;
   1035  		return 0;
   1036 	}
   1037 
   1038 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1039 	bp = fd->sc_q.b_actf;
   1040 	if (bp == NULL) {
   1041 		fd->sc_ops = 0;
   1042 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1043 		fd->sc_q.b_active = 0;
   1044 		goto loop;
   1045 	}
   1046 
   1047 	switch (fdc->sc_state) {
   1048 	case DEVIDLE:
   1049 		fdc->sc_errors = 0;
   1050 		fd->sc_skip = 0;
   1051 		fd->sc_bcount = bp->b_bcount;
   1052 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
   1053 		untimeout(fd_motor_off, fd);
   1054 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1055 			fdc->sc_state = MOTORWAIT;
   1056 			return 1;
   1057 		}
   1058 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1059 			/* Turn on the motor, being careful about pairing. */
   1060 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1061 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1062 				untimeout(fd_motor_off, ofd);
   1063 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1064 			}
   1065 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1066 			fd_set_motor(fdc);
   1067 			fdc->sc_state = MOTORWAIT;
   1068 			if (fdc->sc_flags & FDC_82077) { /* XXX */
   1069 				/* Allow .25s for motor to stabilize. */
   1070 				timeout(fd_motor_on, fd, hz / 4);
   1071 			} else {
   1072 				fd->sc_flags &= ~FD_MOTOR_WAIT;
   1073 				goto loop;
   1074 			}
   1075 			return 1;
   1076 		}
   1077 		/* Make sure the right drive is selected. */
   1078 		fd_set_motor(fdc);
   1079 
   1080 		/* fall through */
   1081 	case DOSEEK:
   1082 	doseek:
   1083 		if (fdc->sc_flags & FDC_EIS) {
   1084 			fd->sc_cylin = bp->b_cylin;
   1085 			/* We use implied seek */
   1086 			goto doio;
   1087 		}
   1088 
   1089 		if (fd->sc_cylin == bp->b_cylin)
   1090 			goto doio;
   1091 
   1092 		/* specify command */
   1093 		OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
   1094 		OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
   1095 		OUT_FDC(fdc, 6, SEEKTIMEDOUT);	/* XXX head load time == 6ms */
   1096 
   1097 		fdc->sc_istate = ISTATE_SENSEI;
   1098 		/* seek function */
   1099 		OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
   1100 		OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
   1101 		OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
   1102 
   1103 		fd->sc_cylin = -1;
   1104 		fdc->sc_state = SEEKWAIT;
   1105 		fdc->sc_nstat = 0;
   1106 		timeout(fdctimeout, fdc, 4 * hz);
   1107 		return 1;
   1108 
   1109 	case DOIO:
   1110 	doio:
   1111 		type = fd->sc_type;
   1112 		sec = fd->sc_blkno % type->seccyl;
   1113 		nblks = type->seccyl - sec;
   1114 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1115 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
   1116 		fd->sc_nblks = nblks;
   1117 		fd->sc_nbytes = nblks * FDC_BSIZE;
   1118 		head = sec / type->sectrac;
   1119 		sec -= head * type->sectrac;
   1120 #ifdef DIAGNOSTIC
   1121 		{int block;
   1122 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
   1123 		 if (block != fd->sc_blkno) {
   1124 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
   1125 #ifdef DDB
   1126 			 Debugger();
   1127 #endif
   1128 		 }}
   1129 #endif
   1130 		read = bp->b_flags & B_READ;
   1131 
   1132 		/* Setup for pseudo DMA */
   1133 		fdc->sc_data = bp->b_data + fd->sc_skip;
   1134 		fdc->sc_tc = fd->sc_nbytes;
   1135 
   1136 		*fdc->sc_reg_drs = type->rate;
   1137 #ifdef FD_DEBUG
   1138 		if (fdc_debug > 1)
   1139 			printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
   1140 				read ? "read" : "write", fd->sc_drive,
   1141 				fd->sc_cylin, head, sec, nblks);
   1142 #endif
   1143 		fdc->sc_state = IOCOMPLETE;
   1144 		fdc->sc_istate = ISTATE_DMA;
   1145 		fdc->sc_nstat = 0;
   1146 		if (read)
   1147 			OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT);	/* READ */
   1148 		else
   1149 			OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT);	/* WRITE */
   1150 		OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
   1151 		OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT);	/* track */
   1152 		OUT_FDC(fdc, head, IOTIMEDOUT);
   1153 		OUT_FDC(fdc, sec + 1, IOTIMEDOUT);	/* sector +1 */
   1154 		OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/* sector size */
   1155 		OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/* sectors/track */
   1156 		OUT_FDC(fdc, type->gap1, IOTIMEDOUT);	/* gap1 size */
   1157 		OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/* data length */
   1158 		/* allow 2 seconds for operation */
   1159 		timeout(fdctimeout, fdc, 2 * hz);
   1160 		return 1;				/* will return later */
   1161 
   1162 	case SEEKWAIT:
   1163 		untimeout(fdctimeout, fdc);
   1164 		fdc->sc_state = SEEKCOMPLETE;
   1165 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1166 			/* allow 1/50 second for heads to settle */
   1167 			timeout(fdcpseudointr, fdc, hz / 50);
   1168 			return 1;		/* will return later */
   1169 		}
   1170 
   1171 	case SEEKCOMPLETE:
   1172 		/* Make sure seek really happened. */
   1173 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1174 		    cyl != bp->b_cylin * fd->sc_type->step) {
   1175 #ifdef FD_DEBUG
   1176 			if (fdc_debug)
   1177 				fdcstatus(&fd->sc_dk.dk_dev, 2, "seek failed");
   1178 #endif
   1179 			fdcretry(fdc);
   1180 			goto loop;
   1181 		}
   1182 		fd->sc_cylin = bp->b_cylin;
   1183 		goto doio;
   1184 
   1185 	case IOTIMEDOUT:
   1186 		auxregbisc(AUXIO_FTC, 0);
   1187 		delay(10);
   1188 		auxregbisc(0, AUXIO_FTC);
   1189 		(void)fdcresult(fdc);
   1190 	case SEEKTIMEDOUT:
   1191 	case RECALTIMEDOUT:
   1192 	case RESETTIMEDOUT:
   1193 		fdcretry(fdc);
   1194 		goto loop;
   1195 
   1196 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1197 		untimeout(fdctimeout, fdc);
   1198 		if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
   1199 #ifdef FD_DEBUG
   1200 			if (fdc_debug) {
   1201 				fdcstatus(&fd->sc_dk.dk_dev, 7,
   1202 					bp->b_flags & B_READ
   1203 					? "read failed" : "write failed");
   1204 				printf("blkno %d nblks %d tc %d\n",
   1205 				       fd->sc_blkno, fd->sc_nblks, fdc->sc_tc);
   1206 			}
   1207 #endif
   1208 			if (fdc->sc_nstat == 7 &&
   1209 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
   1210 
   1211 				/*
   1212 				 * Silently retry overruns if no other
   1213 				 * error bit is set. Adjust threshold.
   1214 				 */
   1215 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1216 				if (thr < 15) {
   1217 					thr++;
   1218 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1219 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1220 #ifdef FD_DEBUG
   1221 					if (fdc_debug)
   1222 						printf("fdc: %d -> threshold\n", thr);
   1223 #endif
   1224 					fdconf(fdc);
   1225 					fdc->sc_state = DOIO;
   1226 					fdc->sc_overruns = 0;
   1227 				}
   1228 				if (++fdc->sc_overruns < 3)
   1229 					goto loop;
   1230 			}
   1231 			fdcretry(fdc);
   1232 			goto loop;
   1233 		}
   1234 		if (fdc->sc_errors) {
   1235 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1236 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1237 			printf("\n");
   1238 			fdc->sc_errors = 0;
   1239 		} else {
   1240 			if (--fdc->sc_overruns < -20) {
   1241 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1242 				if (thr > 0) {
   1243 					thr--;
   1244 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1245 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1246 #ifdef FD_DEBUG
   1247 					if (fdc_debug)
   1248 						printf("fdc: %d -> threshold\n", thr);
   1249 #endif
   1250 					fdconf(fdc);
   1251 				}
   1252 				fdc->sc_overruns = 0;
   1253 			}
   1254 		}
   1255 		fd->sc_blkno += fd->sc_nblks;
   1256 		fd->sc_skip += fd->sc_nbytes;
   1257 		fd->sc_bcount -= fd->sc_nbytes;
   1258 		if (fd->sc_bcount > 0) {
   1259 			bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
   1260 			goto doseek;
   1261 		}
   1262 		fdfinish(fd, bp);
   1263 		goto loop;
   1264 
   1265 	case DORESET:
   1266 	doreset:
   1267 		/* try a reset, keep motor on */
   1268 		fd_set_motor(fdc);
   1269 		delay(100);
   1270 		fdc_reset(fdc);
   1271 		fdc->sc_nstat = 0;
   1272 		fdc->sc_istate = ISTATE_SENSEI;
   1273 		fdc->sc_state = RESETCOMPLETE;
   1274 		timeout(fdctimeout, fdc, hz / 2);
   1275 		return 1;			/* will return later */
   1276 
   1277 	case RESETCOMPLETE:
   1278 		untimeout(fdctimeout, fdc);
   1279 		fdconf(fdc);
   1280 
   1281 		/* fall through */
   1282 	case DORECAL:
   1283 		fdc->sc_state = RECALWAIT;
   1284 		fdc->sc_istate = ISTATE_SENSEI;
   1285 		fdc->sc_nstat = 0;
   1286 		/* recalibrate function */
   1287 		OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
   1288 		OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
   1289 		timeout(fdctimeout, fdc, 5 * hz);
   1290 		return 1;			/* will return later */
   1291 
   1292 	case RECALWAIT:
   1293 		untimeout(fdctimeout, fdc);
   1294 		fdc->sc_state = RECALCOMPLETE;
   1295 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1296 			/* allow 1/30 second for heads to settle */
   1297 			timeout(fdcpseudointr, fdc, hz / 30);
   1298 			return 1;		/* will return later */
   1299 		}
   1300 
   1301 	case RECALCOMPLETE:
   1302 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1303 #ifdef FD_DEBUG
   1304 			if (fdc_debug)
   1305 				fdcstatus(&fd->sc_dk.dk_dev, 2, "recalibrate failed");
   1306 #endif
   1307 			fdcretry(fdc);
   1308 			goto loop;
   1309 		}
   1310 		fd->sc_cylin = 0;
   1311 		goto doseek;
   1312 
   1313 	case MOTORWAIT:
   1314 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1315 			return 1;		/* time's not up yet */
   1316 		goto doseek;
   1317 
   1318 	default:
   1319 		fdcstatus(&fd->sc_dk.dk_dev, 0, "stray interrupt");
   1320 		return 1;
   1321 	}
   1322 #ifdef DIAGNOSTIC
   1323 	panic("fdcintr: impossible");
   1324 #endif
   1325 #undef	st0
   1326 #undef	st1
   1327 #undef	cyl
   1328 }
   1329 
   1330 void
   1331 fdcretry(fdc)
   1332 	struct fdc_softc *fdc;
   1333 {
   1334 	struct fd_softc *fd;
   1335 	struct buf *bp;
   1336 
   1337 	fd = fdc->sc_drives.tqh_first;
   1338 	bp = fd->sc_q.b_actf;
   1339 
   1340 	fdc->sc_overruns = 0;
   1341 
   1342 	switch (fdc->sc_errors) {
   1343 	case 0:
   1344 		/* try again */
   1345 		fdc->sc_state =
   1346 			(fdc->sc_flags & FDC_EIS) ? DOIO : SEEKCOMPLETE;
   1347 		break;
   1348 
   1349 	case 1: case 2: case 3:
   1350 		/* didn't work; try recalibrating */
   1351 		fdc->sc_state = DORECAL;
   1352 		break;
   1353 
   1354 	case 4:
   1355 		/* still no go; reset the bastard */
   1356 		fdc->sc_state = DORESET;
   1357 		break;
   1358 
   1359 	default:
   1360 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1361 		    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1362 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
   1363 		    fdc->sc_status[0], NE7_ST0BITS,
   1364 		    fdc->sc_status[1], NE7_ST1BITS,
   1365 		    fdc->sc_status[2], NE7_ST2BITS,
   1366 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1367 
   1368 		bp->b_flags |= B_ERROR;
   1369 		bp->b_error = EIO;
   1370 		fdfinish(fd, bp);
   1371 	}
   1372 	fdc->sc_errors++;
   1373 }
   1374 
   1375 int
   1376 fdsize(dev)
   1377 	dev_t dev;
   1378 {
   1379 
   1380 	/* Swapping to floppies would not make sense. */
   1381 	return -1;
   1382 }
   1383 
   1384 int
   1385 fddump()
   1386 {
   1387 
   1388 	/* Not implemented. */
   1389 	return EINVAL;
   1390 }
   1391 
   1392 int
   1393 fdioctl(dev, cmd, addr, flag)
   1394 	dev_t dev;
   1395 	u_long cmd;
   1396 	caddr_t addr;
   1397 	int flag;
   1398 {
   1399 	struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
   1400 	struct disklabel buffer;
   1401 	int error;
   1402 
   1403 	switch (cmd) {
   1404 	case DIOCGDINFO:
   1405 		bzero(&fd->sc_dk.dk_label, sizeof(struct disklabel));
   1406 
   1407 		fd->sc_dk.dk_label.d_secpercyl = fd->sc_type->seccyl;
   1408 		fd->sc_dk.dk_label.d_type = DTYPE_FLOPPY;
   1409 		fd->sc_dk.dk_label.d_secsize = FDC_BSIZE;
   1410 
   1411 		if (readdisklabel(dev, fdstrategy,
   1412 				  &fd->sc_dk.dk_label,
   1413 				  &fd->sc_dk.dk_cpulabel) != NULL)
   1414 			return EINVAL;
   1415 
   1416 		*(struct disklabel *)addr = fd->sc_dk.dk_label;
   1417 		return 0;
   1418 
   1419 	case DIOCWLABEL:
   1420 		if ((flag & FWRITE) == 0)
   1421 			return EBADF;
   1422 		/* XXX do something */
   1423 		return 0;
   1424 
   1425 	case DIOCWDINFO:
   1426 		if ((flag & FWRITE) == 0)
   1427 			return EBADF;
   1428 
   1429 		error = setdisklabel(&fd->sc_dk.dk_label,
   1430 				    (struct disklabel *)addr, 0,
   1431 				    &fd->sc_dk.dk_cpulabel);
   1432 		if (error)
   1433 			return error;
   1434 
   1435 		error = writedisklabel(dev, fdstrategy,
   1436 				       &fd->sc_dk.dk_label,
   1437 				       &fd->sc_dk.dk_cpulabel);
   1438 		return error;
   1439 
   1440 	case DIOCEJECT:
   1441 		auxregbisc(AUXIO_FDS, AUXIO_FEJ);
   1442 		delay(10);
   1443 		auxregbisc(AUXIO_FEJ, AUXIO_FDS);
   1444 		return 0;
   1445 #ifdef DEBUG
   1446 	case _IO('f', 100):
   1447 		{
   1448 		int i;
   1449 		struct fdc_softc *fdc = (struct fdc_softc *)
   1450 					fd->sc_dk.dk_dev.dv_parent;
   1451 
   1452 		out_fdc(fdc, NE7CMD_DUMPREG);
   1453 		fdcresult(fdc);
   1454 		printf("dumpreg(%d regs): <", fdc->sc_nstat);
   1455 		for (i = 0; i < fdc->sc_nstat; i++)
   1456 			printf(" %x", fdc->sc_status[i]);
   1457 		printf(">\n");
   1458 		}
   1459 
   1460 		return 0;
   1461 	case _IOW('f', 101, int):
   1462 		((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent)->sc_cfg &=
   1463 			~CFG_THRHLD_MASK;
   1464 		((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent)->sc_cfg |=
   1465 			(*(int *)addr & CFG_THRHLD_MASK);
   1466 		fdconf(fd->sc_dk.dk_dev.dv_parent);
   1467 		return 0;
   1468 	case _IO('f', 102):
   1469 		{
   1470 		int i;
   1471 		struct fdc_softc *fdc = (struct fdc_softc *)
   1472 					fd->sc_dk.dk_dev.dv_parent;
   1473 		out_fdc(fdc, NE7CMD_SENSEI);
   1474 		fdcresult(fdc);
   1475 		printf("sensei(%d regs): <", fdc->sc_nstat);
   1476 		for (i=0; i< fdc->sc_nstat; i++)
   1477 			printf(" 0x%x", fdc->sc_status[i]);
   1478 		}
   1479 		printf(">\n");
   1480 		return 0;
   1481 #endif
   1482 	default:
   1483 		return ENOTTY;
   1484 	}
   1485 
   1486 #ifdef DIAGNOSTIC
   1487 	panic("fdioctl: impossible");
   1488 #endif
   1489 }
   1490