Home | History | Annotate | Line # | Download | only in dev
fdc.c revision 1.21
      1 /*	$NetBSD: fdc.c,v 1.21 2008/04/05 13:40:05 cegger Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1990 The Regents of the University of California.
     41  * All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * Don Ahn.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     71  */
     72 
     73 /*-
     74  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
     75  *
     76  * This code is derived from software contributed to Berkeley by
     77  * Don Ahn.
     78  *
     79  * Redistribution and use in source and binary forms, with or without
     80  * modification, are permitted provided that the following conditions
     81  * are met:
     82  * 1. Redistributions of source code must retain the above copyright
     83  *    notice, this list of conditions and the following disclaimer.
     84  * 2. Redistributions in binary form must reproduce the above copyright
     85  *    notice, this list of conditions and the following disclaimer in the
     86  *    documentation and/or other materials provided with the distribution.
     87  * 3. All advertising materials mentioning features or use of this software
     88  *    must display the following acknowledgement:
     89  *	This product includes software developed by the University of
     90  *	California, Berkeley and its contributors.
     91  * 4. Neither the name of the University nor the names of its contributors
     92  *    may be used to endorse or promote products derived from this software
     93  *    without specific prior written permission.
     94  *
     95  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     96  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     97  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     98  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     99  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    100  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    101  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    102  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    103  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    104  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    105  * SUCH DAMAGE.
    106  *
    107  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
    108  */
    109 
    110 #include <sys/cdefs.h>
    111 __KERNEL_RCSID(0, "$NetBSD: fdc.c,v 1.21 2008/04/05 13:40:05 cegger Exp $");
    112 
    113 #include "opt_ddb.h"
    114 #include "opt_md.h"
    115 
    116 #include <sys/param.h>
    117 #include <sys/types.h>
    118 #include <sys/systm.h>
    119 #include <sys/callout.h>
    120 #include <sys/kernel.h>
    121 #include <sys/file.h>
    122 #include <sys/ioctl.h>
    123 #include <sys/device.h>
    124 #include <sys/disklabel.h>
    125 #include <sys/disk.h>
    126 #include <sys/fdio.h>
    127 #include <sys/buf.h>
    128 #include <sys/bufq.h>
    129 #include <sys/malloc.h>
    130 #include <sys/proc.h>
    131 #include <sys/uio.h>
    132 #include <sys/stat.h>
    133 #include <sys/syslog.h>
    134 #include <sys/queue.h>
    135 #include <sys/conf.h>
    136 #include <sys/intr.h>
    137 
    138 #include <dev/cons.h>
    139 
    140 #include <uvm/uvm_extern.h>
    141 
    142 #include <machine/autoconf.h>
    143 
    144 #ifdef SUN4
    145 #include <sparc/sparc/auxreg.h>
    146 #include <sparc/dev/fdreg.h>
    147 #include <sparc/dev/fdvar.h>
    148 #elif SUN4U
    149 #include <dev/ebus/ebusreg.h>
    150 #include <dev/ebus/ebusvar.h>
    151 /* #include <sparc/sparc/auxreg.h> */
    152 #include <sparc64/dev/auxioreg.h>
    153 #include <sparc64/dev/auxiovar.h>
    154 #include <sparc64/dev/fdcreg.h>
    155 #include <sparc64/dev/fdcvar.h>
    156 #endif
    157 
    158 #include <prop/proplib.h>
    159 
    160 #define FDUNIT(dev)	(minor(dev) / 8)
    161 #define FDTYPE(dev)	(minor(dev) % 8)
    162 
    163 /* (mis)use device use flag to identify format operation */
    164 #define B_FORMAT B_DEVPRIVATE
    165 
    166 #define FD_DEBUG
    167 #ifdef FD_DEBUG
    168 int	fdc_debug = 0;
    169 #endif
    170 
    171 enum fdc_state {
    172 	DEVIDLE = 0,
    173 	MOTORWAIT,	/*  1 */
    174 	DOSEEK,		/*  2 */
    175 	SEEKWAIT,	/*  3 */
    176 	SEEKTIMEDOUT,	/*  4 */
    177 	SEEKCOMPLETE,	/*  5 */
    178 	DOIO,		/*  6 */
    179 	IOCOMPLETE,	/*  7 */
    180 	IOTIMEDOUT,	/*  8 */
    181 	IOCLEANUPWAIT,	/*  9 */
    182 	IOCLEANUPTIMEDOUT,/*10 */
    183 	DORESET,	/* 11 */
    184 	RESETCOMPLETE,	/* 12 */
    185 	RESETTIMEDOUT,	/* 13 */
    186 	DORECAL,	/* 14 */
    187 	RECALWAIT,	/* 15 */
    188 	RECALTIMEDOUT,	/* 16 */
    189 	RECALCOMPLETE,	/* 17 */
    190 	DODSKCHG,	/* 18 */
    191 	DSKCHGWAIT,	/* 19 */
    192 	DSKCHGTIMEDOUT,	/* 20 */
    193 };
    194 
    195 /* software state, per controller */
    196 struct fdc_softc {
    197 	struct device	sc_dev;		/* boilerplate */
    198 	bus_space_tag_t	sc_bustag;
    199 
    200 	struct callout sc_timo_ch;	/* timeout callout */
    201 	struct callout sc_intr_ch;	/* pseudo-intr callout */
    202 
    203 	struct fd_softc *sc_fd[4];	/* pointers to children */
    204 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    205 	enum fdc_state	sc_state;
    206 	int		sc_flags;
    207 #define FDC_82077		0x01
    208 #define FDC_NEEDHEADSETTLE	0x02
    209 #define FDC_EIS			0x04
    210 #define FDC_NEEDMOTORWAIT	0x08
    211 #define FDC_NOEJECT		0x10
    212 #define FDC_EBUS		0x20
    213 	int		sc_errors;		/* number of retries so far */
    214 	int		sc_overruns;		/* number of DMA overruns */
    215 	int		sc_cfg;			/* current configuration */
    216 	struct fdcio	sc_io;
    217 #define sc_handle	sc_io.fdcio_handle
    218 #define sc_reg_msr	sc_io.fdcio_reg_msr
    219 #define sc_reg_fifo	sc_io.fdcio_reg_fifo
    220 #define sc_reg_dor	sc_io.fdcio_reg_dor
    221 #define sc_reg_dir	sc_io.fdcio_reg_dir
    222 #define sc_reg_drs	sc_io.fdcio_reg_msr
    223 #define sc_itask	sc_io.fdcio_itask
    224 #define sc_istatus	sc_io.fdcio_istatus
    225 #define sc_data		sc_io.fdcio_data
    226 #define sc_tc		sc_io.fdcio_tc
    227 #define sc_nstat	sc_io.fdcio_nstat
    228 #define sc_status	sc_io.fdcio_status
    229 #define sc_intrcnt	sc_io.fdcio_intrcnt
    230 
    231 	void		*sc_sicookie;	/* softint(9) cookie */
    232 };
    233 
    234 #ifdef SUN4
    235 extern	struct fdcio	*fdciop;	/* I/O descriptor used in fdintr.s */
    236 #endif
    237 
    238 /* controller driver configuration */
    239 #ifdef SUN4
    240 int	fdcmatch_mainbus(struct device *, struct cfdata *, void*);
    241 int	fdcmatch_obio(struct device *, struct cfdata *, void *);
    242 void	fdcattach_mainbus(struct device *, struct device *, void *);
    243 void	fdcattach_obio(struct device *, struct device *, void *);
    244 #elif SUN4U
    245 int	fdcmatch_sbus(struct device *, struct cfdata *, void *);
    246 int	fdcmatch_ebus(struct device *, struct cfdata *, void *);
    247 void	fdcattach_sbus(struct device *, struct device *, void *);
    248 void	fdcattach_ebus(struct device *, struct device *, void *);
    249 #endif
    250 
    251 int	fdcattach(struct fdc_softc *, int);
    252 
    253 #ifdef SUN4
    254 CFATTACH_DECL(fdc_mainbus, sizeof(struct fdc_softc),
    255     fdcmatch_mainbus, fdcattach_mainbus, NULL, NULL);
    256 
    257 CFATTACH_DECL(fdc_obio, sizeof(struct fdc_softc),
    258     fdcmatch_obio, fdcattach_obio, NULL, NULL);
    259 #elif SUN4U
    260 CFATTACH_DECL(fdc_sbus, sizeof(struct fdc_softc),
    261     fdcmatch_sbus, fdcattach_sbus, NULL, NULL);
    262 
    263 CFATTACH_DECL(fdc_ebus, sizeof(struct fdc_softc),
    264     fdcmatch_ebus, fdcattach_ebus, NULL, NULL);
    265 #endif
    266 
    267 inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
    268 
    269 /*
    270  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    271  * we tell them apart.
    272  */
    273 struct fd_type {
    274 	int	sectrac;	/* sectors per track */
    275 	int	heads;		/* number of heads */
    276 	int	seccyl;		/* sectors per cylinder */
    277 	int	secsize;	/* size code for sectors */
    278 	int	datalen;	/* data len when secsize = 0 */
    279 	int	steprate;	/* step rate and head unload time */
    280 	int	gap1;		/* gap len between sectors */
    281 	int	gap2;		/* formatting gap */
    282 	int	cylinders;	/* total num of cylinders */
    283 	int	size;		/* size of disk in sectors */
    284 	int	step;		/* steps per cylinder */
    285 	int	rate;		/* transfer speed code */
    286 	int	fillbyte;	/* format fill byte */
    287 	int	interleave;	/* interleave factor (formatting) */
    288 	const char *name;
    289 };
    290 
    291 /* The order of entries in the following table is important -- BEWARE! */
    292 struct fd_type fd_types[] = {
    293 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette */
    294 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5" 720kB diskette */
    295 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
    296 	{  8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" } /* 1.2 MB japanese format */
    297 };
    298 
    299 /* software state, per disk (with up to 4 disks per ctlr) */
    300 struct fd_softc {
    301 	struct device	sc_dv;		/* generic device info */
    302 	struct disk	sc_dk;		/* generic disk info */
    303 
    304 	struct fd_type *sc_deftype;	/* default type descriptor */
    305 	struct fd_type *sc_type;	/* current type descriptor */
    306 
    307 	struct callout sc_motoron_ch;
    308 	struct callout sc_motoroff_ch;
    309 
    310 	daddr_t	sc_blkno;	/* starting block number */
    311 	int sc_bcount;		/* byte count left */
    312 	int sc_skip;		/* bytes already transferred */
    313 	int sc_nblks;		/* number of blocks currently transferring */
    314 	int sc_nbytes;		/* number of bytes currently transferring */
    315 
    316 	int sc_drive;		/* physical unit number */
    317 	int sc_flags;
    318 #define	FD_OPEN		0x01		/* it's open */
    319 #define	FD_MOTOR	0x02		/* motor should be on */
    320 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    321 	int sc_cylin;		/* where we think the head is */
    322 	int sc_opts;		/* user-set options */
    323 
    324 	void	*sc_sdhook;	/* shutdownhook cookie */
    325 
    326 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    327 	int sc_ops;		/* I/O ops since last switch */
    328 	struct bufq_state *sc_q;/* pending I/O requests */
    329 	int sc_active;		/* number of active I/O requests */
    330 };
    331 
    332 /* floppy driver configuration */
    333 int	fdmatch(struct device *, struct cfdata *, void *);
    334 void	fdattach(struct device *, struct device *, void *);
    335 
    336 CFATTACH_DECL(fd, sizeof(struct fd_softc),
    337     fdmatch, fdattach, NULL, NULL);
    338 
    339 extern struct cfdriver fd_cd;
    340 
    341 dev_type_open(fdopen);
    342 dev_type_close(fdclose);
    343 dev_type_read(fdread);
    344 dev_type_write(fdwrite);
    345 dev_type_ioctl(fdioctl);
    346 dev_type_strategy(fdstrategy);
    347 
    348 const struct bdevsw fd_bdevsw = {
    349 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
    350 };
    351 
    352 const struct cdevsw fd_cdevsw = {
    353 	fdopen, fdclose, fdread, fdwrite, fdioctl,
    354 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    355 };
    356 
    357 void fdgetdisklabel(dev_t);
    358 int fd_get_parms(struct fd_softc *);
    359 void fdstrategy(struct buf *);
    360 void fdstart(struct fd_softc *);
    361 int fdprint(void *, const char *);
    362 
    363 struct dkdriver fddkdriver = { fdstrategy, NULL };
    364 
    365 struct	fd_type *fd_nvtotype(char *, int, int);
    366 void	fd_set_motor(struct fdc_softc *);
    367 void	fd_motor_off(void *);
    368 void	fd_motor_on(void *);
    369 int	fdcresult(struct fdc_softc *);
    370 int	fdc_wrfifo(struct fdc_softc *, uint8_t);
    371 void	fdcstart(struct fdc_softc *);
    372 void	fdcstatus(struct fdc_softc *, const char *);
    373 void	fdc_reset(struct fdc_softc *);
    374 int	fdc_diskchange(struct fdc_softc *);
    375 void	fdctimeout(void *);
    376 void	fdcpseudointr(void *);
    377 int	fdc_c_hwintr(void *);
    378 void	fdchwintr(void);
    379 void	fdcswintr(void *);
    380 int	fdcstate(struct fdc_softc *);
    381 void	fdcretry(struct fdc_softc *);
    382 void	fdfinish(struct fd_softc *, struct buf *);
    383 int	fdformat(dev_t, struct ne7_fd_formb *, struct proc *);
    384 void	fd_do_eject(struct fd_softc *);
    385 void	fd_mountroot_hook(struct device *);
    386 static int fdconf(struct fdc_softc *);
    387 static void establish_chip_type(
    388 		struct fdc_softc *,
    389 		bus_space_tag_t,
    390 		bus_addr_t,
    391 		bus_size_t,
    392 		bus_space_handle_t);
    393 static void	fd_set_properties(struct fd_softc *);
    394 
    395 #ifdef MEMORY_DISK_HOOKS
    396 int	fd_read_md_image(size_t *, void **);
    397 #endif
    398 
    399 #ifdef SUN4
    400 #define OBP_FDNAME	(CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
    401 
    402 int
    403 fdcmatch_mainbus(struct device *parent, struct cfdata *match, void *aux)
    404 {
    405 	struct mainbus_attach_args *ma = aux;
    406 
    407 	/*
    408 	 * Floppy controller is on mainbus on sun4c.
    409 	 */
    410 	if (!CPU_ISSUN4C)
    411 		return 0;
    412 
    413 	/* sun4c PROMs call the controller "fd" */
    414 	if (strcmp("fd", ma->ma_name) != 0)
    415 		return 0;
    416 
    417 	return bus_space_probe(ma->ma_bustag,
    418 			       ma->ma_paddr,
    419 			       1,	/* probe size */
    420 			       0,	/* offset */
    421 			       0,	/* flags */
    422 			       NULL, NULL);
    423 }
    424 
    425 int
    426 fdcmatch_obio(struct device *parent, struct cfdata *match, void *aux)
    427 {
    428 	union obio_attach_args *uoba = aux;
    429 	struct sbus_attach_args *sa;
    430 
    431 	/*
    432 	 * Floppy controller is on obio on sun4m.
    433 	 */
    434 	if (uoba->uoba_isobio4 != 0)
    435 		return 0;
    436 
    437 	sa = &uoba->uoba_sbus;
    438 
    439 	/* sun4m PROMs call the controller "SUNW,fdtwo" */
    440 	if (strcmp("SUNW,fdtwo", sa->sa_name) != 0)
    441 		return 0;
    442 
    443 	return bus_space_probe(sa->sa_bustag,
    444 			sbus_bus_addr(sa->sa_bustag,
    445 					sa->sa_slot, sa->sa_offset),
    446 			1,	/* probe size */
    447 			0,	/* offset */
    448 			0,	/* flags */
    449 			NULL, NULL);
    450 }
    451 
    452 #elif SUN4U
    453 
    454 int
    455 fdcmatch_sbus(struct device *parent, struct cfdata *match, void *aux)
    456 {
    457 	struct sbus_attach_args *sa = aux;
    458 
    459 	return strcmp("SUNW,fdtwo", sa->sa_name) == 0;
    460 }
    461 
    462 int
    463 fdcmatch_ebus(struct device *parent, struct cfdata *match, void *aux)
    464 {
    465 	struct ebus_attach_args *ea = aux;
    466 
    467 	return strcmp("fdthree", ea->ea_name) == 0;
    468 }
    469 #endif
    470 
    471 static void
    472 establish_chip_type(struct fdc_softc *fdc,
    473 		    bus_space_tag_t tag, bus_addr_t addr, bus_size_t size,
    474 		    bus_space_handle_t handle)
    475 {
    476 	uint8_t v;
    477 
    478 	/*
    479 	 * This hack from Chris Torek: apparently DOR really
    480 	 * addresses MSR/DRS on a 82072.
    481 	 * We used to rely on the VERSION command to tell the
    482 	 * difference (which did not work).
    483 	 */
    484 
    485 	/* First, check the size of the register bank */
    486 	if (size < 8)
    487 		/* It isn't a 82077 */
    488 		return;
    489 
    490 #ifdef SUN4
    491 	/* Then probe the DOR register offset */
    492 	if (bus_space_probe(tag, addr,
    493 			    1,			/* probe size */
    494 			    FDREG77_DOR,	/* offset */
    495 			    0,			/* flags */
    496 			    NULL, NULL) == 0) {
    497 
    498 		/* It isn't a 82077 */
    499 		return;
    500 	}
    501 #endif
    502 
    503 	v = bus_space_read_1(tag, handle, FDREG77_DOR);
    504 	if (v == NE7_RQM) {
    505 		/*
    506 		 * Value in DOR looks like it's really MSR
    507 		 */
    508 		bus_space_write_1(tag, handle, FDREG77_DOR, FDC_250KBPS);
    509 		v = bus_space_read_1(tag, handle, FDREG77_DOR);
    510 		if (v == NE7_RQM) {
    511 			/*
    512 			 * The value in the DOR didn't stick;
    513 			 * it isn't a 82077
    514 			 */
    515 			return;
    516 		}
    517 	}
    518 
    519 	fdc->sc_flags |= FDC_82077;
    520 }
    521 
    522 /*
    523  * Arguments passed between fdcattach and fdprobe.
    524  */
    525 struct fdc_attach_args {
    526 	int fa_drive;
    527 	struct fd_type *fa_deftype;
    528 };
    529 
    530 /*
    531  * Print the location of a disk drive (called just before attaching the
    532  * the drive).  If `fdc' is not NULL, the drive was found but was not
    533  * in the system config file; print the drive name as well.
    534  * Return QUIET (config_find ignores this if the device was configured) to
    535  * avoid printing `fdN not configured' messages.
    536  */
    537 int
    538 fdprint(void *aux, const char *fdc)
    539 {
    540 	register struct fdc_attach_args *fa = aux;
    541 
    542 	if (!fdc)
    543 		aprint_normal(" drive %d", fa->fa_drive);
    544 	return QUIET;
    545 }
    546 
    547 /*
    548  * Configure several parameters and features on the FDC.
    549  * Return 0 on success.
    550  */
    551 static int
    552 fdconf(struct fdc_softc *fdc)
    553 {
    554 	int	vroom;
    555 
    556 	if (fdc_wrfifo(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
    557 		return -1;
    558 
    559 	/*
    560 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
    561 	 * the PROM thinks is appropriate.
    562 	 */
    563 	if ((vroom = fdc->sc_status[7]) == 0)
    564 		vroom = 0x64;
    565 
    566 	/* Configure controller to use FIFO and Implied Seek */
    567 	if (fdc_wrfifo(fdc, NE7CMD_CFG) != 0)
    568 		return -1;
    569 	if (fdc_wrfifo(fdc, vroom) != 0)
    570 		return -1;
    571 	if (fdc_wrfifo(fdc, fdc->sc_cfg) != 0)
    572 		return -1;
    573 	if (fdc_wrfifo(fdc, 0) != 0)	/* PRETRK */
    574 		return -1;
    575 	/* No result phase for the NE7CMD_CFG command */
    576 
    577 	if ((fdc->sc_flags & FDC_82077) != 0) {
    578 		/* Lock configuration across soft resets. */
    579 		if (fdc_wrfifo(fdc, NE7CMD_LOCK | CFG_LOCK) != 0 ||
    580 		    fdcresult(fdc) != 1) {
    581 #ifdef DEBUG
    582 			printf("fdconf: CFGLOCK failed");
    583 #endif
    584 			return -1;
    585 		}
    586 	}
    587 
    588 	return 0;
    589 #if 0
    590 	if (fdc_wrfifo(fdc, NE7CMD_VERSION) == 0 &&
    591 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
    592 		if (fdc_debug)
    593 			printf("[version cmd]");
    594 	}
    595 #endif
    596 }
    597 
    598 #ifdef SUN4
    599 void
    600 fdcattach_mainbus(struct device *parent, struct device *self, void *aux)
    601 {
    602 	struct fdc_softc *fdc = (void *)self;
    603 	struct mainbus_attach_args *ma = aux;
    604 
    605 	fdc->sc_bustag = ma->ma_bustag;
    606 
    607 	if (bus_space_map(
    608 			ma->ma_bustag,
    609 			ma->ma_paddr,
    610 			ma->ma_size,
    611 			BUS_SPACE_MAP_LINEAR,
    612 			&fdc->sc_handle) != 0) {
    613 		aprint_error_dev(self, "cannot map registers\n");
    614 		return;
    615 	}
    616 
    617 	establish_chip_type(fdc,
    618 			    ma->ma_bustag,
    619 			    ma->ma_paddr,
    620 			    ma->ma_size,
    621 			    fdc->sc_handle);
    622 
    623 	if (fdcattach(fdc, ma->ma_pri) != 0)
    624 		bus_space_unmap(ma->ma_bustag, fdc->sc_handle, ma->ma_size);
    625 }
    626 
    627 void
    628 fdcattach_obio(struct device *parent, struct device *self, void *aux)
    629 {
    630 	struct fdc_softc *fdc = (void *)self;
    631 	union obio_attach_args *uoba = aux;
    632 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    633 
    634 	if (sa->sa_nintr == 0) {
    635 		printf(": no interrupt line configured\n");
    636 		return;
    637 	}
    638 
    639 	fdc->sc_bustag = sa->sa_bustag;
    640 
    641 	if (sbus_bus_map(sa->sa_bustag,
    642 			 sa->sa_slot, sa->sa_offset, sa->sa_size,
    643 			 BUS_SPACE_MAP_LINEAR, &fdc->sc_handle) != 0) {
    644 		aprint_error_dev(self, "cannot map control registers\n");
    645 		return;
    646 	}
    647 
    648 	establish_chip_type(fdc,
    649 		sa->sa_bustag,
    650 		sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset),
    651 		sa->sa_size,
    652 		fdc->sc_handle);
    653 
    654 	if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
    655 		printf(": no drives attached\n");
    656 		return;
    657 	}
    658 
    659 	if (fdcattach(fdc, sa->sa_pri) != 0)
    660 		bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
    661 }
    662 
    663 #elif SUN4U
    664 
    665 void
    666 fdcattach_sbus(struct device *parent, struct device *self, void *aux)
    667 {
    668 	struct fdc_softc *fdc = (void *)self;
    669 	struct sbus_attach_args *sa = aux;
    670 
    671 	if (sa->sa_nintr == 0) {
    672 		printf(": no interrupt line configured\n");
    673 		return;
    674 	}
    675 
    676 	if (auxio_fd_control(0) != 0) {
    677 		printf(": can't attach before auxio\n");
    678 		return;
    679 	}
    680 
    681 	fdc->sc_bustag = sa->sa_bustag;
    682 
    683 	if (bus_space_map(sa->sa_bustag, BUS_ADDR(sa->sa_slot, sa->sa_offset),
    684 			  sa->sa_size, 0, &fdc->sc_handle) != 0) {
    685 		printf(": cannot map control registers\n");
    686 		return;
    687 	}
    688 
    689 	establish_chip_type(fdc,
    690 			    sa->sa_bustag,
    691 			    BUS_ADDR(sa->sa_slot, sa->sa_offset),
    692 			    sa->sa_size,
    693 			    fdc->sc_handle);
    694 
    695 	if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
    696 		printf(": no drives attached\n");
    697 		return;
    698 	}
    699 
    700 	if (prom_getproplen(sa->sa_node, "manual") >= 0)
    701 		fdc->sc_flags |= FDC_NOEJECT;
    702 
    703 
    704 	if (fdcattach(fdc, sa->sa_pri) != 0)
    705 		bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
    706 }
    707 
    708 void
    709 fdcattach_ebus(struct device *parent, struct device *self, void *aux)
    710 {
    711 	struct fdc_softc *fdc = (void *)self;
    712 	struct ebus_attach_args *ea = aux;
    713 	int map_vaddr;
    714 
    715 	if (ea->ea_nintr == 0) {
    716 		printf(": no interrupt line configured\n");
    717 		return;
    718 	}
    719 
    720 	if (ea->ea_nreg < 3) {
    721 		printf(": expected 3 registers, only got %d\n",
    722 		    ea->ea_nreg);
    723 		return;
    724 	}
    725 
    726 	fdc->sc_bustag = ea->ea_bustag;
    727 
    728 	if (ea->ea_nvaddr > 0) {
    729 		sparc_promaddr_to_handle(ea->ea_bustag,
    730 		    ea->ea_vaddr[0], &fdc->sc_handle);
    731 		map_vaddr = 1;
    732 	} else if (bus_space_map(fdc->sc_bustag,
    733 	    EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    734 	    ea->ea_reg[0].size, 0, &fdc->sc_handle) == 0) {
    735 		map_vaddr = 0;
    736 	} else {
    737 		printf(": can't map control registers\n");
    738 		return;
    739 	}
    740 
    741 	establish_chip_type(fdc,
    742 		fdc->sc_bustag,
    743 		map_vaddr ? ea->ea_vaddr[0] :
    744 		    EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    745 		ea->ea_reg[0].size,
    746 		fdc->sc_handle);
    747 
    748 	fdc->sc_flags |= FDC_EBUS;
    749 
    750 	if (prom_getproplen(ea->ea_node, "manual") >= 0)
    751 		fdc->sc_flags |= FDC_NOEJECT;
    752 
    753 	if (fdcattach(fdc, ea->ea_intr[0]) != 0)
    754 		if (map_vaddr == 0)
    755 			bus_space_unmap(ea->ea_bustag, fdc->sc_handle,
    756 			    ea->ea_reg[0].size);
    757 }
    758 #endif
    759 
    760 int
    761 fdcattach(struct fdc_softc *fdc, int pri)
    762 {
    763 	struct fdc_attach_args fa;
    764 	int drive_attached;
    765 	char code;
    766 
    767 	callout_init(&fdc->sc_timo_ch, 0);
    768 	callout_init(&fdc->sc_intr_ch, 0);
    769 
    770 	fdc->sc_state = DEVIDLE;
    771 	fdc->sc_itask = FDC_ITASK_NONE;
    772 	fdc->sc_istatus = FDC_ISTATUS_NONE;
    773 	fdc->sc_flags |= FDC_EIS;
    774 	TAILQ_INIT(&fdc->sc_drives);
    775 
    776 	if ((fdc->sc_flags & FDC_82077) != 0) {
    777 		fdc->sc_reg_msr = FDREG77_MSR;
    778 		fdc->sc_reg_fifo = FDREG77_FIFO;
    779 		fdc->sc_reg_dor = FDREG77_DOR;
    780 		fdc->sc_reg_dir = FDREG77_DIR;
    781 		code = '7';
    782 		fdc->sc_flags |= FDC_NEEDMOTORWAIT;
    783 	} else {
    784 		fdc->sc_reg_msr = FDREG72_MSR;
    785 		fdc->sc_reg_fifo = FDREG72_FIFO;
    786 		fdc->sc_reg_dor = 0;
    787 		code = '2';
    788 	}
    789 
    790 	/*
    791 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
    792 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
    793 	 */
    794 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
    795 	if (fdconf(fdc) != 0) {
    796 		printf(": no drives attached\n");
    797 		return -1;
    798 	}
    799 
    800 	fdc->sc_sicookie = softint_establish(SOFTINT_BIO, fdcswintr, fdc);
    801 	if (fdc->sc_sicookie == NULL) {
    802 		aprint_normal("\n");
    803 		aprint_error_dev(&fdc->sc_dev, "cannot register soft interrupt handler\n");
    804 		callout_stop(&fdc->sc_timo_ch);
    805 		callout_stop(&fdc->sc_intr_ch);
    806 		return -1;
    807 	}
    808 #ifdef SUN4
    809 	printf(" softpri %d: chip 8207%c\n", IPL_SOFTFDC, code);
    810 #elif SUN4U
    811 	printf(" softpri %d: chip 8207%c", PIL_FDSOFT, code);
    812 	if (fdc->sc_flags & FDC_NOEJECT)
    813 		printf(": manual eject");
    814 	printf("\n");
    815 #endif
    816 
    817 #ifdef SUN4
    818 	fdciop = &fdc->sc_io;
    819 	if (bus_intr_establish2(fdc->sc_bustag, pri, 0,
    820 				fdc_c_hwintr, fdc, fdchwintr) == NULL) {
    821 #elif SUN4U
    822 	if (bus_intr_establish(fdc->sc_bustag, pri, IPL_BIO,
    823 				fdc_c_hwintr, fdc) == NULL) {
    824 #endif
    825 		aprint_normal("\n");
    826 		aprint_error_dev(&fdc->sc_dev, "cannot register interrupt handler\n");
    827 		callout_stop(&fdc->sc_timo_ch);
    828 		callout_stop(&fdc->sc_intr_ch);
    829 		softint_disestablish(fdc->sc_sicookie);
    830 		return -1;
    831 	}
    832 
    833 	evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    834 	    device_xname(&fdc->sc_dev), "intr");
    835 
    836 	/* physical limit: four drives per controller. */
    837 	drive_attached = 0;
    838 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    839 		fa.fa_deftype = NULL;		/* unknown */
    840 		fa.fa_deftype = &fd_types[0];	/* XXX */
    841 		if (config_found(&fdc->sc_dev, (void *)&fa, fdprint) != NULL)
    842 			drive_attached = 1;
    843 	}
    844 
    845 	if (drive_attached == 0) {
    846 		/* XXX - dis-establish interrupts here */
    847 		/* return -1; */
    848 	}
    849 
    850 	return 0;
    851 }
    852 
    853 int
    854 fdmatch(struct device *parent, struct cfdata *match, void *aux)
    855 {
    856 	struct fdc_softc *fdc = (void *)parent;
    857 	bus_space_tag_t t = fdc->sc_bustag;
    858 	bus_space_handle_t h = fdc->sc_handle;
    859 	struct fdc_attach_args *fa = aux;
    860 	int drive = fa->fa_drive;
    861 	int n, ok;
    862 
    863 	if (drive > 0)
    864 		/* XXX - for now, punt on more than one drive */
    865 		return 0;
    866 
    867 	if ((fdc->sc_flags & FDC_82077) != 0) {
    868 		/* select drive and turn on motor */
    869 		bus_space_write_1(t, h, fdc->sc_reg_dor,
    870 				  drive | FDO_FRST | FDO_MOEN(drive));
    871 		/* wait for motor to spin up */
    872 		delay(250000);
    873 #ifdef SUN4
    874 	} else {
    875 		auxregbisc(AUXIO4C_FDS, 0);
    876 #endif
    877 	}
    878 	fdc->sc_nstat = 0;
    879 	fdc_wrfifo(fdc, NE7CMD_RECAL);
    880 	fdc_wrfifo(fdc, drive);
    881 
    882 	/* Wait for recalibration to complete */
    883 	for (n = 0; n < 10000; n++) {
    884 		uint8_t v;
    885 
    886 		delay(1000);
    887 		v = bus_space_read_1(t, h, fdc->sc_reg_msr);
    888 		if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
    889 			/* wait a bit longer till device *really* is ready */
    890 			delay(100000);
    891 			if (fdc_wrfifo(fdc, NE7CMD_SENSEI))
    892 				break;
    893 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
    894 				/*
    895 				 * Got `invalid command'; we interpret it
    896 				 * to mean that the re-calibrate hasn't in
    897 				 * fact finished yet
    898 				 */
    899 				continue;
    900 			break;
    901 		}
    902 	}
    903 	n = fdc->sc_nstat;
    904 #ifdef FD_DEBUG
    905 	if (fdc_debug) {
    906 		int i;
    907 		printf("fdprobe: %d stati:", n);
    908 		for (i = 0; i < n; i++)
    909 			printf(" 0x%x", fdc->sc_status[i]);
    910 		printf("\n");
    911 	}
    912 #endif
    913 	ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
    914 
    915 	/* turn off motor */
    916 	if ((fdc->sc_flags & FDC_82077) != 0) {
    917 		/* deselect drive and turn motor off */
    918 		bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
    919 #ifdef SUN4
    920 	} else {
    921 		auxregbisc(0, AUXIO4C_FDS);
    922 #endif
    923 	}
    924 
    925 	return ok;
    926 }
    927 
    928 /*
    929  * Controller is working, and drive responded.  Attach it.
    930  */
    931 void
    932 fdattach(struct device *parent, struct device *self, void *aux)
    933 {
    934 	struct fdc_softc *fdc = (void *)parent;
    935 	struct fd_softc *fd = (void *)self;
    936 	struct fdc_attach_args *fa = aux;
    937 	struct fd_type *type = fa->fa_deftype;
    938 	int drive = fa->fa_drive;
    939 
    940 	callout_init(&fd->sc_motoron_ch, 0);
    941 	callout_init(&fd->sc_motoroff_ch, 0);
    942 
    943 	/* XXX Allow `flags' to override device type? */
    944 
    945 	if (type)
    946 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    947 		    type->cylinders, type->heads, type->sectrac);
    948 	else
    949 		printf(": density unknown\n");
    950 
    951 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    952 	fd->sc_cylin = -1;
    953 	fd->sc_drive = drive;
    954 	fd->sc_deftype = type;
    955 	fdc->sc_fd[drive] = fd;
    956 
    957 	fdc_wrfifo(fdc, NE7CMD_SPECIFY);
    958 	fdc_wrfifo(fdc, type->steprate);
    959 	/* XXX head load time == 6ms */
    960 	fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
    961 
    962 	/*
    963 	 * Initialize and attach the disk structure.
    964 	 */
    965 	disk_init(&fd->sc_dk, device_xname(&fd->sc_dv), &fddkdriver);
    966 	disk_attach(&fd->sc_dk);
    967 
    968 	/*
    969 	 * Establish a mountroot_hook anyway in case we booted
    970 	 * with RB_ASKNAME and get selected as the boot device.
    971 	 */
    972 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
    973 
    974 	fd_set_properties(fd);
    975 
    976 	/* Make sure the drive motor gets turned off at shutdown time. */
    977 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
    978 }
    979 
    980 inline struct fd_type *
    981 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
    982 {
    983 	int type = FDTYPE(dev);
    984 
    985 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    986 		return NULL;
    987 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    988 }
    989 
    990 void
    991 fdstrategy(struct buf *bp)
    992 {
    993 	struct fd_softc *fd;
    994 	int unit = FDUNIT(bp->b_dev);
    995 	int sz;
    996  	int s;
    997 
    998 	/* Valid unit, controller, and request? */
    999 	if (unit >= fd_cd.cd_ndevs ||
   1000 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
   1001 	    bp->b_blkno < 0 ||
   1002 	    (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
   1003 	      (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
   1004 	     (bp->b_flags & B_FORMAT) == 0)) {
   1005 		bp->b_error = EINVAL;
   1006 		goto done;
   1007 	}
   1008 
   1009 	/* If it's a null transfer, return immediately. */
   1010 	if (bp->b_bcount == 0)
   1011 		goto done;
   1012 
   1013 	sz = howmany(bp->b_bcount, DEV_BSIZE);
   1014 
   1015 	if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
   1016 		sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
   1017 		     - bp->b_blkno;
   1018 		if (sz == 0) {
   1019 			/* If exactly at end of disk, return EOF. */
   1020 			bp->b_resid = bp->b_bcount;
   1021 			goto done;
   1022 		}
   1023 		if (sz < 0) {
   1024 			/* If past end of disk, return EINVAL. */
   1025 			bp->b_error = EINVAL;
   1026 			goto done;
   1027 		}
   1028 		/* Otherwise, truncate request. */
   1029 		bp->b_bcount = sz << DEV_BSHIFT;
   1030 	}
   1031 
   1032 	bp->b_rawblkno = bp->b_blkno;
   1033  	bp->b_cylinder = (bp->b_blkno * DEV_BSIZE) /
   1034 		      (FD_BSIZE(fd) * fd->sc_type->seccyl);
   1035 
   1036 #ifdef FD_DEBUG
   1037 	if (fdc_debug > 1)
   1038 	    printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld cylin %d sz %d\n",
   1039 		    (long long)bp->b_blkno, bp->b_bcount,
   1040 		    (long long)fd->sc_blkno, bp->b_cylinder, sz);
   1041 #endif
   1042 
   1043 	/* Queue transfer on drive, activate drive and controller if idle. */
   1044 	s = splbio();
   1045 	BUFQ_PUT(fd->sc_q, bp);
   1046 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
   1047 	if (fd->sc_active == 0)
   1048 		fdstart(fd);
   1049 #ifdef DIAGNOSTIC
   1050 	else {
   1051 		struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1052 		if (fdc->sc_state == DEVIDLE) {
   1053 			printf("fdstrategy: controller inactive\n");
   1054 			fdcstart(fdc);
   1055 		}
   1056 	}
   1057 #endif
   1058 	splx(s);
   1059 	return;
   1060 
   1061 done:
   1062 	/* Toss transfer; we're done early. */
   1063 	biodone(bp);
   1064 }
   1065 
   1066 void
   1067 fdstart(struct fd_softc *fd)
   1068 {
   1069 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1070 	int active = fdc->sc_drives.tqh_first != 0;
   1071 
   1072 	/* Link into controller queue. */
   1073 	fd->sc_active = 1;
   1074 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
   1075 
   1076 	/* If controller not already active, start it. */
   1077 	if (!active)
   1078 		fdcstart(fdc);
   1079 }
   1080 
   1081 void
   1082 fdfinish(struct fd_softc *fd, struct buf *bp)
   1083 {
   1084 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1085 
   1086 	/*
   1087 	 * Move this drive to the end of the queue to give others a `fair'
   1088 	 * chance.  We only force a switch if N operations are completed while
   1089 	 * another drive is waiting to be serviced, since there is a long motor
   1090 	 * startup delay whenever we switch.
   1091 	 */
   1092 	(void)BUFQ_GET(fd->sc_q);
   1093 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
   1094 		fd->sc_ops = 0;
   1095 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1096 		if (BUFQ_PEEK(fd->sc_q) != NULL) {
   1097 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
   1098 		} else
   1099 			fd->sc_active = 0;
   1100 	}
   1101 	bp->b_resid = fd->sc_bcount;
   1102 	fd->sc_skip = 0;
   1103 
   1104 	biodone(bp);
   1105 	/* turn off motor 5s from now */
   1106 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
   1107 	fdc->sc_state = DEVIDLE;
   1108 }
   1109 
   1110 void
   1111 fdc_reset(struct fdc_softc *fdc)
   1112 {
   1113 	bus_space_tag_t t = fdc->sc_bustag;
   1114 	bus_space_handle_t h = fdc->sc_handle;
   1115 
   1116 	if ((fdc->sc_flags & FDC_82077) != 0) {
   1117 		bus_space_write_1(t, h, fdc->sc_reg_dor,
   1118 				  FDO_FDMAEN | FDO_MOEN(0));
   1119 	}
   1120 
   1121 	bus_space_write_1(t, h, fdc->sc_reg_drs, DRS_RESET);
   1122 	delay(10);
   1123 	bus_space_write_1(t, h, fdc->sc_reg_drs, 0);
   1124 
   1125 	if ((fdc->sc_flags & FDC_82077) != 0) {
   1126 		bus_space_write_1(t, h, fdc->sc_reg_dor,
   1127 				  FDO_FRST | FDO_FDMAEN | FDO_DS);
   1128 	}
   1129 #ifdef FD_DEBUG
   1130 	if (fdc_debug)
   1131 		printf("fdc reset\n");
   1132 #endif
   1133 }
   1134 
   1135 void
   1136 fd_set_motor(struct fdc_softc *fdc)
   1137 {
   1138 	struct fd_softc *fd;
   1139 	u_char status;
   1140 	int n;
   1141 
   1142 	if ((fdc->sc_flags & FDC_82077) != 0) {
   1143 		status = FDO_FRST | FDO_FDMAEN;
   1144 		if ((fd = fdc->sc_drives.tqh_first) != NULL)
   1145 			status |= fd->sc_drive;
   1146 
   1147 		for (n = 0; n < 4; n++)
   1148 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
   1149 				status |= FDO_MOEN(n);
   1150 		bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
   1151 				  fdc->sc_reg_dor, status);
   1152 #ifdef SUN4
   1153 	} else {
   1154 
   1155 		for (n = 0; n < 4; n++) {
   1156 			if ((fd = fdc->sc_fd[n]) != NULL  &&
   1157 			    (fd->sc_flags & FD_MOTOR) != 0) {
   1158 				auxregbisc(AUXIO4C_FDS, 0);
   1159 				return;
   1160 			}
   1161 		}
   1162 		auxregbisc(0, AUXIO4C_FDS);
   1163 #endif
   1164 	}
   1165 }
   1166 
   1167 void
   1168 fd_motor_off(void *arg)
   1169 {
   1170 	struct fd_softc *fd = arg;
   1171 	int s;
   1172 
   1173 	s = splbio();
   1174 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1175 	fd_set_motor((struct fdc_softc *)device_parent(&fd->sc_dv));
   1176 	splx(s);
   1177 }
   1178 
   1179 void
   1180 fd_motor_on(void *arg)
   1181 {
   1182 	struct fd_softc *fd = arg;
   1183 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1184 	int s;
   1185 
   1186 	s = splbio();
   1187 	fd->sc_flags &= ~FD_MOTOR_WAIT;
   1188 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
   1189 		(void)fdcstate(fdc);
   1190 	splx(s);
   1191 }
   1192 
   1193 /*
   1194  * Get status bytes off the FDC after a command has finished
   1195  * Returns the number of status bytes read; -1 on error.
   1196  * The return value is also stored in `sc_nstat'.
   1197  */
   1198 int
   1199 fdcresult(struct fdc_softc *fdc)
   1200 {
   1201 	bus_space_tag_t t = fdc->sc_bustag;
   1202 	bus_space_handle_t h = fdc->sc_handle;
   1203 	int j, n = 0;
   1204 
   1205 	for (j = 10000; j; j--) {
   1206 		uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
   1207 		v &= (NE7_DIO | NE7_RQM | NE7_CB);
   1208 		if (v == NE7_RQM)
   1209 			return fdc->sc_nstat = n;
   1210 		if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
   1211 			if (n >= sizeof(fdc->sc_status)) {
   1212 				log(LOG_ERR, "fdcresult: overrun\n");
   1213 				return -1;
   1214 			}
   1215 			fdc->sc_status[n++] =
   1216 				bus_space_read_1(t, h, fdc->sc_reg_fifo);
   1217 		} else
   1218 			delay(1);
   1219 	}
   1220 
   1221 	log(LOG_ERR, "fdcresult: timeout\n");
   1222 	return fdc->sc_nstat = -1;
   1223 }
   1224 
   1225 /*
   1226  * Write a command byte to the FDC.
   1227  * Returns 0 on success; -1 on failure (i.e. timeout)
   1228  */
   1229 int
   1230 fdc_wrfifo(struct fdc_softc *fdc, uint8_t x)
   1231 {
   1232 	bus_space_tag_t t = fdc->sc_bustag;
   1233 	bus_space_handle_t h = fdc->sc_handle;
   1234 	int i;
   1235 
   1236 	for (i = 100000; i-- > 0;) {
   1237 		uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
   1238 		if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
   1239 			/* The chip is ready */
   1240 			bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
   1241 			return 0;
   1242 		}
   1243 		delay(1);
   1244 	}
   1245 	return -1;
   1246 }
   1247 
   1248 int
   1249 fdc_diskchange(struct fdc_softc *fdc)
   1250 {
   1251 
   1252 #ifdef SUN4
   1253 	if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
   1254 #endif
   1255 		bus_space_tag_t t = fdc->sc_bustag;
   1256 		bus_space_handle_t h = fdc->sc_handle;
   1257 		uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_dir);
   1258 		return (v & FDI_DCHG) != 0;
   1259 #ifdef SUN4
   1260 	} else if (CPU_ISSUN4C) {
   1261 		return (*AUXIO4C_REG & AUXIO4C_FDC) != 0;
   1262 	}
   1263 	return 0;
   1264 #endif
   1265 }
   1266 
   1267 int
   1268 fdopen(dev_t dev, int flags, int fmt, struct lwp *l)
   1269 {
   1270  	int unit, pmask;
   1271 	struct fd_softc *fd;
   1272 	struct fd_type *type;
   1273 
   1274 	unit = FDUNIT(dev);
   1275 	if (unit >= fd_cd.cd_ndevs)
   1276 		return ENXIO;
   1277 	fd = fd_cd.cd_devs[unit];
   1278 	if (fd == NULL)
   1279 		return ENXIO;
   1280 	type = fd_dev_to_type(fd, dev);
   1281 	if (type == NULL)
   1282 		return ENXIO;
   1283 
   1284 	if ((fd->sc_flags & FD_OPEN) != 0 &&
   1285 	    fd->sc_type != type)
   1286 		return EBUSY;
   1287 
   1288 	fd->sc_type = type;
   1289 	fd->sc_cylin = -1;
   1290 	fd->sc_flags |= FD_OPEN;
   1291 
   1292 	/*
   1293 	 * Only update the disklabel if we're not open anywhere else.
   1294 	 */
   1295 	if (fd->sc_dk.dk_openmask == 0)
   1296 		fdgetdisklabel(dev);
   1297 
   1298 	pmask = (1 << DISKPART(dev));
   1299 
   1300 	switch (fmt) {
   1301 	case S_IFCHR:
   1302 		fd->sc_dk.dk_copenmask |= pmask;
   1303 		break;
   1304 
   1305 	case S_IFBLK:
   1306 		fd->sc_dk.dk_bopenmask |= pmask;
   1307 		break;
   1308 	}
   1309 	fd->sc_dk.dk_openmask =
   1310 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
   1311 
   1312 	return 0;
   1313 }
   1314 
   1315 int
   1316 fdclose(dev_t dev, int flags, int fmt, struct lwp *l)
   1317 {
   1318 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   1319 	int pmask = (1 << DISKPART(dev));
   1320 
   1321 	fd->sc_flags &= ~FD_OPEN;
   1322 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
   1323 
   1324 	switch (fmt) {
   1325 	case S_IFCHR:
   1326 		fd->sc_dk.dk_copenmask &= ~pmask;
   1327 		break;
   1328 
   1329 	case S_IFBLK:
   1330 		fd->sc_dk.dk_bopenmask &= ~pmask;
   1331 		break;
   1332 	}
   1333 	fd->sc_dk.dk_openmask =
   1334 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
   1335 
   1336 	return 0;
   1337 }
   1338 
   1339 int
   1340 fdread(dev_t dev, struct uio *uio, int flag)
   1341 {
   1342 
   1343         return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
   1344 }
   1345 
   1346 int
   1347 fdwrite(dev_t dev, struct uio *uio, int flag)
   1348 {
   1349 
   1350         return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
   1351 }
   1352 
   1353 void
   1354 fdcstart(struct fdc_softc *fdc)
   1355 {
   1356 
   1357 #ifdef DIAGNOSTIC
   1358 	/* only got here if controller's drive queue was inactive; should
   1359 	   be in idle state */
   1360 	if (fdc->sc_state != DEVIDLE) {
   1361 		printf("fdcstart: not idle\n");
   1362 		return;
   1363 	}
   1364 #endif
   1365 	(void)fdcstate(fdc);
   1366 }
   1367 
   1368 void
   1369 fdcstatus(struct fdc_softc *fdc, const char *s)
   1370 {
   1371 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
   1372 	int n;
   1373 	char bits[64];
   1374 
   1375 	/* Just print last status */
   1376 	n = fdc->sc_nstat;
   1377 
   1378 #if 0
   1379 	/*
   1380 	 * A 82072 seems to return <invalid command> on
   1381 	 * gratuitous Sense Interrupt commands.
   1382 	 */
   1383 	if (n == 0 && (fdc->sc_flags & FDC_82077) != 0) {
   1384 		fdc_wrfifo(fdc, NE7CMD_SENSEI);
   1385 		(void)fdcresult(fdc);
   1386 		n = 2;
   1387 	}
   1388 #endif
   1389 
   1390 	printf("%s: %s: state %d",
   1391 		fd ? device_xname(&fd->sc_dv) : "fdc", s, fdc->sc_state);
   1392 
   1393 	switch (n) {
   1394 	case 0:
   1395 		printf("\n");
   1396 		break;
   1397 	case 2:
   1398 		printf(" (st0 %s cyl %d)\n",
   1399 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
   1400 		    bits, sizeof(bits)), fdc->sc_status[1]);
   1401 		break;
   1402 	case 7:
   1403 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
   1404 		    NE7_ST0BITS, bits, sizeof(bits)));
   1405 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
   1406 		    NE7_ST1BITS, bits, sizeof(bits)));
   1407 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
   1408 		    NE7_ST2BITS, bits, sizeof(bits)));
   1409 		printf(" cyl %d head %d sec %d)\n",
   1410 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1411 		break;
   1412 #ifdef DIAGNOSTIC
   1413 	default:
   1414 		printf(" fdcstatus: weird size: %d\n", n);
   1415 		break;
   1416 #endif
   1417 	}
   1418 }
   1419 
   1420 void
   1421 fdctimeout(void *arg)
   1422 {
   1423 	struct fdc_softc *fdc = arg;
   1424 	struct fd_softc *fd;
   1425 	int s;
   1426 
   1427 	s = splbio();
   1428 	fd = fdc->sc_drives.tqh_first;
   1429 	if (fd == NULL) {
   1430 		aprint_error_dev(&fdc->sc_dev, "timeout but no I/O pending: state %d, istatus=%d\n",
   1431 			fdc->sc_state, fdc->sc_istatus);
   1432 		fdc->sc_state = DEVIDLE;
   1433 		goto out;
   1434 	}
   1435 
   1436 	if (BUFQ_PEEK(fd->sc_q) != NULL)
   1437 		fdc->sc_state++;
   1438 	else
   1439 		fdc->sc_state = DEVIDLE;
   1440 
   1441 	(void)fdcstate(fdc);
   1442 out:
   1443 	splx(s);
   1444 
   1445 }
   1446 
   1447 void
   1448 fdcpseudointr(void *arg)
   1449 {
   1450 	struct fdc_softc *fdc = arg;
   1451 	int s;
   1452 
   1453 	/* Just ensure it has the right spl. */
   1454 	s = splbio();
   1455 	(void)fdcstate(fdc);
   1456 	splx(s);
   1457 }
   1458 
   1459 
   1460 /*
   1461  * hardware interrupt entry point: used only if no `fast trap' * (in-window)
   1462  * handler is available. Unfortunately, we have no reliable way to
   1463  * determine that the interrupt really came from the floppy controller;
   1464  * just hope that the other devices that share this interrupt level
   1465  * can do better..
   1466  */
   1467 int
   1468 fdc_c_hwintr(void *arg)
   1469 {
   1470 	struct fdc_softc *fdc = arg;
   1471 	bus_space_tag_t t = fdc->sc_bustag;
   1472 	bus_space_handle_t h = fdc->sc_handle;
   1473 
   1474 	switch (fdc->sc_itask) {
   1475 	case FDC_ITASK_NONE:
   1476 		return 0;
   1477 	case FDC_ITASK_SENSEI:
   1478 		if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
   1479 			fdc->sc_istatus = FDC_ISTATUS_ERROR;
   1480 		else
   1481 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1482 		softint_schedule(fdc->sc_sicookie);
   1483 		return 1;
   1484 	case FDC_ITASK_RESULT:
   1485 		if (fdcresult(fdc) == -1)
   1486 			fdc->sc_istatus = FDC_ISTATUS_ERROR;
   1487 		else
   1488 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1489 		softint_schedule(fdc->sc_sicookie);
   1490 		return 1;
   1491 	case FDC_ITASK_DMA:
   1492 		/* Proceed with pseudo-DMA below */
   1493 		break;
   1494 	default:
   1495 		printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
   1496 		fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
   1497 		softint_schedule(fdc->sc_sicookie);
   1498 		return 1;
   1499 	}
   1500 
   1501 	/*
   1502 	 * Pseudo DMA in progress
   1503 	 */
   1504 	for (;;) {
   1505 		uint8_t msr;
   1506 
   1507 		msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
   1508 
   1509 		if ((msr & NE7_RQM) == 0)
   1510 			/* That's all this round. */
   1511 			break;
   1512 
   1513 		if ((msr & NE7_NDM) == 0) {
   1514 			/* Execution phase finished, get result. */
   1515 			fdcresult(fdc);
   1516 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1517 			softint_schedule(fdc->sc_sicookie);
   1518 			break;
   1519 		}
   1520 
   1521 		if (fdc->sc_tc == 0)
   1522 			/* For some reason the controller wants to transfer
   1523 			   more data then what we want to transfer. */
   1524 			panic("fdc: overrun");
   1525 
   1526 		/* Another byte can be transferred */
   1527 		if ((msr & NE7_DIO) != 0)
   1528 			*fdc->sc_data =
   1529 				bus_space_read_1(t, h, fdc->sc_reg_fifo);
   1530 		else
   1531 			bus_space_write_1(t, h, fdc->sc_reg_fifo,
   1532 					  *fdc->sc_data);
   1533 
   1534 		fdc->sc_data++;
   1535 		if (--fdc->sc_tc == 0) {
   1536 			FTC_FLIP;
   1537 			break;
   1538 		}
   1539 	}
   1540 	return 1;
   1541 }
   1542 
   1543 void
   1544 fdcswintr(void *arg)
   1545 {
   1546 	struct fdc_softc *fdc = arg;
   1547 
   1548 	if (fdc->sc_istatus == FDC_ISTATUS_NONE)
   1549 		/* This (software) interrupt is not for us */
   1550 		return;
   1551 
   1552 	switch (fdc->sc_istatus) {
   1553 	case FDC_ISTATUS_ERROR:
   1554 		printf("fdc: ierror status: state %d\n", fdc->sc_state);
   1555 		break;
   1556 	case FDC_ISTATUS_SPURIOUS:
   1557 		printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
   1558 		break;
   1559 	}
   1560 
   1561 	fdcstate(fdc);
   1562 	return;
   1563 }
   1564 
   1565 int
   1566 fdcstate(struct fdc_softc *fdc)
   1567 {
   1568 
   1569 #define	st0	fdc->sc_status[0]
   1570 #define	st1	fdc->sc_status[1]
   1571 #define	cyl	fdc->sc_status[1]
   1572 #define FDC_WRFIFO(fdc, c) do {			\
   1573 	if (fdc_wrfifo(fdc, (c))) {		\
   1574 		goto xxx;			\
   1575 	}					\
   1576 } while(0)
   1577 
   1578 	struct fd_softc *fd;
   1579 	struct buf *bp;
   1580 	int read, head, sec, nblks;
   1581 	struct fd_type *type;
   1582 	struct ne7_fd_formb *finfo = NULL;
   1583 
   1584 	if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
   1585 		/* Prevent loop if the reset sequence produces errors */
   1586 		if (fdc->sc_state != RESETCOMPLETE &&
   1587 		    fdc->sc_state != RECALWAIT &&
   1588 		    fdc->sc_state != RECALCOMPLETE)
   1589 			fdc->sc_state = DORESET;
   1590 	}
   1591 
   1592 	/* Clear I task/status field */
   1593 	fdc->sc_istatus = FDC_ISTATUS_NONE;
   1594 	fdc->sc_itask = FDC_ITASK_NONE;
   1595 
   1596 loop:
   1597 	/* Is there a drive for the controller to do a transfer with? */
   1598 	fd = fdc->sc_drives.tqh_first;
   1599 	if (fd == NULL) {
   1600 		fdc->sc_state = DEVIDLE;
   1601  		return 0;
   1602 	}
   1603 
   1604 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1605 	bp = BUFQ_PEEK(fd->sc_q);
   1606 	if (bp == NULL) {
   1607 		fd->sc_ops = 0;
   1608 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1609 		fd->sc_active = 0;
   1610 		goto loop;
   1611 	}
   1612 
   1613 	if (bp->b_flags & B_FORMAT)
   1614 		finfo = (struct ne7_fd_formb *)bp->b_data;
   1615 
   1616 	switch (fdc->sc_state) {
   1617 	case DEVIDLE:
   1618 		fdc->sc_errors = 0;
   1619 		fd->sc_skip = 0;
   1620 		fd->sc_bcount = bp->b_bcount;
   1621 		fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
   1622 		callout_stop(&fd->sc_motoroff_ch);
   1623 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1624 			fdc->sc_state = MOTORWAIT;
   1625 			return 1;
   1626 		}
   1627 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1628 			/* Turn on the motor, being careful about pairing. */
   1629 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1630 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1631 				callout_stop(&ofd->sc_motoroff_ch);
   1632 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1633 			}
   1634 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1635 			fd_set_motor(fdc);
   1636 			fdc->sc_state = MOTORWAIT;
   1637 			if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) { /*XXX*/
   1638 				/* Allow .25s for motor to stabilize. */
   1639 				callout_reset(&fd->sc_motoron_ch, hz / 4,
   1640 				    fd_motor_on, fd);
   1641 			} else {
   1642 				fd->sc_flags &= ~FD_MOTOR_WAIT;
   1643 				goto loop;
   1644 			}
   1645 			return 1;
   1646 		}
   1647 		/* Make sure the right drive is selected. */
   1648 		fd_set_motor(fdc);
   1649 
   1650 		if (fdc_diskchange(fdc))
   1651 			goto dodskchg;
   1652 
   1653 		/*FALLTHROUGH*/
   1654 	case DOSEEK:
   1655 	doseek:
   1656 		if ((fdc->sc_flags & FDC_EIS) &&
   1657 		    (bp->b_flags & B_FORMAT) == 0) {
   1658 			fd->sc_cylin = bp->b_cylinder;
   1659 			/* We use implied seek */
   1660 			goto doio;
   1661 		}
   1662 
   1663 		if (fd->sc_cylin == bp->b_cylinder)
   1664 			goto doio;
   1665 
   1666 		fd->sc_cylin = -1;
   1667 		fdc->sc_state = SEEKWAIT;
   1668 		fdc->sc_nstat = 0;
   1669 
   1670 		iostat_seek(fd->sc_dk.dk_stats);
   1671 
   1672 		disk_busy(&fd->sc_dk);
   1673 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1674 
   1675 		/* specify command */
   1676 		FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
   1677 		FDC_WRFIFO(fdc, fd->sc_type->steprate);
   1678 		/* XXX head load time == 6ms */
   1679 		FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
   1680 
   1681 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1682 		/* seek function */
   1683 		FDC_WRFIFO(fdc, NE7CMD_SEEK);
   1684 		FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
   1685 		FDC_WRFIFO(fdc, bp->b_cylinder * fd->sc_type->step);
   1686 		return 1;
   1687 
   1688 	case DODSKCHG:
   1689 	dodskchg:
   1690 		/*
   1691 		 * Disk change: force a seek operation by going to cyl 1
   1692 		 * followed by a recalibrate.
   1693 		 */
   1694 		disk_busy(&fd->sc_dk);
   1695 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1696 		fd->sc_cylin = -1;
   1697 		fdc->sc_nstat = 0;
   1698 		fdc->sc_state = DSKCHGWAIT;
   1699 
   1700 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1701 		/* seek function */
   1702 		FDC_WRFIFO(fdc, NE7CMD_SEEK);
   1703 		FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
   1704 		FDC_WRFIFO(fdc, 1 * fd->sc_type->step);
   1705 		return 1;
   1706 
   1707 	case DSKCHGWAIT:
   1708 		callout_stop(&fdc->sc_timo_ch);
   1709 		disk_unbusy(&fd->sc_dk, 0, 0);
   1710 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1711 		    cyl != 1 * fd->sc_type->step) {
   1712 			fdcstatus(fdc, "dskchg seek failed");
   1713 			fdc->sc_state = DORESET;
   1714 		} else
   1715 			fdc->sc_state = DORECAL;
   1716 
   1717 		if (fdc_diskchange(fdc)) {
   1718 			aprint_error_dev(&fdc->sc_dev, "cannot clear disk change status\n");
   1719 			fdc->sc_state = DORESET;
   1720 		}
   1721 		goto loop;
   1722 
   1723 	case DOIO:
   1724 	doio:
   1725 		if (finfo != NULL)
   1726 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
   1727 				      (char *)finfo;
   1728 		type = fd->sc_type;
   1729 		sec = fd->sc_blkno % type->seccyl;
   1730 		nblks = type->seccyl - sec;
   1731 		nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
   1732 		nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
   1733 		fd->sc_nblks = nblks;
   1734 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
   1735 		head = sec / type->sectrac;
   1736 		sec -= head * type->sectrac;
   1737 #ifdef DIAGNOSTIC
   1738 		{int block;
   1739 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac +
   1740 			 sec;
   1741 		 if (block != fd->sc_blkno) {
   1742 			 printf("fdcintr: block %d != blkno %d\n", block,
   1743 			        (int)fd->sc_blkno);
   1744 #ifdef DDB
   1745 			 Debugger();
   1746 #endif
   1747 		 }}
   1748 #endif
   1749 		read = bp->b_flags & B_READ;
   1750 
   1751 		/* Setup for pseudo DMA */
   1752 		fdc->sc_data = (char *)bp->b_data + fd->sc_skip;
   1753 		fdc->sc_tc = fd->sc_nbytes;
   1754 
   1755 		bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
   1756 				  fdc->sc_reg_drs, type->rate);
   1757 #ifdef FD_DEBUG
   1758 		if (fdc_debug > 1)
   1759 			printf("fdcstate: doio: %s drive %d "
   1760 				"track %d head %d sec %d nblks %d\n",
   1761 				finfo ? "format" :
   1762 					(read ? "read" : "write"),
   1763 				fd->sc_drive, fd->sc_cylin, head, sec, nblks);
   1764 #endif
   1765 		fdc->sc_state = IOCOMPLETE;
   1766 		fdc->sc_itask = FDC_ITASK_DMA;
   1767 		fdc->sc_nstat = 0;
   1768 
   1769 		disk_busy(&fd->sc_dk);
   1770 
   1771 		/* allow 3 seconds for operation */
   1772 		callout_reset(&fdc->sc_timo_ch, 3 * hz, fdctimeout, fdc);
   1773 
   1774 		if (finfo != NULL) {
   1775 			/* formatting */
   1776 			FDC_WRFIFO(fdc, NE7CMD_FORMAT);
   1777 			FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
   1778 			FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
   1779 			FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
   1780 			FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
   1781 			FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
   1782 		} else {
   1783 			if (read)
   1784 				FDC_WRFIFO(fdc, NE7CMD_READ);
   1785 			else
   1786 				FDC_WRFIFO(fdc, NE7CMD_WRITE);
   1787 			FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
   1788 			FDC_WRFIFO(fdc, fd->sc_cylin);	/*track*/
   1789 			FDC_WRFIFO(fdc, head);
   1790 			FDC_WRFIFO(fdc, sec + 1);	/*sector+1*/
   1791 			FDC_WRFIFO(fdc, type->secsize);	/*sector size*/
   1792 			FDC_WRFIFO(fdc, type->sectrac);	/*secs/track*/
   1793 			FDC_WRFIFO(fdc, type->gap1);	/*gap1 size*/
   1794 			FDC_WRFIFO(fdc, type->datalen);	/*data length*/
   1795 		}
   1796 
   1797 		return 1;				/* will return later */
   1798 
   1799 	case SEEKWAIT:
   1800 		callout_stop(&fdc->sc_timo_ch);
   1801 		fdc->sc_state = SEEKCOMPLETE;
   1802 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1803 			/* allow 1/50 second for heads to settle */
   1804 			callout_reset(&fdc->sc_intr_ch, hz / 50,
   1805 			    fdcpseudointr, fdc);
   1806 			return 1;		/* will return later */
   1807 		}
   1808 		/*FALLTHROUGH*/
   1809 	case SEEKCOMPLETE:
   1810 		/* no data on seek */
   1811 		disk_unbusy(&fd->sc_dk, 0, 0);
   1812 
   1813 		/* Make sure seek really happened. */
   1814 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1815 		    cyl != bp->b_cylinder * fd->sc_type->step) {
   1816 #ifdef FD_DEBUG
   1817 			if (fdc_debug)
   1818 				fdcstatus(fdc, "seek failed");
   1819 #endif
   1820 			fdcretry(fdc);
   1821 			goto loop;
   1822 		}
   1823 		fd->sc_cylin = bp->b_cylinder;
   1824 		goto doio;
   1825 
   1826 	case IOTIMEDOUT:
   1827 		/*
   1828 		 * Try to abort the I/O operation without resetting
   1829 		 * the chip first.  Poke TC and arrange to pick up
   1830 		 * the timed out I/O command's status.
   1831 		 */
   1832 		fdc->sc_itask = FDC_ITASK_RESULT;
   1833 		fdc->sc_state = IOCLEANUPWAIT;
   1834 		fdc->sc_nstat = 0;
   1835 		/* 1/10 second should be enough */
   1836 		callout_reset(&fdc->sc_timo_ch, hz / 10, fdctimeout, fdc);
   1837 		FTC_FLIP;
   1838 		return 1;
   1839 
   1840 	case IOCLEANUPTIMEDOUT:
   1841 	case SEEKTIMEDOUT:
   1842 	case RECALTIMEDOUT:
   1843 	case RESETTIMEDOUT:
   1844 	case DSKCHGTIMEDOUT:
   1845 		fdcstatus(fdc, "timeout");
   1846 
   1847 		/* All other timeouts always roll through to a chip reset */
   1848 		fdcretry(fdc);
   1849 
   1850 		/* Force reset, no matter what fdcretry() says */
   1851 		fdc->sc_state = DORESET;
   1852 		goto loop;
   1853 
   1854 	case IOCLEANUPWAIT: /* IO FAILED, cleanup succeeded */
   1855 		callout_stop(&fdc->sc_timo_ch);
   1856 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
   1857 		    (bp->b_flags & B_READ));
   1858 		fdcretry(fdc);
   1859 		goto loop;
   1860 
   1861 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1862 		callout_stop(&fdc->sc_timo_ch);
   1863 
   1864 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
   1865 		    (bp->b_flags & B_READ));
   1866 
   1867 		if (fdc->sc_nstat != 7 || st1 != 0 ||
   1868 		    ((st0 & 0xf8) != 0 &&
   1869 		     ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
   1870 #ifdef FD_DEBUG
   1871 			if (fdc_debug) {
   1872 				fdcstatus(fdc, bp->b_flags & B_READ ?
   1873 				     "read failed" : "write failed");
   1874 				printf("blkno %lld nblks %d nstat %d tc %d\n",
   1875 				       (long long)fd->sc_blkno, fd->sc_nblks,
   1876 				       fdc->sc_nstat, fdc->sc_tc);
   1877 			}
   1878 #endif
   1879 			if (fdc->sc_nstat == 7 &&
   1880 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
   1881 
   1882 				/*
   1883 				 * Silently retry overruns if no other
   1884 				 * error bit is set. Adjust threshold.
   1885 				 */
   1886 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1887 				if (thr < 15) {
   1888 					thr++;
   1889 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1890 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1891 #ifdef FD_DEBUG
   1892 					if (fdc_debug)
   1893 						printf("fdc: %d -> threshold\n",
   1894 						       thr);
   1895 #endif
   1896 					fdconf(fdc);
   1897 					fdc->sc_overruns = 0;
   1898 				}
   1899 				if (++fdc->sc_overruns < 3) {
   1900 					fdc->sc_state = DOIO;
   1901 					goto loop;
   1902 				}
   1903 			}
   1904 			fdcretry(fdc);
   1905 			goto loop;
   1906 		}
   1907 		if (fdc->sc_errors) {
   1908 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1909 			    fd->sc_skip / FD_BSIZE(fd),
   1910 			    (struct disklabel *)NULL);
   1911 			printf("\n");
   1912 			fdc->sc_errors = 0;
   1913 		} else {
   1914 			if (--fdc->sc_overruns < -20) {
   1915 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1916 				if (thr > 0) {
   1917 					thr--;
   1918 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1919 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1920 #ifdef FD_DEBUG
   1921 					if (fdc_debug)
   1922 						printf("fdc: %d -> threshold\n",
   1923 						       thr);
   1924 #endif
   1925 					fdconf(fdc);
   1926 				}
   1927 				fdc->sc_overruns = 0;
   1928 			}
   1929 		}
   1930 		fd->sc_blkno += fd->sc_nblks;
   1931 		fd->sc_skip += fd->sc_nbytes;
   1932 		fd->sc_bcount -= fd->sc_nbytes;
   1933 		if (finfo == NULL && fd->sc_bcount > 0) {
   1934 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
   1935 			goto doseek;
   1936 		}
   1937 		fdfinish(fd, bp);
   1938 		goto loop;
   1939 
   1940 	case DORESET:
   1941 		/* try a reset, keep motor on */
   1942 		fd_set_motor(fdc);
   1943 		delay(100);
   1944 		fdc->sc_nstat = 0;
   1945 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1946 		fdc->sc_state = RESETCOMPLETE;
   1947 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
   1948 		fdc_reset(fdc);
   1949 		return 1;			/* will return later */
   1950 
   1951 	case RESETCOMPLETE:
   1952 		callout_stop(&fdc->sc_timo_ch);
   1953 		fdconf(fdc);
   1954 
   1955 		/* FALLTHROUGH */
   1956 	case DORECAL:
   1957 		fdc->sc_state = RECALWAIT;
   1958 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1959 		fdc->sc_nstat = 0;
   1960 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
   1961 		/* recalibrate function */
   1962 		FDC_WRFIFO(fdc, NE7CMD_RECAL);
   1963 		FDC_WRFIFO(fdc, fd->sc_drive);
   1964 		return 1;			/* will return later */
   1965 
   1966 	case RECALWAIT:
   1967 		callout_stop(&fdc->sc_timo_ch);
   1968 		fdc->sc_state = RECALCOMPLETE;
   1969 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1970 			/* allow 1/30 second for heads to settle */
   1971 			callout_reset(&fdc->sc_intr_ch, hz / 30,
   1972 			    fdcpseudointr, fdc);
   1973 			return 1;		/* will return later */
   1974 		}
   1975 
   1976 	case RECALCOMPLETE:
   1977 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1978 #ifdef FD_DEBUG
   1979 			if (fdc_debug)
   1980 				fdcstatus(fdc, "recalibrate failed");
   1981 #endif
   1982 			fdcretry(fdc);
   1983 			goto loop;
   1984 		}
   1985 		fd->sc_cylin = 0;
   1986 		goto doseek;
   1987 
   1988 	case MOTORWAIT:
   1989 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1990 			return 1;		/* time's not up yet */
   1991 		goto doseek;
   1992 
   1993 	default:
   1994 		fdcstatus(fdc, "stray interrupt");
   1995 		return 1;
   1996 	}
   1997 #ifdef DIAGNOSTIC
   1998 	panic("fdcintr: impossible");
   1999 #endif
   2000 
   2001 xxx:
   2002 	/*
   2003 	 * We get here if the chip locks up in FDC_WRFIFO()
   2004 	 * Cancel any operation and schedule a reset
   2005 	 */
   2006 	callout_stop(&fdc->sc_timo_ch);
   2007 	fdcretry(fdc);
   2008 	fdc->sc_state = DORESET;
   2009 	goto loop;
   2010 
   2011 #undef	st0
   2012 #undef	st1
   2013 #undef	cyl
   2014 }
   2015 
   2016 void
   2017 fdcretry(struct fdc_softc *fdc)
   2018 {
   2019 	struct fd_softc *fd;
   2020 	struct buf *bp;
   2021 	int error = EIO;
   2022 
   2023 	fd = fdc->sc_drives.tqh_first;
   2024 	bp = BUFQ_PEEK(fd->sc_q);
   2025 
   2026 	fdc->sc_overruns = 0;
   2027 	if (fd->sc_opts & FDOPT_NORETRY)
   2028 		goto fail;
   2029 
   2030 	switch (fdc->sc_errors) {
   2031 	case 0:
   2032 		if (fdc->sc_nstat == 7 &&
   2033 		    (fdc->sc_status[0] & 0xd8) == 0x40 &&
   2034 		    (fdc->sc_status[1] & 0x2) == 0x2) {
   2035 			aprint_error_dev(&fdc->sc_dev, "read-only medium\n");
   2036 			error = EROFS;
   2037 			goto failsilent;
   2038 		}
   2039 		/* try again */
   2040 		fdc->sc_state =
   2041 			(fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
   2042 		break;
   2043 
   2044 	case 1: case 2: case 3:
   2045 		/* didn't work; try recalibrating */
   2046 		fdc->sc_state = DORECAL;
   2047 		break;
   2048 
   2049 	case 4:
   2050 		if (fdc->sc_nstat == 7 &&
   2051 		    fdc->sc_status[0] == 0 &&
   2052 		    fdc->sc_status[1] == 0 &&
   2053 		    fdc->sc_status[2] == 0) {
   2054 			/*
   2055 			 * We've retried a few times and we've got
   2056 			 * valid status and all three status bytes
   2057 			 * are zero.  Assume this condition is the
   2058 			 * result of no disk loaded into the drive.
   2059 			 */
   2060 			aprint_error_dev(&fdc->sc_dev, "no medium?\n");
   2061 			error = ENODEV;
   2062 			goto failsilent;
   2063 		}
   2064 
   2065 		/* still no go; reset the bastard */
   2066 		fdc->sc_state = DORESET;
   2067 		break;
   2068 
   2069 	default:
   2070 	fail:
   2071 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
   2072 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
   2073 				fd->sc_skip / FD_BSIZE(fd),
   2074 				(struct disklabel *)NULL);
   2075 			printf("\n");
   2076 			fdcstatus(fdc, "controller status");
   2077 		}
   2078 
   2079 	failsilent:
   2080 		bp->b_error = error;
   2081 		fdfinish(fd, bp);
   2082 	}
   2083 	fdc->sc_errors++;
   2084 }
   2085 
   2086 int
   2087 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   2088 {
   2089 	struct fd_softc *fd;
   2090 	struct fdc_softc *fdc;
   2091 	struct fdformat_parms *form_parms;
   2092 	struct fdformat_cmd *form_cmd;
   2093 	struct ne7_fd_formb *fd_formb;
   2094 	int il[FD_MAX_NSEC + 1];
   2095 	int unit;
   2096 	int i, j;
   2097 	int error;
   2098 
   2099 	unit = FDUNIT(dev);
   2100 	if (unit >= fd_cd.cd_ndevs)
   2101 		return ENXIO;
   2102 
   2103 	fd = fd_cd.cd_devs[FDUNIT(dev)];
   2104 	fdc = (struct fdc_softc *)device_parent(&fd->sc_dv);
   2105 
   2106 	switch (cmd) {
   2107 	case DIOCGDINFO:
   2108 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
   2109 		return 0;
   2110 
   2111 	case DIOCWLABEL:
   2112 		if ((flag & FWRITE) == 0)
   2113 			return EBADF;
   2114 		/* XXX do something */
   2115 		return 0;
   2116 
   2117 	case DIOCWDINFO:
   2118 		if ((flag & FWRITE) == 0)
   2119 			return EBADF;
   2120 
   2121 		error = setdisklabel(fd->sc_dk.dk_label,
   2122 				    (struct disklabel *)addr, 0,
   2123 				    fd->sc_dk.dk_cpulabel);
   2124 		if (error)
   2125 			return error;
   2126 
   2127 		error = writedisklabel(dev, fdstrategy,
   2128 				       fd->sc_dk.dk_label,
   2129 				       fd->sc_dk.dk_cpulabel);
   2130 		return error;
   2131 
   2132 	case DIOCLOCK:
   2133 		/*
   2134 		 * Nothing to do here, really.
   2135 		 */
   2136 		return 0;
   2137 
   2138 	case DIOCEJECT:
   2139 		if (*(int *)addr == 0) {
   2140 			int part = DISKPART(dev);
   2141 			/*
   2142 			 * Don't force eject: check that we are the only
   2143 			 * partition open. If so, unlock it.
   2144 			 */
   2145 			if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
   2146 			    fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
   2147 			    fd->sc_dk.dk_openmask) {
   2148 				return EBUSY;
   2149 			}
   2150 		}
   2151 		/* FALLTHROUGH */
   2152 	case ODIOCEJECT:
   2153 		if (fdc->sc_flags & FDC_NOEJECT)
   2154 			return EINVAL;
   2155 		fd_do_eject(fd);
   2156 		return 0;
   2157 
   2158 	case FDIOCGETFORMAT:
   2159 		form_parms = (struct fdformat_parms *)addr;
   2160 		form_parms->fdformat_version = FDFORMAT_VERSION;
   2161 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
   2162 		form_parms->ncyl = fd->sc_type->cylinders;
   2163 		form_parms->nspt = fd->sc_type->sectrac;
   2164 		form_parms->ntrk = fd->sc_type->heads;
   2165 		form_parms->stepspercyl = fd->sc_type->step;
   2166 		form_parms->gaplen = fd->sc_type->gap2;
   2167 		form_parms->fillbyte = fd->sc_type->fillbyte;
   2168 		form_parms->interleave = fd->sc_type->interleave;
   2169 		switch (fd->sc_type->rate) {
   2170 		case FDC_500KBPS:
   2171 			form_parms->xfer_rate = 500 * 1024;
   2172 			break;
   2173 		case FDC_300KBPS:
   2174 			form_parms->xfer_rate = 300 * 1024;
   2175 			break;
   2176 		case FDC_250KBPS:
   2177 			form_parms->xfer_rate = 250 * 1024;
   2178 			break;
   2179 		default:
   2180 			return EINVAL;
   2181 		}
   2182 		return 0;
   2183 
   2184 	case FDIOCSETFORMAT:
   2185 		if ((flag & FWRITE) == 0)
   2186 			return EBADF;	/* must be opened for writing */
   2187 
   2188 		form_parms = (struct fdformat_parms *)addr;
   2189 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
   2190 			return EINVAL;/* wrong version of formatting prog */
   2191 
   2192 		i = form_parms->nbps >> 7;
   2193 		if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
   2194 		    i & ~(1 << (ffs(i)-1)))
   2195 			/* not a power-of-two multiple of 128 */
   2196 			return EINVAL;
   2197 
   2198 		switch (form_parms->xfer_rate) {
   2199 		case 500 * 1024:
   2200 			fd->sc_type->rate = FDC_500KBPS;
   2201 			break;
   2202 		case 300 * 1024:
   2203 			fd->sc_type->rate = FDC_300KBPS;
   2204 			break;
   2205 		case 250 * 1024:
   2206 			fd->sc_type->rate = FDC_250KBPS;
   2207 			break;
   2208 		default:
   2209 			return EINVAL;
   2210 		}
   2211 
   2212 		if (form_parms->nspt > FD_MAX_NSEC ||
   2213 		    form_parms->fillbyte > 0xff ||
   2214 		    form_parms->interleave > 0xff)
   2215 			return EINVAL;
   2216 		fd->sc_type->sectrac = form_parms->nspt;
   2217 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
   2218 			return EINVAL;
   2219 		fd->sc_type->heads = form_parms->ntrk;
   2220 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
   2221 		fd->sc_type->secsize = ffs(i)-1;
   2222 		fd->sc_type->gap2 = form_parms->gaplen;
   2223 		fd->sc_type->cylinders = form_parms->ncyl;
   2224 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
   2225 			form_parms->nbps / DEV_BSIZE;
   2226 		fd->sc_type->step = form_parms->stepspercyl;
   2227 		fd->sc_type->fillbyte = form_parms->fillbyte;
   2228 		fd->sc_type->interleave = form_parms->interleave;
   2229 		return 0;
   2230 
   2231 	case FDIOCFORMAT_TRACK:
   2232 		if((flag & FWRITE) == 0)
   2233 			/* must be opened for writing */
   2234 			return EBADF;
   2235 		form_cmd = (struct fdformat_cmd *)addr;
   2236 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
   2237 			/* wrong version of formatting prog */
   2238 			return EINVAL;
   2239 
   2240 		if (form_cmd->head >= fd->sc_type->heads ||
   2241 		    form_cmd->cylinder >= fd->sc_type->cylinders) {
   2242 			return EINVAL;
   2243 		}
   2244 
   2245 		fd_formb = malloc(sizeof(struct ne7_fd_formb),
   2246 		    M_TEMP, M_NOWAIT);
   2247 		if (fd_formb == 0)
   2248 			return ENOMEM;
   2249 
   2250 		fd_formb->head = form_cmd->head;
   2251 		fd_formb->cyl = form_cmd->cylinder;
   2252 		fd_formb->transfer_rate = fd->sc_type->rate;
   2253 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
   2254 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
   2255 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
   2256 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
   2257 
   2258 		bzero(il, sizeof il);
   2259 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
   2260 			while (il[(j % fd_formb->fd_formb_nsecs) + 1])
   2261 				j++;
   2262 			il[(j % fd_formb->fd_formb_nsecs) + 1] = i;
   2263 			j += fd->sc_type->interleave;
   2264 		}
   2265 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
   2266 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
   2267 			fd_formb->fd_formb_headno(i) = form_cmd->head;
   2268 			fd_formb->fd_formb_secno(i) = il[i + 1];
   2269 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
   2270 		}
   2271 
   2272 		error = fdformat(dev, fd_formb, l->l_proc);
   2273 		free(fd_formb, M_TEMP);
   2274 		return error;
   2275 
   2276 	case FDIOCGETOPTS:		/* get drive options */
   2277 		*(int *)addr = fd->sc_opts;
   2278 		return 0;
   2279 
   2280 	case FDIOCSETOPTS:		/* set drive options */
   2281 		fd->sc_opts = *(int *)addr;
   2282 		return 0;
   2283 
   2284 #ifdef FD_DEBUG
   2285 	case _IO('f', 100):
   2286 		fdc_wrfifo(fdc, NE7CMD_DUMPREG);
   2287 		fdcresult(fdc);
   2288 		printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
   2289 		for (i = 0; i < fdc->sc_nstat; i++)
   2290 			printf(" 0x%x", fdc->sc_status[i]);
   2291 		printf(">\n");
   2292 		return 0;
   2293 
   2294 	case _IOW('f', 101, int):
   2295 		fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   2296 		fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
   2297 		fdconf(fdc);
   2298 		return 0;
   2299 
   2300 	case _IO('f', 102):
   2301 		fdc_wrfifo(fdc, NE7CMD_SENSEI);
   2302 		fdcresult(fdc);
   2303 		printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
   2304 		for (i=0; i< fdc->sc_nstat; i++)
   2305 			printf(" 0x%x", fdc->sc_status[i]);
   2306 		printf(">\n");
   2307 		return 0;
   2308 #endif
   2309 	default:
   2310 		return ENOTTY;
   2311 	}
   2312 
   2313 #ifdef DIAGNOSTIC
   2314 	panic("fdioctl: impossible");
   2315 #endif
   2316 }
   2317 
   2318 int
   2319 fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct proc *p)
   2320 {
   2321 	int rv = 0;
   2322 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   2323 	struct fd_type *type = fd->sc_type;
   2324 	struct buf *bp;
   2325 
   2326 	/* set up a buffer header for fdstrategy() */
   2327 	bp = getiobuf(NULL, false);
   2328 	if (bp == NULL)
   2329 		return ENOBUFS;
   2330 
   2331 	bp->b_vp = NULL;
   2332 	bp->b_cflags = BC_BUSY;
   2333 	bp->b_flags = B_PHYS | B_FORMAT;
   2334 	bp->b_proc = p;
   2335 	bp->b_dev = dev;
   2336 
   2337 	/*
   2338 	 * Calculate a fake blkno, so fdstrategy() would initiate a
   2339 	 * seek to the requested cylinder.
   2340 	 */
   2341 	bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
   2342 		       + finfo->head * type->sectrac) * FD_BSIZE(fd))
   2343 		      / DEV_BSIZE;
   2344 
   2345 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
   2346 	bp->b_data = (void *)finfo;
   2347 
   2348 #ifdef FD_DEBUG
   2349 	if (fdc_debug) {
   2350 		int i;
   2351 
   2352 		printf("fdformat: blkno 0x%llx count %d\n",
   2353 			(unsigned long long)bp->b_blkno, bp->b_bcount);
   2354 
   2355 		printf("\tcyl:\t%d\n", finfo->cyl);
   2356 		printf("\thead:\t%d\n", finfo->head);
   2357 		printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
   2358 		printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
   2359 		printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
   2360 		printf("\ttrack data:");
   2361 		for (i = 0; i < finfo->fd_formb_nsecs; i++) {
   2362 			printf(" [c%d h%d s%d]",
   2363 					finfo->fd_formb_cylno(i),
   2364 					finfo->fd_formb_headno(i),
   2365 					finfo->fd_formb_secno(i) );
   2366 			if (finfo->fd_formb_secsize(i) != 2)
   2367 				printf("<sz:%d>", finfo->fd_formb_secsize(i));
   2368 		}
   2369 		printf("\n");
   2370 	}
   2371 #endif
   2372 
   2373 	/* now do the format */
   2374 	fdstrategy(bp);
   2375 
   2376 	/* ...and wait for it to complete */
   2377 	rv = biowait(bp);
   2378 	putiobuf(bp);
   2379 	return rv;
   2380 }
   2381 
   2382 void
   2383 fdgetdisklabel(dev_t dev)
   2384 {
   2385 	int unit = FDUNIT(dev), i;
   2386 	struct fd_softc *fd = fd_cd.cd_devs[unit];
   2387 	struct disklabel *lp = fd->sc_dk.dk_label;
   2388 	struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
   2389 
   2390 	bzero(lp, sizeof(struct disklabel));
   2391 	bzero(lp, sizeof(struct cpu_disklabel));
   2392 
   2393 	lp->d_type = DTYPE_FLOPPY;
   2394 	lp->d_secsize = FD_BSIZE(fd);
   2395 	lp->d_secpercyl = fd->sc_type->seccyl;
   2396 	lp->d_nsectors = fd->sc_type->sectrac;
   2397 	lp->d_ncylinders = fd->sc_type->cylinders;
   2398 	lp->d_ntracks = fd->sc_type->heads;	/* Go figure... */
   2399 	lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
   2400 	lp->d_rpm = 300;	/* XXX like it matters... */
   2401 
   2402 	strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
   2403 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   2404 	lp->d_interleave = 1;
   2405 	lp->d_flags = D_REMOVABLE;
   2406 
   2407 	lp->d_partitions[RAW_PART].p_offset = 0;
   2408 	lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
   2409 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   2410 	lp->d_npartitions = RAW_PART + 1;
   2411 
   2412 	lp->d_magic = DISKMAGIC;
   2413 	lp->d_magic2 = DISKMAGIC;
   2414 	lp->d_checksum = dkcksum(lp);
   2415 
   2416 	/*
   2417 	 * Call the generic disklabel extraction routine.  If there's
   2418 	 * not a label there, fake it.
   2419 	 */
   2420 	if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
   2421 		strncpy(lp->d_packname, "default label",
   2422 		    sizeof(lp->d_packname));
   2423 		/*
   2424 		 * Reset the partition info; it might have gotten
   2425 		 * trashed in readdisklabel().
   2426 		 *
   2427 		 * XXX Why do we have to do this?  readdisklabel()
   2428 		 * should be safe...
   2429 		 */
   2430 		for (i = 0; i < MAXPARTITIONS; ++i) {
   2431 			lp->d_partitions[i].p_offset = 0;
   2432 			if (i == RAW_PART) {
   2433 				lp->d_partitions[i].p_size =
   2434 				    lp->d_secpercyl * lp->d_ncylinders;
   2435 				lp->d_partitions[i].p_fstype = FS_BSDFFS;
   2436 			} else {
   2437 				lp->d_partitions[i].p_size = 0;
   2438 				lp->d_partitions[i].p_fstype = FS_UNUSED;
   2439 			}
   2440 		}
   2441 		lp->d_npartitions = RAW_PART + 1;
   2442 	}
   2443 }
   2444 
   2445 void
   2446 fd_do_eject(struct fd_softc *fd)
   2447 {
   2448 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   2449 
   2450 #ifdef SUN4
   2451 	if (CPU_ISSUN4C) {
   2452 		auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
   2453 		delay(10);
   2454 		auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
   2455 		return;
   2456 	}
   2457 	if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
   2458 #endif
   2459 		bus_space_tag_t t = fdc->sc_bustag;
   2460 		bus_space_handle_t h = fdc->sc_handle;
   2461 		uint8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
   2462 
   2463 		bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
   2464 		delay(10);
   2465 		bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
   2466 		return;
   2467 #ifdef SUN4
   2468 	}
   2469 #endif
   2470 }
   2471 
   2472 /* ARGSUSED */
   2473 void
   2474 fd_mountroot_hook(struct device *dev)
   2475 {
   2476 	int c;
   2477 
   2478 	fd_do_eject((struct fd_softc *)dev);
   2479 	printf("Insert filesystem floppy and press return.");
   2480 	for (;;) {
   2481 		c = cngetc();
   2482 		if ((c == '\r') || (c == '\n')) {
   2483 			printf("\n");
   2484 			break;
   2485 		}
   2486 	}
   2487 }
   2488 
   2489 #ifdef MEMORY_DISK_HOOKS
   2490 
   2491 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
   2492 
   2493 int
   2494 fd_read_md_image(size_t	*sizep, void **addrp)
   2495 {
   2496 	struct buf buf, *bp = &buf;
   2497 	dev_t dev;
   2498 	off_t offset;
   2499 	char *addr;
   2500 
   2501 	dev = makedev(54,0);	/* XXX */
   2502 
   2503 	MALLOC(addr, void *, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
   2504 	*addrp = addr;
   2505 
   2506 	if (fdopen(dev, 0, S_IFCHR, NULL))
   2507 		panic("fd: mountroot: fdopen");
   2508 
   2509 	offset = 0;
   2510 
   2511 	for (;;) {
   2512 		bp->b_dev = dev;
   2513 		bp->b_error = 0;
   2514 		bp->b_resid = 0;
   2515 		bp->b_proc = NULL;
   2516 		bp->b_cflags = BC_BUSY;
   2517 		bp->b_flags = B_PHYS | B_RAW | B_READ;
   2518 		bp->b_blkno = btodb(offset);
   2519 		bp->b_bcount = DEV_BSIZE;
   2520 		bp->b_data = addr;
   2521 		fdstrategy(bp);
   2522 		biowait(bp);
   2523 		if (bp->b_error)
   2524 			panic("fd: mountroot: fdread error %d", bp->b_error);
   2525 
   2526 		if (bp->b_resid != 0)
   2527 			break;
   2528 
   2529 		addr += DEV_BSIZE;
   2530 		offset += DEV_BSIZE;
   2531 		if (offset + DEV_BSIZE > FDMICROROOTSIZE)
   2532 			break;
   2533 	}
   2534 	(void)fdclose(dev, 0, S_IFCHR, NULL);
   2535 	*sizep = offset;
   2536 	fd_do_eject(fd_cd.cd_devs[FDUNIT(dev)]);
   2537 	return 0;
   2538 }
   2539 #endif /* MEMORY_DISK_HOOKS */
   2540 
   2541 static void
   2542 fd_set_properties(struct fd_softc *fd)
   2543 {
   2544 	prop_dictionary_t disk_info, odisk_info, geom;
   2545 	struct fd_type *fdt;
   2546 	int secsize;
   2547 
   2548 	fdt = fd->sc_deftype;
   2549 
   2550 	disk_info = prop_dictionary_create();
   2551 
   2552 	geom = prop_dictionary_create();
   2553 
   2554 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
   2555 	    fdt->size);
   2556 
   2557 	switch (fdt->secsize) {
   2558 	case 2:
   2559 		secsize = 512;
   2560 		break;
   2561 	case 3:
   2562 		secsize = 1024;
   2563 		break;
   2564 	default:
   2565 		secsize = 0;
   2566 	}
   2567 
   2568 	prop_dictionary_set_uint32(geom, "sector-size",
   2569 	    secsize);
   2570 
   2571 	prop_dictionary_set_uint16(geom, "sectors-per-track",
   2572 	    fdt->sectrac);
   2573 
   2574 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
   2575 	    fdt->heads);
   2576 
   2577 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
   2578 	    fdt->cylinders);
   2579 
   2580 	prop_dictionary_set(disk_info, "geometry", geom);
   2581 	prop_object_release(geom);
   2582 
   2583 	prop_dictionary_set(device_properties(&fd->sc_dv),
   2584 	    "disk-info", disk_info);
   2585 
   2586 	/*
   2587 	 * Don't release disk_info here; we keep a reference to it.
   2588 	 * disk_detach() will release it when we go away.
   2589 	 */
   2590 
   2591 	odisk_info = fd->sc_dk.dk_info;
   2592 	fd->sc_dk.dk_info = disk_info;
   2593 	if (odisk_info)
   2594 		prop_object_release(odisk_info);
   2595 }
   2596