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