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