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