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