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