Home | History | Annotate | Line # | Download | only in dev
fdc.c revision 1.12
      1 /*	$NetBSD: fdc.c,v 1.12 2007/03/27 10:17:48 jnemeth 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.12 2007/03/27 10:17:48 jnemeth 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 
    137 #include <dev/cons.h>
    138 
    139 #include <uvm/uvm_extern.h>
    140 
    141 #include <machine/autoconf.h>
    142 #include <machine/intr.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;	/* softintr(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 		printf("%s: cannot map registers\n", self->dv_xname);
    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 		printf("%s: cannot map control registers\n",
    645 			self->dv_xname);
    646 		return;
    647 	}
    648 
    649 	establish_chip_type(fdc,
    650 		sa->sa_bustag,
    651 		sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset),
    652 		sa->sa_size,
    653 		fdc->sc_handle);
    654 
    655 	if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
    656 		printf(": no drives attached\n");
    657 		return;
    658 	}
    659 
    660 	if (fdcattach(fdc, sa->sa_pri) != 0)
    661 		bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
    662 }
    663 
    664 #elif SUN4U
    665 
    666 void
    667 fdcattach_sbus(struct device *parent, struct device *self, void *aux)
    668 {
    669 	struct fdc_softc *fdc = (void *)self;
    670 	struct sbus_attach_args *sa = aux;
    671 
    672 	if (sa->sa_nintr == 0) {
    673 		printf(": no interrupt line configured\n");
    674 		return;
    675 	}
    676 
    677 	if (auxio_fd_control(0) != 0) {
    678 		printf(": can't attach before auxio\n");
    679 		return;
    680 	}
    681 
    682 	fdc->sc_bustag = sa->sa_bustag;
    683 
    684 	if (bus_space_map(sa->sa_bustag, BUS_ADDR(sa->sa_slot, sa->sa_offset),
    685 			  sa->sa_size, 0, &fdc->sc_handle) != 0) {
    686 		printf(": cannot map control registers\n");
    687 		return;
    688 	}
    689 
    690 	establish_chip_type(fdc,
    691 			    sa->sa_bustag,
    692 			    BUS_ADDR(sa->sa_slot, sa->sa_offset),
    693 			    sa->sa_size,
    694 			    fdc->sc_handle);
    695 
    696 	if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
    697 		printf(": no drives attached\n");
    698 		return;
    699 	}
    700 
    701 	if (prom_getproplen(sa->sa_node, "manual") >= 0)
    702 		fdc->sc_flags |= FDC_NOEJECT;
    703 
    704 
    705 	if (fdcattach(fdc, sa->sa_pri) != 0)
    706 		bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
    707 }
    708 
    709 void
    710 fdcattach_ebus(struct device *parent, struct device *self, void *aux)
    711 {
    712 	struct fdc_softc *fdc = (void *)self;
    713 	struct ebus_attach_args *ea = aux;
    714 	int map_vaddr;
    715 
    716 	if (ea->ea_nintr == 0) {
    717 		printf(": no interrupt line configured\n");
    718 		return;
    719 	}
    720 
    721 	if (ea->ea_nreg < 3) {
    722 		printf(": expected 3 registers, only got %d\n",
    723 		    ea->ea_nreg);
    724 		return;
    725 	}
    726 
    727 	fdc->sc_bustag = ea->ea_bustag;
    728 
    729 	if (ea->ea_nvaddr > 0) {
    730 		sparc_promaddr_to_handle(ea->ea_bustag,
    731 		    ea->ea_vaddr[0], &fdc->sc_handle);
    732 		map_vaddr = 1;
    733 	} else if (bus_space_map(fdc->sc_bustag,
    734 	    EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    735 	    ea->ea_reg[0].size, 0, &fdc->sc_handle) == 0) {
    736 		map_vaddr = 0;
    737 	} else {
    738 		printf(": can't map control registers\n");
    739 		return;
    740 	}
    741 
    742 	establish_chip_type(fdc,
    743 		fdc->sc_bustag,
    744 		map_vaddr ? ea->ea_vaddr[0] :
    745 		    EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    746 		ea->ea_reg[0].size,
    747 		fdc->sc_handle);
    748 
    749 	fdc->sc_flags |= FDC_EBUS;
    750 
    751 	if (prom_getproplen(ea->ea_node, "manual") >= 0)
    752 		fdc->sc_flags |= FDC_NOEJECT;
    753 
    754 	if (fdcattach(fdc, ea->ea_intr[0]) != 0)
    755 		if (map_vaddr == 0)
    756 			bus_space_unmap(ea->ea_bustag, fdc->sc_handle,
    757 			    ea->ea_reg[0].size);
    758 }
    759 #endif
    760 
    761 int
    762 fdcattach(struct fdc_softc *fdc, int pri)
    763 {
    764 	struct fdc_attach_args fa;
    765 	int drive_attached;
    766 	char code;
    767 
    768 	callout_init(&fdc->sc_timo_ch);
    769 	callout_init(&fdc->sc_intr_ch);
    770 
    771 	fdc->sc_state = DEVIDLE;
    772 	fdc->sc_itask = FDC_ITASK_NONE;
    773 	fdc->sc_istatus = FDC_ISTATUS_NONE;
    774 	fdc->sc_flags |= FDC_EIS;
    775 	TAILQ_INIT(&fdc->sc_drives);
    776 
    777 	if ((fdc->sc_flags & FDC_82077) != 0) {
    778 		fdc->sc_reg_msr = FDREG77_MSR;
    779 		fdc->sc_reg_fifo = FDREG77_FIFO;
    780 		fdc->sc_reg_dor = FDREG77_DOR;
    781 		fdc->sc_reg_dir = FDREG77_DIR;
    782 		code = '7';
    783 		fdc->sc_flags |= FDC_NEEDMOTORWAIT;
    784 	} else {
    785 		fdc->sc_reg_msr = FDREG72_MSR;
    786 		fdc->sc_reg_fifo = FDREG72_FIFO;
    787 		fdc->sc_reg_dor = 0;
    788 		code = '2';
    789 	}
    790 
    791 	/*
    792 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
    793 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
    794 	 */
    795 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
    796 	if (fdconf(fdc) != 0) {
    797 		printf(": no drives attached\n");
    798 		return -1;
    799 	}
    800 
    801 #ifdef SUN4
    802 	fdc->sc_sicookie = softintr_establish(IPL_BIO, fdcswintr, fdc);
    803 #elif SUN4U
    804 	fdc->sc_sicookie = softintr_establish(IPL_FDSOFT, fdcswintr, fdc);
    805 #endif
    806 	if (fdc->sc_sicookie == NULL) {
    807 		printf("\n%s: cannot register soft interrupt handler\n",
    808 			fdc->sc_dev.dv_xname);
    809 		callout_stop(&fdc->sc_timo_ch);
    810 		callout_stop(&fdc->sc_intr_ch);
    811 		return -1;
    812 	}
    813 #ifdef SUN4
    814 	printf(" softpri %d: chip 8207%c\n", IPL_SOFTFDC, code);
    815 #elif SUN4U
    816 	printf(" softpri %d: chip 8207%c", PIL_FDSOFT, code);
    817 	if (fdc->sc_flags & FDC_NOEJECT)
    818 		printf(": manual eject");
    819 	printf("\n");
    820 #endif
    821 
    822 #ifdef SUN4
    823 	fdciop = &fdc->sc_io;
    824 	if (bus_intr_establish2(fdc->sc_bustag, pri, 0,
    825 				fdc_c_hwintr, fdc, fdchwintr) == NULL) {
    826 #elif SUN4U
    827 	if (bus_intr_establish(fdc->sc_bustag, pri, IPL_BIO,
    828 				fdc_c_hwintr, fdc) == NULL) {
    829 #endif
    830 		printf("\n%s: cannot register interrupt handler\n",
    831 			fdc->sc_dev.dv_xname);
    832 		callout_stop(&fdc->sc_timo_ch);
    833 		callout_stop(&fdc->sc_intr_ch);
    834 		softintr_disestablish(fdc->sc_sicookie);
    835 		return -1;
    836 	}
    837 
    838 	evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    839 	    fdc->sc_dev.dv_xname, "intr");
    840 
    841 	/* physical limit: four drives per controller. */
    842 	drive_attached = 0;
    843 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    844 		fa.fa_deftype = NULL;		/* unknown */
    845 		fa.fa_deftype = &fd_types[0];	/* XXX */
    846 		if (config_found(&fdc->sc_dev, (void *)&fa, fdprint) != NULL)
    847 			drive_attached = 1;
    848 	}
    849 
    850 	if (drive_attached == 0) {
    851 		/* XXX - dis-establish interrupts here */
    852 		/* return -1; */
    853 	}
    854 
    855 	return 0;
    856 }
    857 
    858 int
    859 fdmatch(struct device *parent, struct cfdata *match, void *aux)
    860 {
    861 	struct fdc_softc *fdc = (void *)parent;
    862 	bus_space_tag_t t = fdc->sc_bustag;
    863 	bus_space_handle_t h = fdc->sc_handle;
    864 	struct fdc_attach_args *fa = aux;
    865 	int drive = fa->fa_drive;
    866 	int n, ok;
    867 
    868 	if (drive > 0)
    869 		/* XXX - for now, punt on more than one drive */
    870 		return 0;
    871 
    872 	if ((fdc->sc_flags & FDC_82077) != 0) {
    873 		/* select drive and turn on motor */
    874 		bus_space_write_1(t, h, fdc->sc_reg_dor,
    875 				  drive | FDO_FRST | FDO_MOEN(drive));
    876 		/* wait for motor to spin up */
    877 		delay(250000);
    878 #ifdef SUN4
    879 	} else {
    880 		auxregbisc(AUXIO4C_FDS, 0);
    881 #endif
    882 	}
    883 	fdc->sc_nstat = 0;
    884 	fdc_wrfifo(fdc, NE7CMD_RECAL);
    885 	fdc_wrfifo(fdc, drive);
    886 
    887 	/* Wait for recalibration to complete */
    888 	for (n = 0; n < 10000; n++) {
    889 		uint8_t v;
    890 
    891 		delay(1000);
    892 		v = bus_space_read_1(t, h, fdc->sc_reg_msr);
    893 		if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
    894 			/* wait a bit longer till device *really* is ready */
    895 			delay(100000);
    896 			if (fdc_wrfifo(fdc, NE7CMD_SENSEI))
    897 				break;
    898 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
    899 				/*
    900 				 * Got `invalid command'; we interpret it
    901 				 * to mean that the re-calibrate hasn't in
    902 				 * fact finished yet
    903 				 */
    904 				continue;
    905 			break;
    906 		}
    907 	}
    908 	n = fdc->sc_nstat;
    909 #ifdef FD_DEBUG
    910 	if (fdc_debug) {
    911 		int i;
    912 		printf("fdprobe: %d stati:", n);
    913 		for (i = 0; i < n; i++)
    914 			printf(" 0x%x", fdc->sc_status[i]);
    915 		printf("\n");
    916 	}
    917 #endif
    918 	ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
    919 
    920 	/* turn off motor */
    921 	if ((fdc->sc_flags & FDC_82077) != 0) {
    922 		/* deselect drive and turn motor off */
    923 		bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
    924 #ifdef SUN4
    925 	} else {
    926 		auxregbisc(0, AUXIO4C_FDS);
    927 #endif
    928 	}
    929 
    930 	return ok;
    931 }
    932 
    933 /*
    934  * Controller is working, and drive responded.  Attach it.
    935  */
    936 void
    937 fdattach(struct device *parent, struct device *self, void *aux)
    938 {
    939 	struct fdc_softc *fdc = (void *)parent;
    940 	struct fd_softc *fd = (void *)self;
    941 	struct fdc_attach_args *fa = aux;
    942 	struct fd_type *type = fa->fa_deftype;
    943 	int drive = fa->fa_drive;
    944 
    945 	callout_init(&fd->sc_motoron_ch);
    946 	callout_init(&fd->sc_motoroff_ch);
    947 
    948 	/* XXX Allow `flags' to override device type? */
    949 
    950 	if (type)
    951 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    952 		    type->cylinders, type->heads, type->sectrac);
    953 	else
    954 		printf(": density unknown\n");
    955 
    956 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    957 	fd->sc_cylin = -1;
    958 	fd->sc_drive = drive;
    959 	fd->sc_deftype = type;
    960 	fdc->sc_fd[drive] = fd;
    961 
    962 	fdc_wrfifo(fdc, NE7CMD_SPECIFY);
    963 	fdc_wrfifo(fdc, type->steprate);
    964 	/* XXX head load time == 6ms */
    965 	fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
    966 
    967 	/*
    968 	 * Initialize and attach the disk structure.
    969 	 */
    970 	fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
    971 	fd->sc_dk.dk_driver = &fddkdriver;
    972 	disk_attach(&fd->sc_dk);
    973 
    974 	/*
    975 	 * Establish a mountroot_hook anyway in case we booted
    976 	 * with RB_ASKNAME and get selected as the boot device.
    977 	 */
    978 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
    979 
    980 	fd_set_properties(fd);
    981 
    982 	/* Make sure the drive motor gets turned off at shutdown time. */
    983 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
    984 }
    985 
    986 inline struct fd_type *
    987 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
    988 {
    989 	int type = FDTYPE(dev);
    990 
    991 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    992 		return NULL;
    993 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    994 }
    995 
    996 void
    997 fdstrategy(struct buf *bp)
    998 {
    999 	struct fd_softc *fd;
   1000 	int unit = FDUNIT(bp->b_dev);
   1001 	int sz;
   1002  	int s;
   1003 
   1004 	/* Valid unit, controller, and request? */
   1005 	if (unit >= fd_cd.cd_ndevs ||
   1006 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
   1007 	    bp->b_blkno < 0 ||
   1008 	    (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
   1009 	      (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
   1010 	     (bp->b_flags & B_FORMAT) == 0)) {
   1011 		bp->b_error = EINVAL;
   1012 		goto bad;
   1013 	}
   1014 
   1015 	/* If it's a null transfer, return immediately. */
   1016 	if (bp->b_bcount == 0)
   1017 		goto done;
   1018 
   1019 	sz = howmany(bp->b_bcount, DEV_BSIZE);
   1020 
   1021 	if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
   1022 		sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
   1023 		     - bp->b_blkno;
   1024 		if (sz == 0) {
   1025 			/* If exactly at end of disk, return EOF. */
   1026 			bp->b_resid = bp->b_bcount;
   1027 			goto done;
   1028 		}
   1029 		if (sz < 0) {
   1030 			/* If past end of disk, return EINVAL. */
   1031 			bp->b_error = EINVAL;
   1032 			goto bad;
   1033 		}
   1034 		/* Otherwise, truncate request. */
   1035 		bp->b_bcount = sz << DEV_BSHIFT;
   1036 	}
   1037 
   1038 	bp->b_rawblkno = bp->b_blkno;
   1039  	bp->b_cylinder = (bp->b_blkno * DEV_BSIZE) /
   1040 		      (FD_BSIZE(fd) * fd->sc_type->seccyl);
   1041 
   1042 #ifdef FD_DEBUG
   1043 	if (fdc_debug > 1)
   1044 	    printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld cylin %d sz %d\n",
   1045 		    (long long)bp->b_blkno, bp->b_bcount,
   1046 		    (long long)fd->sc_blkno, bp->b_cylinder, sz);
   1047 #endif
   1048 
   1049 	/* Queue transfer on drive, activate drive and controller if idle. */
   1050 	s = splbio();
   1051 	BUFQ_PUT(fd->sc_q, bp);
   1052 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
   1053 	if (fd->sc_active == 0)
   1054 		fdstart(fd);
   1055 #ifdef DIAGNOSTIC
   1056 	else {
   1057 		struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1058 		if (fdc->sc_state == DEVIDLE) {
   1059 			printf("fdstrategy: controller inactive\n");
   1060 			fdcstart(fdc);
   1061 		}
   1062 	}
   1063 #endif
   1064 	splx(s);
   1065 	return;
   1066 
   1067 bad:
   1068 	bp->b_flags |= B_ERROR;
   1069 done:
   1070 	/* Toss transfer; we're done early. */
   1071 	biodone(bp);
   1072 }
   1073 
   1074 void
   1075 fdstart(struct fd_softc *fd)
   1076 {
   1077 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1078 	int active = fdc->sc_drives.tqh_first != 0;
   1079 
   1080 	/* Link into controller queue. */
   1081 	fd->sc_active = 1;
   1082 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
   1083 
   1084 	/* If controller not already active, start it. */
   1085 	if (!active)
   1086 		fdcstart(fdc);
   1087 }
   1088 
   1089 void
   1090 fdfinish(struct fd_softc *fd, struct buf *bp)
   1091 {
   1092 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1093 
   1094 	/*
   1095 	 * Move this drive to the end of the queue to give others a `fair'
   1096 	 * chance.  We only force a switch if N operations are completed while
   1097 	 * another drive is waiting to be serviced, since there is a long motor
   1098 	 * startup delay whenever we switch.
   1099 	 */
   1100 	(void)BUFQ_GET(fd->sc_q);
   1101 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
   1102 		fd->sc_ops = 0;
   1103 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1104 		if (BUFQ_PEEK(fd->sc_q) != NULL) {
   1105 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
   1106 		} else
   1107 			fd->sc_active = 0;
   1108 	}
   1109 	bp->b_resid = fd->sc_bcount;
   1110 	fd->sc_skip = 0;
   1111 
   1112 	biodone(bp);
   1113 	/* turn off motor 5s from now */
   1114 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
   1115 	fdc->sc_state = DEVIDLE;
   1116 }
   1117 
   1118 void
   1119 fdc_reset(struct fdc_softc *fdc)
   1120 {
   1121 	bus_space_tag_t t = fdc->sc_bustag;
   1122 	bus_space_handle_t h = fdc->sc_handle;
   1123 
   1124 	if ((fdc->sc_flags & FDC_82077) != 0) {
   1125 		bus_space_write_1(t, h, fdc->sc_reg_dor,
   1126 				  FDO_FDMAEN | FDO_MOEN(0));
   1127 	}
   1128 
   1129 	bus_space_write_1(t, h, fdc->sc_reg_drs, DRS_RESET);
   1130 	delay(10);
   1131 	bus_space_write_1(t, h, fdc->sc_reg_drs, 0);
   1132 
   1133 	if ((fdc->sc_flags & FDC_82077) != 0) {
   1134 		bus_space_write_1(t, h, fdc->sc_reg_dor,
   1135 				  FDO_FRST | FDO_FDMAEN | FDO_DS);
   1136 	}
   1137 #ifdef FD_DEBUG
   1138 	if (fdc_debug)
   1139 		printf("fdc reset\n");
   1140 #endif
   1141 }
   1142 
   1143 void
   1144 fd_set_motor(struct fdc_softc *fdc)
   1145 {
   1146 	struct fd_softc *fd;
   1147 	u_char status;
   1148 	int n;
   1149 
   1150 	if ((fdc->sc_flags & FDC_82077) != 0) {
   1151 		status = FDO_FRST | FDO_FDMAEN;
   1152 		if ((fd = fdc->sc_drives.tqh_first) != NULL)
   1153 			status |= fd->sc_drive;
   1154 
   1155 		for (n = 0; n < 4; n++)
   1156 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
   1157 				status |= FDO_MOEN(n);
   1158 		bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
   1159 				  fdc->sc_reg_dor, status);
   1160 #ifdef SUN4
   1161 	} else {
   1162 
   1163 		for (n = 0; n < 4; n++) {
   1164 			if ((fd = fdc->sc_fd[n]) != NULL  &&
   1165 			    (fd->sc_flags & FD_MOTOR) != 0) {
   1166 				auxregbisc(AUXIO4C_FDS, 0);
   1167 				return;
   1168 			}
   1169 		}
   1170 		auxregbisc(0, AUXIO4C_FDS);
   1171 #endif
   1172 	}
   1173 }
   1174 
   1175 void
   1176 fd_motor_off(void *arg)
   1177 {
   1178 	struct fd_softc *fd = arg;
   1179 	int s;
   1180 
   1181 	s = splbio();
   1182 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1183 	fd_set_motor((struct fdc_softc *)device_parent(&fd->sc_dv));
   1184 	splx(s);
   1185 }
   1186 
   1187 void
   1188 fd_motor_on(void *arg)
   1189 {
   1190 	struct fd_softc *fd = arg;
   1191 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1192 	int s;
   1193 
   1194 	s = splbio();
   1195 	fd->sc_flags &= ~FD_MOTOR_WAIT;
   1196 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
   1197 		(void)fdcstate(fdc);
   1198 	splx(s);
   1199 }
   1200 
   1201 /*
   1202  * Get status bytes off the FDC after a command has finished
   1203  * Returns the number of status bytes read; -1 on error.
   1204  * The return value is also stored in `sc_nstat'.
   1205  */
   1206 int
   1207 fdcresult(struct fdc_softc *fdc)
   1208 {
   1209 	bus_space_tag_t t = fdc->sc_bustag;
   1210 	bus_space_handle_t h = fdc->sc_handle;
   1211 	int j, n = 0;
   1212 
   1213 	for (j = 10000; j; j--) {
   1214 		uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
   1215 		v &= (NE7_DIO | NE7_RQM | NE7_CB);
   1216 		if (v == NE7_RQM)
   1217 			return fdc->sc_nstat = n;
   1218 		if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
   1219 			if (n >= sizeof(fdc->sc_status)) {
   1220 				log(LOG_ERR, "fdcresult: overrun\n");
   1221 				return -1;
   1222 			}
   1223 			fdc->sc_status[n++] =
   1224 				bus_space_read_1(t, h, fdc->sc_reg_fifo);
   1225 		} else
   1226 			delay(1);
   1227 	}
   1228 
   1229 	log(LOG_ERR, "fdcresult: timeout\n");
   1230 	return fdc->sc_nstat = -1;
   1231 }
   1232 
   1233 /*
   1234  * Write a command byte to the FDC.
   1235  * Returns 0 on success; -1 on failure (i.e. timeout)
   1236  */
   1237 int
   1238 fdc_wrfifo(struct fdc_softc *fdc, uint8_t x)
   1239 {
   1240 	bus_space_tag_t t = fdc->sc_bustag;
   1241 	bus_space_handle_t h = fdc->sc_handle;
   1242 	int i;
   1243 
   1244 	for (i = 100000; i-- > 0;) {
   1245 		uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
   1246 		if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
   1247 			/* The chip is ready */
   1248 			bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
   1249 			return 0;
   1250 		}
   1251 		delay(1);
   1252 	}
   1253 	return -1;
   1254 }
   1255 
   1256 int
   1257 fdc_diskchange(struct fdc_softc *fdc)
   1258 {
   1259 
   1260 #ifdef SUN4
   1261 	if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
   1262 #endif
   1263 		bus_space_tag_t t = fdc->sc_bustag;
   1264 		bus_space_handle_t h = fdc->sc_handle;
   1265 		uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_dir);
   1266 		return (v & FDI_DCHG) != 0;
   1267 #ifdef SUN4
   1268 	} else if (CPU_ISSUN4C) {
   1269 		return (*AUXIO4C_REG & AUXIO4C_FDC) != 0;
   1270 	}
   1271 	return 0;
   1272 #endif
   1273 }
   1274 
   1275 int
   1276 fdopen(dev_t dev, int flags, int fmt, struct lwp *l)
   1277 {
   1278  	int unit, pmask;
   1279 	struct fd_softc *fd;
   1280 	struct fd_type *type;
   1281 
   1282 	unit = FDUNIT(dev);
   1283 	if (unit >= fd_cd.cd_ndevs)
   1284 		return ENXIO;
   1285 	fd = fd_cd.cd_devs[unit];
   1286 	if (fd == NULL)
   1287 		return ENXIO;
   1288 	type = fd_dev_to_type(fd, dev);
   1289 	if (type == NULL)
   1290 		return ENXIO;
   1291 
   1292 	if ((fd->sc_flags & FD_OPEN) != 0 &&
   1293 	    fd->sc_type != type)
   1294 		return EBUSY;
   1295 
   1296 	fd->sc_type = type;
   1297 	fd->sc_cylin = -1;
   1298 	fd->sc_flags |= FD_OPEN;
   1299 
   1300 	/*
   1301 	 * Only update the disklabel if we're not open anywhere else.
   1302 	 */
   1303 	if (fd->sc_dk.dk_openmask == 0)
   1304 		fdgetdisklabel(dev);
   1305 
   1306 	pmask = (1 << DISKPART(dev));
   1307 
   1308 	switch (fmt) {
   1309 	case S_IFCHR:
   1310 		fd->sc_dk.dk_copenmask |= pmask;
   1311 		break;
   1312 
   1313 	case S_IFBLK:
   1314 		fd->sc_dk.dk_bopenmask |= pmask;
   1315 		break;
   1316 	}
   1317 	fd->sc_dk.dk_openmask =
   1318 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
   1319 
   1320 	return 0;
   1321 }
   1322 
   1323 int
   1324 fdclose(dev_t dev, int flags, int fmt, struct lwp *l)
   1325 {
   1326 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   1327 	int pmask = (1 << DISKPART(dev));
   1328 
   1329 	fd->sc_flags &= ~FD_OPEN;
   1330 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
   1331 
   1332 	switch (fmt) {
   1333 	case S_IFCHR:
   1334 		fd->sc_dk.dk_copenmask &= ~pmask;
   1335 		break;
   1336 
   1337 	case S_IFBLK:
   1338 		fd->sc_dk.dk_bopenmask &= ~pmask;
   1339 		break;
   1340 	}
   1341 	fd->sc_dk.dk_openmask =
   1342 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
   1343 
   1344 	return 0;
   1345 }
   1346 
   1347 int
   1348 fdread(dev_t dev, struct uio *uio, int flag)
   1349 {
   1350 
   1351         return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
   1352 }
   1353 
   1354 int
   1355 fdwrite(dev_t dev, struct uio *uio, int flag)
   1356 {
   1357 
   1358         return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
   1359 }
   1360 
   1361 void
   1362 fdcstart(struct fdc_softc *fdc)
   1363 {
   1364 
   1365 #ifdef DIAGNOSTIC
   1366 	/* only got here if controller's drive queue was inactive; should
   1367 	   be in idle state */
   1368 	if (fdc->sc_state != DEVIDLE) {
   1369 		printf("fdcstart: not idle\n");
   1370 		return;
   1371 	}
   1372 #endif
   1373 	(void)fdcstate(fdc);
   1374 }
   1375 
   1376 void
   1377 fdcstatus(struct fdc_softc *fdc, const char *s)
   1378 {
   1379 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
   1380 	int n;
   1381 	char bits[64];
   1382 
   1383 	/* Just print last status */
   1384 	n = fdc->sc_nstat;
   1385 
   1386 #if 0
   1387 	/*
   1388 	 * A 82072 seems to return <invalid command> on
   1389 	 * gratuitous Sense Interrupt commands.
   1390 	 */
   1391 	if (n == 0 && (fdc->sc_flags & FDC_82077) != 0) {
   1392 		fdc_wrfifo(fdc, NE7CMD_SENSEI);
   1393 		(void)fdcresult(fdc);
   1394 		n = 2;
   1395 	}
   1396 #endif
   1397 
   1398 	printf("%s: %s: state %d",
   1399 		fd ? fd->sc_dv.dv_xname : "fdc", s, fdc->sc_state);
   1400 
   1401 	switch (n) {
   1402 	case 0:
   1403 		printf("\n");
   1404 		break;
   1405 	case 2:
   1406 		printf(" (st0 %s cyl %d)\n",
   1407 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
   1408 		    bits, sizeof(bits)), fdc->sc_status[1]);
   1409 		break;
   1410 	case 7:
   1411 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
   1412 		    NE7_ST0BITS, bits, sizeof(bits)));
   1413 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
   1414 		    NE7_ST1BITS, bits, sizeof(bits)));
   1415 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
   1416 		    NE7_ST2BITS, bits, sizeof(bits)));
   1417 		printf(" cyl %d head %d sec %d)\n",
   1418 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1419 		break;
   1420 #ifdef DIAGNOSTIC
   1421 	default:
   1422 		printf(" fdcstatus: weird size: %d\n", n);
   1423 		break;
   1424 #endif
   1425 	}
   1426 }
   1427 
   1428 void
   1429 fdctimeout(void *arg)
   1430 {
   1431 	struct fdc_softc *fdc = arg;
   1432 	struct fd_softc *fd;
   1433 	int s;
   1434 
   1435 	s = splbio();
   1436 	fd = fdc->sc_drives.tqh_first;
   1437 	if (fd == NULL) {
   1438 		printf("%s: timeout but no I/O pending: state %d, istatus=%d\n",
   1439 			fdc->sc_dev.dv_xname,
   1440 			fdc->sc_state, fdc->sc_istatus);
   1441 		fdc->sc_state = DEVIDLE;
   1442 		goto out;
   1443 	}
   1444 
   1445 	if (BUFQ_PEEK(fd->sc_q) != NULL)
   1446 		fdc->sc_state++;
   1447 	else
   1448 		fdc->sc_state = DEVIDLE;
   1449 
   1450 	(void)fdcstate(fdc);
   1451 out:
   1452 	splx(s);
   1453 
   1454 }
   1455 
   1456 void
   1457 fdcpseudointr(void *arg)
   1458 {
   1459 	struct fdc_softc *fdc = arg;
   1460 	int s;
   1461 
   1462 	/* Just ensure it has the right spl. */
   1463 	s = splbio();
   1464 	(void)fdcstate(fdc);
   1465 	splx(s);
   1466 }
   1467 
   1468 
   1469 /*
   1470  * hardware interrupt entry point: used only if no `fast trap' * (in-window)
   1471  * handler is available. Unfortunately, we have no reliable way to
   1472  * determine that the interrupt really came from the floppy controller;
   1473  * just hope that the other devices that share this interrupt level
   1474  * can do better..
   1475  */
   1476 int
   1477 fdc_c_hwintr(void *arg)
   1478 {
   1479 	struct fdc_softc *fdc = arg;
   1480 	bus_space_tag_t t = fdc->sc_bustag;
   1481 	bus_space_handle_t h = fdc->sc_handle;
   1482 
   1483 	switch (fdc->sc_itask) {
   1484 	case FDC_ITASK_NONE:
   1485 		return 0;
   1486 	case FDC_ITASK_SENSEI:
   1487 		if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
   1488 			fdc->sc_istatus = FDC_ISTATUS_ERROR;
   1489 		else
   1490 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1491 		softintr_schedule(fdc->sc_sicookie);
   1492 		return 1;
   1493 	case FDC_ITASK_RESULT:
   1494 		if (fdcresult(fdc) == -1)
   1495 			fdc->sc_istatus = FDC_ISTATUS_ERROR;
   1496 		else
   1497 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1498 		softintr_schedule(fdc->sc_sicookie);
   1499 		return 1;
   1500 	case FDC_ITASK_DMA:
   1501 		/* Proceed with pseudo-DMA below */
   1502 		break;
   1503 	default:
   1504 		printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
   1505 		fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
   1506 		softintr_schedule(fdc->sc_sicookie);
   1507 		return 1;
   1508 	}
   1509 
   1510 	/*
   1511 	 * Pseudo DMA in progress
   1512 	 */
   1513 	for (;;) {
   1514 		uint8_t msr;
   1515 
   1516 		msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
   1517 
   1518 		if ((msr & NE7_RQM) == 0)
   1519 			/* That's all this round. */
   1520 			break;
   1521 
   1522 		if ((msr & NE7_NDM) == 0) {
   1523 			/* Execution phase finished, get result. */
   1524 			fdcresult(fdc);
   1525 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1526 			softintr_schedule(fdc->sc_sicookie);
   1527 			break;
   1528 		}
   1529 
   1530 		if (fdc->sc_tc == 0)
   1531 			/* For some reason the controller wants to transfer
   1532 			   more data then what we want to transfer. */
   1533 			panic("fdc: overrun");
   1534 
   1535 		/* Another byte can be transferred */
   1536 		if ((msr & NE7_DIO) != 0)
   1537 			*fdc->sc_data =
   1538 				bus_space_read_1(t, h, fdc->sc_reg_fifo);
   1539 		else
   1540 			bus_space_write_1(t, h, fdc->sc_reg_fifo,
   1541 					  *fdc->sc_data);
   1542 
   1543 		fdc->sc_data++;
   1544 		if (--fdc->sc_tc == 0) {
   1545 			FTC_FLIP;
   1546 			break;
   1547 		}
   1548 	}
   1549 	return 1;
   1550 }
   1551 
   1552 void
   1553 fdcswintr(void *arg)
   1554 {
   1555 	struct fdc_softc *fdc = arg;
   1556 
   1557 	if (fdc->sc_istatus == FDC_ISTATUS_NONE)
   1558 		/* This (software) interrupt is not for us */
   1559 		return;
   1560 
   1561 	switch (fdc->sc_istatus) {
   1562 	case FDC_ISTATUS_ERROR:
   1563 		printf("fdc: ierror status: state %d\n", fdc->sc_state);
   1564 		break;
   1565 	case FDC_ISTATUS_SPURIOUS:
   1566 		printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
   1567 		break;
   1568 	}
   1569 
   1570 	fdcstate(fdc);
   1571 	return;
   1572 }
   1573 
   1574 int
   1575 fdcstate(struct fdc_softc *fdc)
   1576 {
   1577 
   1578 #define	st0	fdc->sc_status[0]
   1579 #define	st1	fdc->sc_status[1]
   1580 #define	cyl	fdc->sc_status[1]
   1581 #define FDC_WRFIFO(fdc, c) do {			\
   1582 	if (fdc_wrfifo(fdc, (c))) {		\
   1583 		goto xxx;			\
   1584 	}					\
   1585 } while(0)
   1586 
   1587 	struct fd_softc *fd;
   1588 	struct buf *bp;
   1589 	int read, head, sec, nblks;
   1590 	struct fd_type *type;
   1591 	struct ne7_fd_formb *finfo = NULL;
   1592 
   1593 	if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
   1594 		/* Prevent loop if the reset sequence produces errors */
   1595 		if (fdc->sc_state != RESETCOMPLETE &&
   1596 		    fdc->sc_state != RECALWAIT &&
   1597 		    fdc->sc_state != RECALCOMPLETE)
   1598 			fdc->sc_state = DORESET;
   1599 	}
   1600 
   1601 	/* Clear I task/status field */
   1602 	fdc->sc_istatus = FDC_ISTATUS_NONE;
   1603 	fdc->sc_itask = FDC_ITASK_NONE;
   1604 
   1605 loop:
   1606 	/* Is there a drive for the controller to do a transfer with? */
   1607 	fd = fdc->sc_drives.tqh_first;
   1608 	if (fd == NULL) {
   1609 		fdc->sc_state = DEVIDLE;
   1610  		return 0;
   1611 	}
   1612 
   1613 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1614 	bp = BUFQ_PEEK(fd->sc_q);
   1615 	if (bp == NULL) {
   1616 		fd->sc_ops = 0;
   1617 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1618 		fd->sc_active = 0;
   1619 		goto loop;
   1620 	}
   1621 
   1622 	if (bp->b_flags & B_FORMAT)
   1623 		finfo = (struct ne7_fd_formb *)bp->b_data;
   1624 
   1625 	switch (fdc->sc_state) {
   1626 	case DEVIDLE:
   1627 		fdc->sc_errors = 0;
   1628 		fd->sc_skip = 0;
   1629 		fd->sc_bcount = bp->b_bcount;
   1630 		fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
   1631 		callout_stop(&fd->sc_motoroff_ch);
   1632 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1633 			fdc->sc_state = MOTORWAIT;
   1634 			return 1;
   1635 		}
   1636 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1637 			/* Turn on the motor, being careful about pairing. */
   1638 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1639 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1640 				callout_stop(&ofd->sc_motoroff_ch);
   1641 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1642 			}
   1643 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1644 			fd_set_motor(fdc);
   1645 			fdc->sc_state = MOTORWAIT;
   1646 			if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) { /*XXX*/
   1647 				/* Allow .25s for motor to stabilize. */
   1648 				callout_reset(&fd->sc_motoron_ch, hz / 4,
   1649 				    fd_motor_on, fd);
   1650 			} else {
   1651 				fd->sc_flags &= ~FD_MOTOR_WAIT;
   1652 				goto loop;
   1653 			}
   1654 			return 1;
   1655 		}
   1656 		/* Make sure the right drive is selected. */
   1657 		fd_set_motor(fdc);
   1658 
   1659 		if (fdc_diskchange(fdc))
   1660 			goto dodskchg;
   1661 
   1662 		/*FALLTHROUGH*/
   1663 	case DOSEEK:
   1664 	doseek:
   1665 		if ((fdc->sc_flags & FDC_EIS) &&
   1666 		    (bp->b_flags & B_FORMAT) == 0) {
   1667 			fd->sc_cylin = bp->b_cylinder;
   1668 			/* We use implied seek */
   1669 			goto doio;
   1670 		}
   1671 
   1672 		if (fd->sc_cylin == bp->b_cylinder)
   1673 			goto doio;
   1674 
   1675 		fd->sc_cylin = -1;
   1676 		fdc->sc_state = SEEKWAIT;
   1677 		fdc->sc_nstat = 0;
   1678 
   1679 		iostat_seek(fd->sc_dk.dk_stats);
   1680 
   1681 		disk_busy(&fd->sc_dk);
   1682 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1683 
   1684 		/* specify command */
   1685 		FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
   1686 		FDC_WRFIFO(fdc, fd->sc_type->steprate);
   1687 		/* XXX head load time == 6ms */
   1688 		FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
   1689 
   1690 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1691 		/* seek function */
   1692 		FDC_WRFIFO(fdc, NE7CMD_SEEK);
   1693 		FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
   1694 		FDC_WRFIFO(fdc, bp->b_cylinder * fd->sc_type->step);
   1695 		return 1;
   1696 
   1697 	case DODSKCHG:
   1698 	dodskchg:
   1699 		/*
   1700 		 * Disk change: force a seek operation by going to cyl 1
   1701 		 * followed by a recalibrate.
   1702 		 */
   1703 		disk_busy(&fd->sc_dk);
   1704 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1705 		fd->sc_cylin = -1;
   1706 		fdc->sc_nstat = 0;
   1707 		fdc->sc_state = DSKCHGWAIT;
   1708 
   1709 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1710 		/* seek function */
   1711 		FDC_WRFIFO(fdc, NE7CMD_SEEK);
   1712 		FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
   1713 		FDC_WRFIFO(fdc, 1 * fd->sc_type->step);
   1714 		return 1;
   1715 
   1716 	case DSKCHGWAIT:
   1717 		callout_stop(&fdc->sc_timo_ch);
   1718 		disk_unbusy(&fd->sc_dk, 0, 0);
   1719 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1720 		    cyl != 1 * fd->sc_type->step) {
   1721 			fdcstatus(fdc, "dskchg seek failed");
   1722 			fdc->sc_state = DORESET;
   1723 		} else
   1724 			fdc->sc_state = DORECAL;
   1725 
   1726 		if (fdc_diskchange(fdc)) {
   1727 			printf("%s: cannot clear disk change status\n",
   1728 				fdc->sc_dev.dv_xname);
   1729 			fdc->sc_state = DORESET;
   1730 		}
   1731 		goto loop;
   1732 
   1733 	case DOIO:
   1734 	doio:
   1735 		if (finfo != NULL)
   1736 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
   1737 				      (char *)finfo;
   1738 		type = fd->sc_type;
   1739 		sec = fd->sc_blkno % type->seccyl;
   1740 		nblks = type->seccyl - sec;
   1741 		nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
   1742 		nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
   1743 		fd->sc_nblks = nblks;
   1744 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
   1745 		head = sec / type->sectrac;
   1746 		sec -= head * type->sectrac;
   1747 #ifdef DIAGNOSTIC
   1748 		{int block;
   1749 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac +
   1750 			 sec;
   1751 		 if (block != fd->sc_blkno) {
   1752 			 printf("fdcintr: block %d != blkno %d\n", block,
   1753 			        (int)fd->sc_blkno);
   1754 #ifdef DDB
   1755 			 Debugger();
   1756 #endif
   1757 		 }}
   1758 #endif
   1759 		read = bp->b_flags & B_READ;
   1760 
   1761 		/* Setup for pseudo DMA */
   1762 		fdc->sc_data = (char *)bp->b_data + fd->sc_skip;
   1763 		fdc->sc_tc = fd->sc_nbytes;
   1764 
   1765 		bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
   1766 				  fdc->sc_reg_drs, type->rate);
   1767 #ifdef FD_DEBUG
   1768 		if (fdc_debug > 1)
   1769 			printf("fdcstate: doio: %s drive %d "
   1770 				"track %d head %d sec %d nblks %d\n",
   1771 				finfo ? "format" :
   1772 					(read ? "read" : "write"),
   1773 				fd->sc_drive, fd->sc_cylin, head, sec, nblks);
   1774 #endif
   1775 		fdc->sc_state = IOCOMPLETE;
   1776 		fdc->sc_itask = FDC_ITASK_DMA;
   1777 		fdc->sc_nstat = 0;
   1778 
   1779 		disk_busy(&fd->sc_dk);
   1780 
   1781 		/* allow 3 seconds for operation */
   1782 		callout_reset(&fdc->sc_timo_ch, 3 * hz, fdctimeout, fdc);
   1783 
   1784 		if (finfo != NULL) {
   1785 			/* formatting */
   1786 			FDC_WRFIFO(fdc, NE7CMD_FORMAT);
   1787 			FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
   1788 			FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
   1789 			FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
   1790 			FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
   1791 			FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
   1792 		} else {
   1793 			if (read)
   1794 				FDC_WRFIFO(fdc, NE7CMD_READ);
   1795 			else
   1796 				FDC_WRFIFO(fdc, NE7CMD_WRITE);
   1797 			FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
   1798 			FDC_WRFIFO(fdc, fd->sc_cylin);	/*track*/
   1799 			FDC_WRFIFO(fdc, head);
   1800 			FDC_WRFIFO(fdc, sec + 1);	/*sector+1*/
   1801 			FDC_WRFIFO(fdc, type->secsize);	/*sector size*/
   1802 			FDC_WRFIFO(fdc, type->sectrac);	/*secs/track*/
   1803 			FDC_WRFIFO(fdc, type->gap1);	/*gap1 size*/
   1804 			FDC_WRFIFO(fdc, type->datalen);	/*data length*/
   1805 		}
   1806 
   1807 		return 1;				/* will return later */
   1808 
   1809 	case SEEKWAIT:
   1810 		callout_stop(&fdc->sc_timo_ch);
   1811 		fdc->sc_state = SEEKCOMPLETE;
   1812 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1813 			/* allow 1/50 second for heads to settle */
   1814 			callout_reset(&fdc->sc_intr_ch, hz / 50,
   1815 			    fdcpseudointr, fdc);
   1816 			return 1;		/* will return later */
   1817 		}
   1818 		/*FALLTHROUGH*/
   1819 	case SEEKCOMPLETE:
   1820 		/* no data on seek */
   1821 		disk_unbusy(&fd->sc_dk, 0, 0);
   1822 
   1823 		/* Make sure seek really happened. */
   1824 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1825 		    cyl != bp->b_cylinder * fd->sc_type->step) {
   1826 #ifdef FD_DEBUG
   1827 			if (fdc_debug)
   1828 				fdcstatus(fdc, "seek failed");
   1829 #endif
   1830 			fdcretry(fdc);
   1831 			goto loop;
   1832 		}
   1833 		fd->sc_cylin = bp->b_cylinder;
   1834 		goto doio;
   1835 
   1836 	case IOTIMEDOUT:
   1837 		/*
   1838 		 * Try to abort the I/O operation without resetting
   1839 		 * the chip first.  Poke TC and arrange to pick up
   1840 		 * the timed out I/O command's status.
   1841 		 */
   1842 		fdc->sc_itask = FDC_ITASK_RESULT;
   1843 		fdc->sc_state = IOCLEANUPWAIT;
   1844 		fdc->sc_nstat = 0;
   1845 		/* 1/10 second should be enough */
   1846 		callout_reset(&fdc->sc_timo_ch, hz / 10, fdctimeout, fdc);
   1847 		FTC_FLIP;
   1848 		return 1;
   1849 
   1850 	case IOCLEANUPTIMEDOUT:
   1851 	case SEEKTIMEDOUT:
   1852 	case RECALTIMEDOUT:
   1853 	case RESETTIMEDOUT:
   1854 	case DSKCHGTIMEDOUT:
   1855 		fdcstatus(fdc, "timeout");
   1856 
   1857 		/* All other timeouts always roll through to a chip reset */
   1858 		fdcretry(fdc);
   1859 
   1860 		/* Force reset, no matter what fdcretry() says */
   1861 		fdc->sc_state = DORESET;
   1862 		goto loop;
   1863 
   1864 	case IOCLEANUPWAIT: /* IO FAILED, cleanup succeeded */
   1865 		callout_stop(&fdc->sc_timo_ch);
   1866 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
   1867 		    (bp->b_flags & B_READ));
   1868 		fdcretry(fdc);
   1869 		goto loop;
   1870 
   1871 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1872 		callout_stop(&fdc->sc_timo_ch);
   1873 
   1874 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
   1875 		    (bp->b_flags & B_READ));
   1876 
   1877 		if (fdc->sc_nstat != 7 || st1 != 0 ||
   1878 		    ((st0 & 0xf8) != 0 &&
   1879 		     ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
   1880 #ifdef FD_DEBUG
   1881 			if (fdc_debug) {
   1882 				fdcstatus(fdc, bp->b_flags & B_READ ?
   1883 				     "read failed" : "write failed");
   1884 				printf("blkno %lld nblks %d nstat %d tc %d\n",
   1885 				       (long long)fd->sc_blkno, fd->sc_nblks,
   1886 				       fdc->sc_nstat, fdc->sc_tc);
   1887 			}
   1888 #endif
   1889 			if (fdc->sc_nstat == 7 &&
   1890 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
   1891 
   1892 				/*
   1893 				 * Silently retry overruns if no other
   1894 				 * error bit is set. Adjust threshold.
   1895 				 */
   1896 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1897 				if (thr < 15) {
   1898 					thr++;
   1899 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1900 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1901 #ifdef FD_DEBUG
   1902 					if (fdc_debug)
   1903 						printf("fdc: %d -> threshold\n",
   1904 						       thr);
   1905 #endif
   1906 					fdconf(fdc);
   1907 					fdc->sc_overruns = 0;
   1908 				}
   1909 				if (++fdc->sc_overruns < 3) {
   1910 					fdc->sc_state = DOIO;
   1911 					goto loop;
   1912 				}
   1913 			}
   1914 			fdcretry(fdc);
   1915 			goto loop;
   1916 		}
   1917 		if (fdc->sc_errors) {
   1918 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1919 			    fd->sc_skip / FD_BSIZE(fd),
   1920 			    (struct disklabel *)NULL);
   1921 			printf("\n");
   1922 			fdc->sc_errors = 0;
   1923 		} else {
   1924 			if (--fdc->sc_overruns < -20) {
   1925 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1926 				if (thr > 0) {
   1927 					thr--;
   1928 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1929 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1930 #ifdef FD_DEBUG
   1931 					if (fdc_debug)
   1932 						printf("fdc: %d -> threshold\n",
   1933 						       thr);
   1934 #endif
   1935 					fdconf(fdc);
   1936 				}
   1937 				fdc->sc_overruns = 0;
   1938 			}
   1939 		}
   1940 		fd->sc_blkno += fd->sc_nblks;
   1941 		fd->sc_skip += fd->sc_nbytes;
   1942 		fd->sc_bcount -= fd->sc_nbytes;
   1943 		if (finfo == NULL && fd->sc_bcount > 0) {
   1944 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
   1945 			goto doseek;
   1946 		}
   1947 		fdfinish(fd, bp);
   1948 		goto loop;
   1949 
   1950 	case DORESET:
   1951 		/* try a reset, keep motor on */
   1952 		fd_set_motor(fdc);
   1953 		delay(100);
   1954 		fdc->sc_nstat = 0;
   1955 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1956 		fdc->sc_state = RESETCOMPLETE;
   1957 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
   1958 		fdc_reset(fdc);
   1959 		return 1;			/* will return later */
   1960 
   1961 	case RESETCOMPLETE:
   1962 		callout_stop(&fdc->sc_timo_ch);
   1963 		fdconf(fdc);
   1964 
   1965 		/* FALLTHROUGH */
   1966 	case DORECAL:
   1967 		fdc->sc_state = RECALWAIT;
   1968 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1969 		fdc->sc_nstat = 0;
   1970 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
   1971 		/* recalibrate function */
   1972 		FDC_WRFIFO(fdc, NE7CMD_RECAL);
   1973 		FDC_WRFIFO(fdc, fd->sc_drive);
   1974 		return 1;			/* will return later */
   1975 
   1976 	case RECALWAIT:
   1977 		callout_stop(&fdc->sc_timo_ch);
   1978 		fdc->sc_state = RECALCOMPLETE;
   1979 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1980 			/* allow 1/30 second for heads to settle */
   1981 			callout_reset(&fdc->sc_intr_ch, hz / 30,
   1982 			    fdcpseudointr, fdc);
   1983 			return 1;		/* will return later */
   1984 		}
   1985 
   1986 	case RECALCOMPLETE:
   1987 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1988 #ifdef FD_DEBUG
   1989 			if (fdc_debug)
   1990 				fdcstatus(fdc, "recalibrate failed");
   1991 #endif
   1992 			fdcretry(fdc);
   1993 			goto loop;
   1994 		}
   1995 		fd->sc_cylin = 0;
   1996 		goto doseek;
   1997 
   1998 	case MOTORWAIT:
   1999 		if (fd->sc_flags & FD_MOTOR_WAIT)
   2000 			return 1;		/* time's not up yet */
   2001 		goto doseek;
   2002 
   2003 	default:
   2004 		fdcstatus(fdc, "stray interrupt");
   2005 		return 1;
   2006 	}
   2007 #ifdef DIAGNOSTIC
   2008 	panic("fdcintr: impossible");
   2009 #endif
   2010 
   2011 xxx:
   2012 	/*
   2013 	 * We get here if the chip locks up in FDC_WRFIFO()
   2014 	 * Cancel any operation and schedule a reset
   2015 	 */
   2016 	callout_stop(&fdc->sc_timo_ch);
   2017 	fdcretry(fdc);
   2018 	fdc->sc_state = DORESET;
   2019 	goto loop;
   2020 
   2021 #undef	st0
   2022 #undef	st1
   2023 #undef	cyl
   2024 }
   2025 
   2026 void
   2027 fdcretry(struct fdc_softc *fdc)
   2028 {
   2029 	struct fd_softc *fd;
   2030 	struct buf *bp;
   2031 	int error = EIO;
   2032 
   2033 	fd = fdc->sc_drives.tqh_first;
   2034 	bp = BUFQ_PEEK(fd->sc_q);
   2035 
   2036 	fdc->sc_overruns = 0;
   2037 	if (fd->sc_opts & FDOPT_NORETRY)
   2038 		goto fail;
   2039 
   2040 	switch (fdc->sc_errors) {
   2041 	case 0:
   2042 		if (fdc->sc_nstat == 7 &&
   2043 		    (fdc->sc_status[0] & 0xd8) == 0x40 &&
   2044 		    (fdc->sc_status[1] & 0x2) == 0x2) {
   2045 			printf("%s: read-only medium\n", fd->sc_dv.dv_xname);
   2046 			error = EROFS;
   2047 			goto failsilent;
   2048 		}
   2049 		/* try again */
   2050 		fdc->sc_state =
   2051 			(fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
   2052 		break;
   2053 
   2054 	case 1: case 2: case 3:
   2055 		/* didn't work; try recalibrating */
   2056 		fdc->sc_state = DORECAL;
   2057 		break;
   2058 
   2059 	case 4:
   2060 		if (fdc->sc_nstat == 7 &&
   2061 		    fdc->sc_status[0] == 0 &&
   2062 		    fdc->sc_status[1] == 0 &&
   2063 		    fdc->sc_status[2] == 0) {
   2064 			/*
   2065 			 * We've retried a few times and we've got
   2066 			 * valid status and all three status bytes
   2067 			 * are zero.  Assume this condition is the
   2068 			 * result of no disk loaded into the drive.
   2069 			 */
   2070 			printf("%s: no medium?\n", fd->sc_dv.dv_xname);
   2071 			error = ENODEV;
   2072 			goto failsilent;
   2073 		}
   2074 
   2075 		/* still no go; reset the bastard */
   2076 		fdc->sc_state = DORESET;
   2077 		break;
   2078 
   2079 	default:
   2080 	fail:
   2081 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
   2082 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
   2083 				fd->sc_skip / FD_BSIZE(fd),
   2084 				(struct disklabel *)NULL);
   2085 			printf("\n");
   2086 			fdcstatus(fdc, "controller status");
   2087 		}
   2088 
   2089 	failsilent:
   2090 		bp->b_flags |= B_ERROR;
   2091 		bp->b_error = error;
   2092 		fdfinish(fd, bp);
   2093 	}
   2094 	fdc->sc_errors++;
   2095 }
   2096 
   2097 int
   2098 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   2099 {
   2100 	struct fd_softc *fd;
   2101 	struct fdc_softc *fdc;
   2102 	struct fdformat_parms *form_parms;
   2103 	struct fdformat_cmd *form_cmd;
   2104 	struct ne7_fd_formb *fd_formb;
   2105 	int il[FD_MAX_NSEC + 1];
   2106 	int unit;
   2107 	int i, j;
   2108 	int error;
   2109 
   2110 	unit = FDUNIT(dev);
   2111 	if (unit >= fd_cd.cd_ndevs)
   2112 		return ENXIO;
   2113 
   2114 	fd = fd_cd.cd_devs[FDUNIT(dev)];
   2115 	fdc = (struct fdc_softc *)device_parent(&fd->sc_dv);
   2116 
   2117 	switch (cmd) {
   2118 	case DIOCGDINFO:
   2119 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
   2120 		return 0;
   2121 
   2122 	case DIOCWLABEL:
   2123 		if ((flag & FWRITE) == 0)
   2124 			return EBADF;
   2125 		/* XXX do something */
   2126 		return 0;
   2127 
   2128 	case DIOCWDINFO:
   2129 		if ((flag & FWRITE) == 0)
   2130 			return EBADF;
   2131 
   2132 		error = setdisklabel(fd->sc_dk.dk_label,
   2133 				    (struct disklabel *)addr, 0,
   2134 				    fd->sc_dk.dk_cpulabel);
   2135 		if (error)
   2136 			return error;
   2137 
   2138 		error = writedisklabel(dev, fdstrategy,
   2139 				       fd->sc_dk.dk_label,
   2140 				       fd->sc_dk.dk_cpulabel);
   2141 		return error;
   2142 
   2143 	case DIOCLOCK:
   2144 		/*
   2145 		 * Nothing to do here, really.
   2146 		 */
   2147 		return 0;
   2148 
   2149 	case DIOCEJECT:
   2150 		if (*(int *)addr == 0) {
   2151 			int part = DISKPART(dev);
   2152 			/*
   2153 			 * Don't force eject: check that we are the only
   2154 			 * partition open. If so, unlock it.
   2155 			 */
   2156 			if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
   2157 			    fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
   2158 			    fd->sc_dk.dk_openmask) {
   2159 				return EBUSY;
   2160 			}
   2161 		}
   2162 		/* FALLTHROUGH */
   2163 	case ODIOCEJECT:
   2164 		if (fdc->sc_flags & FDC_NOEJECT)
   2165 			return EINVAL;
   2166 		fd_do_eject(fd);
   2167 		return 0;
   2168 
   2169 	case FDIOCGETFORMAT:
   2170 		form_parms = (struct fdformat_parms *)addr;
   2171 		form_parms->fdformat_version = FDFORMAT_VERSION;
   2172 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
   2173 		form_parms->ncyl = fd->sc_type->cylinders;
   2174 		form_parms->nspt = fd->sc_type->sectrac;
   2175 		form_parms->ntrk = fd->sc_type->heads;
   2176 		form_parms->stepspercyl = fd->sc_type->step;
   2177 		form_parms->gaplen = fd->sc_type->gap2;
   2178 		form_parms->fillbyte = fd->sc_type->fillbyte;
   2179 		form_parms->interleave = fd->sc_type->interleave;
   2180 		switch (fd->sc_type->rate) {
   2181 		case FDC_500KBPS:
   2182 			form_parms->xfer_rate = 500 * 1024;
   2183 			break;
   2184 		case FDC_300KBPS:
   2185 			form_parms->xfer_rate = 300 * 1024;
   2186 			break;
   2187 		case FDC_250KBPS:
   2188 			form_parms->xfer_rate = 250 * 1024;
   2189 			break;
   2190 		default:
   2191 			return EINVAL;
   2192 		}
   2193 		return 0;
   2194 
   2195 	case FDIOCSETFORMAT:
   2196 		if ((flag & FWRITE) == 0)
   2197 			return EBADF;	/* must be opened for writing */
   2198 
   2199 		form_parms = (struct fdformat_parms *)addr;
   2200 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
   2201 			return EINVAL;/* wrong version of formatting prog */
   2202 
   2203 		i = form_parms->nbps >> 7;
   2204 		if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
   2205 		    i & ~(1 << (ffs(i)-1)))
   2206 			/* not a power-of-two multiple of 128 */
   2207 			return EINVAL;
   2208 
   2209 		switch (form_parms->xfer_rate) {
   2210 		case 500 * 1024:
   2211 			fd->sc_type->rate = FDC_500KBPS;
   2212 			break;
   2213 		case 300 * 1024:
   2214 			fd->sc_type->rate = FDC_300KBPS;
   2215 			break;
   2216 		case 250 * 1024:
   2217 			fd->sc_type->rate = FDC_250KBPS;
   2218 			break;
   2219 		default:
   2220 			return EINVAL;
   2221 		}
   2222 
   2223 		if (form_parms->nspt > FD_MAX_NSEC ||
   2224 		    form_parms->fillbyte > 0xff ||
   2225 		    form_parms->interleave > 0xff)
   2226 			return EINVAL;
   2227 		fd->sc_type->sectrac = form_parms->nspt;
   2228 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
   2229 			return EINVAL;
   2230 		fd->sc_type->heads = form_parms->ntrk;
   2231 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
   2232 		fd->sc_type->secsize = ffs(i)-1;
   2233 		fd->sc_type->gap2 = form_parms->gaplen;
   2234 		fd->sc_type->cylinders = form_parms->ncyl;
   2235 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
   2236 			form_parms->nbps / DEV_BSIZE;
   2237 		fd->sc_type->step = form_parms->stepspercyl;
   2238 		fd->sc_type->fillbyte = form_parms->fillbyte;
   2239 		fd->sc_type->interleave = form_parms->interleave;
   2240 		return 0;
   2241 
   2242 	case FDIOCFORMAT_TRACK:
   2243 		if((flag & FWRITE) == 0)
   2244 			/* must be opened for writing */
   2245 			return EBADF;
   2246 		form_cmd = (struct fdformat_cmd *)addr;
   2247 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
   2248 			/* wrong version of formatting prog */
   2249 			return EINVAL;
   2250 
   2251 		if (form_cmd->head >= fd->sc_type->heads ||
   2252 		    form_cmd->cylinder >= fd->sc_type->cylinders) {
   2253 			return EINVAL;
   2254 		}
   2255 
   2256 		fd_formb = malloc(sizeof(struct ne7_fd_formb),
   2257 		    M_TEMP, M_NOWAIT);
   2258 		if (fd_formb == 0)
   2259 			return ENOMEM;
   2260 
   2261 		fd_formb->head = form_cmd->head;
   2262 		fd_formb->cyl = form_cmd->cylinder;
   2263 		fd_formb->transfer_rate = fd->sc_type->rate;
   2264 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
   2265 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
   2266 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
   2267 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
   2268 
   2269 		bzero(il, sizeof il);
   2270 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
   2271 			while (il[(j % fd_formb->fd_formb_nsecs) + 1])
   2272 				j++;
   2273 			il[(j % fd_formb->fd_formb_nsecs) + 1] = i;
   2274 			j += fd->sc_type->interleave;
   2275 		}
   2276 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
   2277 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
   2278 			fd_formb->fd_formb_headno(i) = form_cmd->head;
   2279 			fd_formb->fd_formb_secno(i) = il[i + 1];
   2280 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
   2281 		}
   2282 
   2283 		error = fdformat(dev, fd_formb, l->l_proc);
   2284 		free(fd_formb, M_TEMP);
   2285 		return error;
   2286 
   2287 	case FDIOCGETOPTS:		/* get drive options */
   2288 		*(int *)addr = fd->sc_opts;
   2289 		return 0;
   2290 
   2291 	case FDIOCSETOPTS:		/* set drive options */
   2292 		fd->sc_opts = *(int *)addr;
   2293 		return 0;
   2294 
   2295 #ifdef FD_DEBUG
   2296 	case _IO('f', 100):
   2297 		fdc_wrfifo(fdc, NE7CMD_DUMPREG);
   2298 		fdcresult(fdc);
   2299 		printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
   2300 		for (i = 0; i < fdc->sc_nstat; i++)
   2301 			printf(" 0x%x", fdc->sc_status[i]);
   2302 		printf(">\n");
   2303 		return 0;
   2304 
   2305 	case _IOW('f', 101, int):
   2306 		fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   2307 		fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
   2308 		fdconf(fdc);
   2309 		return 0;
   2310 
   2311 	case _IO('f', 102):
   2312 		fdc_wrfifo(fdc, NE7CMD_SENSEI);
   2313 		fdcresult(fdc);
   2314 		printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
   2315 		for (i=0; i< fdc->sc_nstat; i++)
   2316 			printf(" 0x%x", fdc->sc_status[i]);
   2317 		printf(">\n");
   2318 		return 0;
   2319 #endif
   2320 	default:
   2321 		return ENOTTY;
   2322 	}
   2323 
   2324 #ifdef DIAGNOSTIC
   2325 	panic("fdioctl: impossible");
   2326 #endif
   2327 }
   2328 
   2329 int
   2330 fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct proc *p)
   2331 {
   2332 	int rv = 0;
   2333 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   2334 	struct fd_type *type = fd->sc_type;
   2335 	struct buf *bp;
   2336 
   2337 	/* set up a buffer header for fdstrategy() */
   2338 	bp = getiobuf_nowait();
   2339 	if (bp == NULL)
   2340 		return ENOBUFS;
   2341 
   2342 	bp->b_vp = NULL;
   2343 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
   2344 	bp->b_proc = p;
   2345 	bp->b_dev = dev;
   2346 
   2347 	/*
   2348 	 * Calculate a fake blkno, so fdstrategy() would initiate a
   2349 	 * seek to the requested cylinder.
   2350 	 */
   2351 	bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
   2352 		       + finfo->head * type->sectrac) * FD_BSIZE(fd))
   2353 		      / DEV_BSIZE;
   2354 
   2355 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
   2356 	bp->b_data = (void *)finfo;
   2357 
   2358 #ifdef FD_DEBUG
   2359 	if (fdc_debug) {
   2360 		int i;
   2361 
   2362 		printf("fdformat: blkno 0x%llx count %d\n",
   2363 			(unsigned long long)bp->b_blkno, bp->b_bcount);
   2364 
   2365 		printf("\tcyl:\t%d\n", finfo->cyl);
   2366 		printf("\thead:\t%d\n", finfo->head);
   2367 		printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
   2368 		printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
   2369 		printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
   2370 		printf("\ttrack data:");
   2371 		for (i = 0; i < finfo->fd_formb_nsecs; i++) {
   2372 			printf(" [c%d h%d s%d]",
   2373 					finfo->fd_formb_cylno(i),
   2374 					finfo->fd_formb_headno(i),
   2375 					finfo->fd_formb_secno(i) );
   2376 			if (finfo->fd_formb_secsize(i) != 2)
   2377 				printf("<sz:%d>", finfo->fd_formb_secsize(i));
   2378 		}
   2379 		printf("\n");
   2380 	}
   2381 #endif
   2382 
   2383 	/* now do the format */
   2384 	fdstrategy(bp);
   2385 
   2386 	/* ...and wait for it to complete */
   2387 	rv = biowait(bp);
   2388 	putiobuf(bp);
   2389 	return rv;
   2390 }
   2391 
   2392 void
   2393 fdgetdisklabel(dev_t dev)
   2394 {
   2395 	int unit = FDUNIT(dev), i;
   2396 	struct fd_softc *fd = fd_cd.cd_devs[unit];
   2397 	struct disklabel *lp = fd->sc_dk.dk_label;
   2398 	struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
   2399 
   2400 	bzero(lp, sizeof(struct disklabel));
   2401 	bzero(lp, sizeof(struct cpu_disklabel));
   2402 
   2403 	lp->d_type = DTYPE_FLOPPY;
   2404 	lp->d_secsize = FD_BSIZE(fd);
   2405 	lp->d_secpercyl = fd->sc_type->seccyl;
   2406 	lp->d_nsectors = fd->sc_type->sectrac;
   2407 	lp->d_ncylinders = fd->sc_type->cylinders;
   2408 	lp->d_ntracks = fd->sc_type->heads;	/* Go figure... */
   2409 	lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
   2410 	lp->d_rpm = 300;	/* XXX like it matters... */
   2411 
   2412 	strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
   2413 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   2414 	lp->d_interleave = 1;
   2415 	lp->d_flags = D_REMOVABLE;
   2416 
   2417 	lp->d_partitions[RAW_PART].p_offset = 0;
   2418 	lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
   2419 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   2420 	lp->d_npartitions = RAW_PART + 1;
   2421 
   2422 	lp->d_magic = DISKMAGIC;
   2423 	lp->d_magic2 = DISKMAGIC;
   2424 	lp->d_checksum = dkcksum(lp);
   2425 
   2426 	/*
   2427 	 * Call the generic disklabel extraction routine.  If there's
   2428 	 * not a label there, fake it.
   2429 	 */
   2430 	if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
   2431 		strncpy(lp->d_packname, "default label",
   2432 		    sizeof(lp->d_packname));
   2433 		/*
   2434 		 * Reset the partition info; it might have gotten
   2435 		 * trashed in readdisklabel().
   2436 		 *
   2437 		 * XXX Why do we have to do this?  readdisklabel()
   2438 		 * should be safe...
   2439 		 */
   2440 		for (i = 0; i < MAXPARTITIONS; ++i) {
   2441 			lp->d_partitions[i].p_offset = 0;
   2442 			if (i == RAW_PART) {
   2443 				lp->d_partitions[i].p_size =
   2444 				    lp->d_secpercyl * lp->d_ncylinders;
   2445 				lp->d_partitions[i].p_fstype = FS_BSDFFS;
   2446 			} else {
   2447 				lp->d_partitions[i].p_size = 0;
   2448 				lp->d_partitions[i].p_fstype = FS_UNUSED;
   2449 			}
   2450 		}
   2451 		lp->d_npartitions = RAW_PART + 1;
   2452 	}
   2453 }
   2454 
   2455 void
   2456 fd_do_eject(struct fd_softc *fd)
   2457 {
   2458 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   2459 
   2460 #ifdef SUN4
   2461 	if (CPU_ISSUN4C) {
   2462 		auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
   2463 		delay(10);
   2464 		auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
   2465 		return;
   2466 	}
   2467 	if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
   2468 #endif
   2469 		bus_space_tag_t t = fdc->sc_bustag;
   2470 		bus_space_handle_t h = fdc->sc_handle;
   2471 		uint8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
   2472 
   2473 		bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
   2474 		delay(10);
   2475 		bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
   2476 		return;
   2477 #ifdef SUN4
   2478 	}
   2479 #endif
   2480 }
   2481 
   2482 /* ARGSUSED */
   2483 void
   2484 fd_mountroot_hook(struct device *dev)
   2485 {
   2486 	int c;
   2487 
   2488 	fd_do_eject((struct fd_softc *)dev);
   2489 	printf("Insert filesystem floppy and press return.");
   2490 	for (;;) {
   2491 		c = cngetc();
   2492 		if ((c == '\r') || (c == '\n')) {
   2493 			printf("\n");
   2494 			break;
   2495 		}
   2496 	}
   2497 }
   2498 
   2499 #ifdef MEMORY_DISK_HOOKS
   2500 
   2501 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
   2502 
   2503 int
   2504 fd_read_md_image(size_t	*sizep, void **addrp)
   2505 {
   2506 	struct buf buf, *bp = &buf;
   2507 	dev_t dev;
   2508 	off_t offset;
   2509 	char *addr;
   2510 
   2511 	dev = makedev(54,0);	/* XXX */
   2512 
   2513 	MALLOC(addr, void *, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
   2514 	*addrp = addr;
   2515 
   2516 	if (fdopen(dev, 0, S_IFCHR, NULL))
   2517 		panic("fd: mountroot: fdopen");
   2518 
   2519 	offset = 0;
   2520 
   2521 	for (;;) {
   2522 		bp->b_dev = dev;
   2523 		bp->b_error = 0;
   2524 		bp->b_resid = 0;
   2525 		bp->b_proc = NULL;
   2526 		bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
   2527 		bp->b_blkno = btodb(offset);
   2528 		bp->b_bcount = DEV_BSIZE;
   2529 		bp->b_data = addr;
   2530 		fdstrategy(bp);
   2531 		while ((bp->b_flags & B_DONE) == 0) {
   2532 			tsleep((void *)bp, PRIBIO + 1, "physio", 0);
   2533 		}
   2534 		if (bp->b_error)
   2535 			panic("fd: mountroot: fdread error %d", bp->b_error);
   2536 
   2537 		if (bp->b_resid != 0)
   2538 			break;
   2539 
   2540 		addr += DEV_BSIZE;
   2541 		offset += DEV_BSIZE;
   2542 		if (offset + DEV_BSIZE > FDMICROROOTSIZE)
   2543 			break;
   2544 	}
   2545 	(void)fdclose(dev, 0, S_IFCHR, NULL);
   2546 	*sizep = offset;
   2547 	fd_do_eject(fd_cd.cd_devs[FDUNIT(dev)]);
   2548 	return 0;
   2549 }
   2550 #endif /* MEMORY_DISK_HOOKS */
   2551 
   2552 static void
   2553 fd_set_properties(struct fd_softc *fd)
   2554 {
   2555 	prop_dictionary_t disk_info, odisk_info, geom;
   2556 	struct fd_type *fdt;
   2557 	int secsize;
   2558 
   2559 	fdt = fd->sc_deftype;
   2560 
   2561 	disk_info = prop_dictionary_create();
   2562 
   2563 	geom = prop_dictionary_create();
   2564 
   2565 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
   2566 	    fdt->size);
   2567 
   2568 	switch (fdt->secsize) {
   2569 	case 2:
   2570 		secsize = 512;
   2571 		break;
   2572 	case 3:
   2573 		secsize = 1024;
   2574 		break;
   2575 	default:
   2576 		secsize = 0;
   2577 	}
   2578 
   2579 	prop_dictionary_set_uint32(geom, "sector-size",
   2580 	    secsize);
   2581 
   2582 	prop_dictionary_set_uint16(geom, "sectors-per-track",
   2583 	    fdt->sectrac);
   2584 
   2585 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
   2586 	    fdt->heads);
   2587 
   2588 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
   2589 	    fdt->cylinders);
   2590 
   2591 	prop_dictionary_set(disk_info, "geometry", geom);
   2592 	prop_object_release(geom);
   2593 
   2594 	prop_dictionary_set(device_properties(&fd->sc_dv),
   2595 	    "disk-info", disk_info);
   2596 
   2597 	/*
   2598 	 * Don't release disk_info here; we keep a reference to it.
   2599 	 * disk_detach() will release it when we go away.
   2600 	 */
   2601 
   2602 	odisk_info = fd->sc_dk.dk_info;
   2603 	fd->sc_dk.dk_info = disk_info;
   2604 	if (odisk_info)
   2605 		prop_object_release(odisk_info);
   2606 }
   2607