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