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