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