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