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