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