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