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