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