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