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