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