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