xref: /netbsd-src/sys/arch/sun3/dev/fd.c (revision 89c5a767f8fc7a4633b2d409966e2becbb98ff92)
1 /*	$NetBSD: fd.c,v 1.15 2000/02/07 20:16:53 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
5  * Copyright (c) 1995 Paul Kranenburg.
6  * Copyright (c) 1990 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Don Ahn.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
41  */
42 #include "opt_ddb.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/file.h>
48 #include <sys/ioctl.h>
49 #include <sys/device.h>
50 #include <sys/disklabel.h>
51 #include <sys/dkstat.h>
52 #include <sys/disk.h>
53 #include <sys/fdio.h>
54 #include <sys/buf.h>
55 #include <sys/malloc.h>
56 #include <sys/proc.h>
57 #include <sys/uio.h>
58 #include <sys/stat.h>
59 #include <sys/syslog.h>
60 #include <sys/queue.h>
61 #include <sys/conf.h>
62 
63 #include <dev/cons.h>
64 
65 #include <machine/cpu.h>
66 #include <machine/autoconf.h>
67 
68 #include <sun3/dev/fdreg.h>
69 #include <sun3/dev/fdvar.h>
70 
71 /*
72  * Print a complaint when no fd children were specified
73  * in the config file.  Better than a link error...
74  *
75  * XXX: Some folks say this driver should be split in two,
76  * but that seems pointless with ONLY one type of child.
77  * (Thankfully, no 3/80 boxes have floppy tapes!:)
78  */
79 #include "fdc.h"
80 #if NFD == 0
81 #error "fdc but no fd?"
82 #endif
83 
84 #define FDUNIT(dev)	(minor(dev) / 8)
85 #define FDTYPE(dev)	(minor(dev) % 8)
86 
87 /* XXX misuse a flag to identify format operation */
88 #define B_FORMAT B_XXX
89 
90 #ifdef FD_DEBUG
91 int	fdc_debug = 0;
92 #endif
93 
94 enum fdc_state {
95 	DEVIDLE = 0,
96 	MOTORWAIT,
97 	DOSEEK,
98 	SEEKWAIT,
99 	SEEKTIMEDOUT,
100 	SEEKCOMPLETE,
101 	DOIO,
102 	IOCOMPLETE,
103 	IOTIMEDOUT,
104 	DORESET,
105 	RESETCOMPLETE,
106 	RESETTIMEDOUT,
107 	DORECAL,
108 	RECALWAIT,
109 	RECALTIMEDOUT,
110 	RECALCOMPLETE,
111 };
112 
113 /* software state, per controller */
114 struct fdc_softc {
115 	struct device	sc_dev;		/* boilerplate */
116 	caddr_t		sc_reg;
117 	struct fd_softc *sc_fd[4];	/* pointers to children */
118 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
119 	enum fdc_state	sc_state;
120 	int		sc_flags;
121 #define FDC_82077		0x01
122 #define FDC_NEEDHEADSETTLE	0x02
123 #define FDC_EIS			0x04
124 	int		sc_errors;		/* number of retries so far */
125 	int		sc_overruns;		/* number of DMA overruns */
126 	int		sc_cfg;			/* current configuration */
127 	int		sc_fcr;			/* current image of floppy ctrlr reg. */
128 	struct fdcio	sc_io;
129 #define sc_reg_msr	sc_io.fdcio_reg_msr
130 #define sc_reg_fifo	sc_io.fdcio_reg_fifo
131 #define sc_reg_fcr	sc_io.fdcio_reg_fcr
132 #define	sc_reg_fvr	sc_io.fdcio_reg_fvr
133 #define sc_reg_drs	sc_io.fdcio_reg_msr
134 #define sc_istate	sc_io.fdcio_istate
135 #define sc_data		sc_io.fdcio_data
136 #define sc_tc		sc_io.fdcio_tc
137 #define sc_nstat	sc_io.fdcio_nstat
138 #define sc_status	sc_io.fdcio_status
139 #define sc_intrcnt	sc_io.fdcio_intrcnt
140 };
141 
142 /* controller driver configuration */
143 int	fdcmatch __P((struct device *, struct cfdata *, void *));
144 void	fdcattach __P((struct device *, struct device *, void *));
145 
146 struct cfattach fdc_ca = {
147 	sizeof(struct fdc_softc), fdcmatch, fdcattach
148 };
149 
150 extern struct cfdriver fdc_cd;
151 
152 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
153 
154 /*
155  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
156  * we tell them apart.
157  */
158 struct fd_type {
159 	int	sectrac;	/* sectors per track */
160 	int	heads;		/* number of heads */
161 	int	seccyl;		/* sectors per cylinder */
162 	int	secsize;	/* size code for sectors */
163 	int	datalen;	/* data len when secsize = 0 */
164 	int	steprate;	/* step rate and head unload time */
165 	int	gap1;		/* gap len between sectors */
166 	int	gap2;		/* formatting gap */
167 	int	tracks;		/* total num of tracks */
168 	int	size;		/* size of disk in sectors */
169 	int	step;		/* steps per cylinder */
170 	int	rate;		/* transfer speed code */
171 	int	fillbyte;	/* format fill byte */
172 	int	interleave;	/* interleave factor (formatting) */
173 	char	*name;
174 };
175 
176 /* The order of entries in the following table is important -- BEWARE! */
177 struct fd_type fd_types[] = {
178 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette */
179 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB"    }, /* 1.2 MB AT-diskettes */
180 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
181 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
182 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5" 720kB diskette */
183 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x"  }, /* 720kB in 1.2MB drive */
184 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
185 };
186 
187 /* software state, per disk (with up to 4 disks per ctlr) */
188 struct fd_softc {
189 	struct device	sc_dv;		/* generic device info */
190 	struct disk	sc_dk;		/* generic disk info */
191 
192 	struct fd_type *sc_deftype;	/* default type descriptor */
193 	struct fd_type *sc_type;	/* current type descriptor */
194 
195 	daddr_t	sc_blkno;	/* starting block number */
196 	int sc_bcount;		/* byte count left */
197 	int sc_skip;		/* bytes already transferred */
198 	int sc_nblks;		/* number of blocks currently tranferring */
199 	int sc_nbytes;		/* number of bytes currently tranferring */
200 
201 	int sc_drive;		/* physical unit number */
202 	int sc_flags;
203 #define	FD_OPEN		0x01		/* it's open */
204 #define	FD_MOTOR	0x02		/* motor should be on */
205 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
206 	int sc_cylin;		/* where we think the head is */
207 	int sc_opts;		/* user-set options */
208 
209 	void	*sc_sdhook;	/* shutdownhook cookie */
210 
211 	TAILQ_ENTRY(fd_softc) sc_drivechain;
212 	int sc_ops;		/* I/O ops since last switch */
213 	struct buf_queue sc_q;	/* pending I/O requests */
214 	int sc_active;		/* number of active I/O operations */
215 };
216 
217 bdev_decl(fd);
218 cdev_decl(fd);
219 
220 /* floppy driver configuration */
221 int	fdmatch __P((struct device *, struct cfdata *, void *));
222 void	fdattach __P((struct device *, struct device *, void *));
223 
224 struct cfattach fd_ca = {
225 	sizeof(struct fd_softc), fdmatch, fdattach
226 };
227 
228 extern struct cfdriver fd_cd;
229 
230 void fdgetdisklabel __P((dev_t));
231 int fd_get_parms __P((struct fd_softc *));
232 void fdstrategy __P((struct buf *));
233 void fdstart __P((struct fd_softc *));
234 int fdprint __P((void *, const char *));
235 
236 struct dkdriver fddkdriver = { fdstrategy };
237 
238 struct	fd_type *fd_nvtotype __P((char *, int, int));
239 void	fd_set_motor __P((struct fdc_softc *fdc));
240 void	fd_motor_off __P((void *arg));
241 void	fd_motor_on __P((void *arg));
242 int	fdcresult __P((struct fdc_softc *fdc));
243 int	out_fdc __P((struct fdc_softc *fdc, u_char x));
244 void	fdcstart __P((struct fdc_softc *fdc));
245 void	fdcstatus __P((struct device *dv, int n, char *s));
246 void	fdc_reset __P((struct fdc_softc *fdc));
247 void	fdctimeout __P((void *arg));
248 void	fdcpseudointr __P((void *arg));
249 int	fdchwintr __P((void *));
250 int	fdcswintr __P((void *));
251 int	fdcstate __P((struct fdc_softc *));
252 void	fdcretry __P((struct fdc_softc *fdc));
253 void	fdfinish __P((struct fd_softc *fd, struct buf *bp));
254 int	fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
255 void	fd_do_eject __P((struct fdc_softc *, int));
256 void	fd_mountroot_hook __P((struct device *));
257 static void fdconf __P((struct fdc_softc *));
258 
259 static int fdc_softpend = 0;
260 #ifndef	FDC_SOFTPRI
261 #define	FDC_SOFTPRI	2
262 #endif
263 #define FD_SET_SWINTR()	{ fdc_softpend = 1; isr_soft_request(FDC_SOFTPRI); }
264 
265 /*
266  * The Floppy Control Register on the sun3x, not to be confused with the
267  * Floppy ControllER Registers that this driver mostly insterfaces with,
268  * controls some of the auxillary functions of the floppy drive.  These
269  * include asserting the floppy eject and terminal data count (or TC) pins
270  * of the floppy drive and controller chip respectively.
271  *
272  * Often it is necessary to toggle individual bits within this register
273  * while keeping the others untouched.  However, the register does not
274  * present its latched data to the processor when read.  This prevents the
275  * use of a read-modify-write cycle that would normally be used to modify
276  * individual bits.  To get around this we must keep a copy of register's
277  * current value and always insure that when we wish to modify the register,
278  * we actually modify the copy and synchronize the register to it.
279  */
280 #define	FCR_REG_SYNC()	(*fdc->sc_reg_fcr = fdc->sc_fcr)
281 
282 int
283 fdcmatch(parent, match, aux)
284 	struct device *parent;
285 	struct cfdata *match;
286 	void *aux;
287 {
288 	struct confargs *ca = aux;
289 
290 	if (bus_peek(ca->ca_bustype, ca->ca_paddr, sizeof(u_char)) == -1)
291 		return (0);
292 
293 	return (1);
294 }
295 
296 /*
297  * Arguments passed between fdcattach and fdprobe.
298  */
299 struct fdc_attach_args {
300 	int fa_drive;
301 	struct bootpath *fa_bootpath;
302 	struct fd_type *fa_deftype;
303 };
304 
305 /*
306  * Print the location of a disk drive (called just before attaching the
307  * the drive).  If `fdc' is not NULL, the drive was found but was not
308  * in the system config file; print the drive name as well.
309  * Return QUIET (config_find ignores this if the device was configured) to
310  * avoid printing `fdN not configured' messages.
311  */
312 int
313 fdprint(aux, fdc)
314 	void *aux;
315 	const char *fdc;
316 {
317 	register struct fdc_attach_args *fa = aux;
318 
319 	if (!fdc)
320 		printf(" drive %d", fa->fa_drive);
321 	return (QUIET);
322 }
323 
324 static void
325 fdconf(fdc)
326 	struct fdc_softc *fdc;
327 {
328 	int	vroom;
329 
330 	if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
331 		return;
332 
333 	/*
334 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
335 	 * the PROM thinks is appropriate.
336 	 */
337 	if ((vroom = fdc->sc_status[7]) == 0)
338 		vroom = 0x64;
339 
340 	/* Configure controller to use FIFO and Implied Seek */
341 	out_fdc(fdc, NE7CMD_CFG);
342 	out_fdc(fdc, vroom);
343 	out_fdc(fdc, fdc->sc_cfg);
344 	out_fdc(fdc, 0); /* PRETRK */
345 	/* No result phase */
346 }
347 
348 void
349 fdcattach(parent, self, aux)
350 	struct device *parent, *self;
351 	void *aux;
352 {
353 	register struct confargs *ca = aux;
354 	struct fdc_softc *fdc = (void *)self;
355 	struct fdc_attach_args fa;
356 	int pri, vec;
357 	char code;
358 
359 	fdc->sc_reg = (caddr_t)bus_mapin(ca->ca_bustype, ca->ca_paddr,
360 		sizeof(union fdreg));
361 
362 	fdc->sc_state = DEVIDLE;
363 	fdc->sc_istate = ISTATE_IDLE;
364 	fdc->sc_flags |= FDC_EIS;
365 	TAILQ_INIT(&fdc->sc_drives);
366 
367 	/* Assume a 82072 */
368 	code = '2';
369 
370 	if (code == '7') {
371 		panic("no 82077 fdc in this kernel");
372 		/* NOTREACHED */
373 	} else {
374 		fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr;
375 		fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo;
376 
377 		fdc->sc_reg_fcr = ((volatile u_int8_t *) fdc->sc_reg)
378 			+ FDC_FCR_OFFSET;
379 		fdc->sc_reg_fvr = ((volatile u_int8_t *) fdc->sc_reg)
380 			+ FDC_FVR_OFFSET;
381 	}
382 
383 	isr_add_autovect(fdcswintr, fdc, FDC_SOFTPRI);
384 	pri = ca->ca_intpri;
385 	vec = ca->ca_intvec;
386 	if (vec == -1) {
387 		/* Tell the FDC to fake an autovector. */
388 		vec = 0x18 + pri; /* XXX */
389 		isr_add_autovect(fdchwintr, fdc, pri);
390 	} else {
391 		/* An OBIO bus with vectors?  Weird exception. */
392 		isr_add_vectored(fdchwintr, fdc, pri, vec);
393 	}
394 	*fdc->sc_reg_fvr = vec;	/* Program controller w/ interrupt vector */
395 
396 	printf(": (softpri %d) chip 8207%c\n", FDC_SOFTPRI, code);
397 
398 #ifdef FD_DEBUG
399 	if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
400 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
401 		if (fdc_debug)
402 			printf("[version cmd]");
403 	}
404 #endif
405 
406 	fdc_reset(fdc);
407 	/*
408 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
409 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
410 	 */
411 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
412 	fdconf(fdc);
413 
414 	evcnt_attach(&fdc->sc_dev, "intr", &fdc->sc_intrcnt);
415 
416 	/* physical limit: four drives per controller. */
417 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
418 		fa.fa_deftype = NULL;		/* unknown */
419 	fa.fa_deftype = &fd_types[0];		/* XXX */
420 		(void)config_found(self, (void *)&fa, fdprint);
421 	}
422 }
423 
424 int
425 fdmatch(parent, match, aux)
426 	struct device *parent;
427 	struct cfdata *match;
428 	void *aux;
429 {
430 	struct fdc_softc *fdc = (void *)parent;
431 	struct fdc_attach_args *fa = aux;
432 	int drive = fa->fa_drive;
433 	int n, ok;
434 
435 	if (drive > 0)
436 		/* XXX - for now, punt > 1 drives */
437 		return (0);
438 
439 	/* select drive and turn on motor */
440 	fdc->sc_fcr |= FCR_DSEL(drive) | FCR_MTRON;
441 	FCR_REG_SYNC();
442 	/* wait for motor to spin up */
443 	delay(250000);
444 
445 	fdc->sc_nstat = 0;
446 	out_fdc(fdc, NE7CMD_RECAL);
447 	out_fdc(fdc, drive);
448 	/* wait for recalibrate */
449 	for (n = 0; n < 10000; n++) {
450 		delay(1000);
451 		if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
452 			/* wait a bit longer till device *really* is ready */
453 			delay(100000);
454 			if (out_fdc(fdc, NE7CMD_SENSEI))
455 				break;
456 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
457 				/*
458 				 * Got `invalid command'; we interpret it
459 				 * to mean that the re-calibrate hasn't in
460 				 * fact finished yet
461 				 */
462 				continue;
463 			break;
464 		}
465 	}
466 	n = fdc->sc_nstat;
467 #ifdef FD_DEBUG
468 	if (fdc_debug) {
469 		int i;
470 		printf("fdprobe: %d stati:", n);
471 		for (i = 0; i < n; i++)
472 			printf(" %x", fdc->sc_status[i]);
473 		printf("\n");
474 	}
475 #endif
476 	ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
477 
478 	/* turn off motor */
479 	fdc->sc_fcr &= ~(FCR_DSEL(drive)|FCR_MTRON);
480 	FCR_REG_SYNC();
481 
482 	return (ok);
483 }
484 
485 /*
486  * Controller is working, and drive responded.  Attach it.
487  */
488 void
489 fdattach(parent, self, aux)
490 	struct device *parent, *self;
491 	void *aux;
492 {
493 	struct fdc_softc *fdc = (void *)parent;
494 	struct fd_softc *fd = (void *)self;
495 	struct fdc_attach_args *fa = aux;
496 	struct fd_type *type = fa->fa_deftype;
497 	int drive = fa->fa_drive;
498 
499 	/* XXX Allow `flags' to override device type? */
500 
501 	if (type)
502 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
503 		    type->tracks, type->heads, type->sectrac);
504 	else
505 		printf(": density unknown\n");
506 
507 	BUFQ_INIT(&fd->sc_q);
508 	fd->sc_cylin = -1;
509 	fd->sc_drive = drive;
510 	fd->sc_deftype = type;
511 	fdc->sc_fd[drive] = fd;
512 
513 	/*
514 	 * Initialize and attach the disk structure.
515 	 */
516 	fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
517 	fd->sc_dk.dk_driver = &fddkdriver;
518 	disk_attach(&fd->sc_dk);
519 
520 #ifdef	sparc
521 	/*
522 	 * We're told if we're the boot device in fdcattach().
523 	 */
524 	if (fa->fa_bootpath)
525 		fa->fa_bootpath->dev = &fd->sc_dv;
526 #endif
527 #define	OUT_FDC(sc, c)	{                                \
528 	if (out_fdc((sc), (c)))                          \
529 		printf("fdc: specify command failed.\n");\
530 	}
531 	/* specify command */
532 	OUT_FDC(fdc, NE7CMD_SPECIFY);
533 	OUT_FDC(fdc, type->steprate);
534 	/*
535 	 * The '|1' in the following statement turns on the 'Non-DMA' bit
536 	 * specifier in the last byte of the SPECIFY command as described in the
537 	 * datasheet I have.  This is necessary for the driver to work on the
538 	 * sun3x, because the system will not respond to the chip's requests
539 	 * for DMA; there is no hardware on the motherboard to support it.
540 	 * By enabling this bit, we will force the chip to interrupt when its
541 	 * FIFO is full, at which point the interrupt handler will empty it and
542 	 * continue.  This is ``pseudo-DMA''.
543 	 * -J
544 	 */
545 	OUT_FDC(fdc, 6|1);	/* XXX head load time == 6ms */
546 #undef	OUT_FDC
547 
548 	/*
549 	 * Establish a mountroot_hook anyway in case we booted
550 	 * with RB_ASKNAME and get selected as the boot device.
551 	 */
552 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
553 
554 	/* Make sure the drive motor gets turned off at shutdown time. */
555 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
556 
557 	/* XXX Need to do some more fiddling with sc_dk. */
558 	dk_establish(&fd->sc_dk, &fd->sc_dv);
559 }
560 
561 __inline struct fd_type *
562 fd_dev_to_type(fd, dev)
563 	struct fd_softc *fd;
564 	dev_t dev;
565 {
566 	int type = FDTYPE(dev);
567 
568 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
569 		return (NULL);
570 	return (type ? &fd_types[type - 1] : fd->sc_deftype);
571 }
572 
573 void
574 fdstrategy(bp)
575 	register struct buf *bp;	/* IO operation to perform */
576 {
577 	struct fd_softc *fd;
578 	int unit = FDUNIT(bp->b_dev);
579 	int sz;
580  	int s;
581 
582 	/* Valid unit, controller, and request? */
583 	if (unit >= fd_cd.cd_ndevs ||
584 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
585 	    bp->b_blkno < 0 ||
586 	    ((bp->b_bcount % FDC_BSIZE) != 0 &&
587 	     (bp->b_flags & B_FORMAT) == 0)) {
588 		bp->b_error = EINVAL;
589 		goto bad;
590 	}
591 
592 	/* If it's a null transfer, return immediately. */
593 	if (bp->b_bcount == 0)
594 		goto done;
595 
596 	sz = howmany(bp->b_bcount, FDC_BSIZE);
597 
598 	if (bp->b_blkno + sz > fd->sc_type->size) {
599 		sz = fd->sc_type->size - bp->b_blkno;
600 		if (sz == 0) {
601 			/* If exactly at end of disk, return EOF. */
602 			bp->b_resid = bp->b_bcount;
603 			goto done;
604 		}
605 		if (sz < 0) {
606 			/* If past end of disk, return EINVAL. */
607 			bp->b_error = EINVAL;
608 			goto bad;
609 		}
610 		/* Otherwise, truncate request. */
611 		bp->b_bcount = sz << DEV_BSHIFT;
612 	}
613 
614 	bp->b_rawblkno = bp->b_blkno;
615  	bp->b_cylinder = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
616 
617 #ifdef FD_DEBUG
618 	if (fdc_debug > 1)
619 	    printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n",
620 		    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder);
621 #endif
622 
623 	/* Queue transfer on drive, activate drive and controller if idle. */
624 	s = splbio();
625 	disksort_cylinder(&fd->sc_q, bp);
626 	untimeout(fd_motor_off, fd); /* a good idea */
627 	if (fd->sc_active == 0)
628 		fdstart(fd);
629 #ifdef DIAGNOSTIC
630 	else {
631 		struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
632 		if (fdc->sc_state == DEVIDLE) {
633 			printf("fdstrategy: controller inactive\n");
634 			fdcstart(fdc);
635 		}
636 	}
637 #endif
638 	splx(s);
639 	return;
640 
641 bad:
642 	bp->b_flags |= B_ERROR;
643 done:
644 	/* Toss transfer; we're done early. */
645 	biodone(bp);
646 }
647 
648 void
649 fdstart(fd)
650 	struct fd_softc *fd;
651 {
652 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
653 	int active = fdc->sc_drives.tqh_first != 0;
654 
655 	/* Link into controller queue. */
656 	fd->sc_active = 1;
657 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
658 
659 	/* If controller not already active, start it. */
660 	if (!active)
661 		fdcstart(fdc);
662 }
663 
664 void
665 fdfinish(fd, bp)
666 	struct fd_softc *fd;
667 	struct buf *bp;
668 {
669 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
670 
671 	/*
672 	 * Move this drive to the end of the queue to give others a `fair'
673 	 * chance.  We only force a switch if N operations are completed while
674 	 * another drive is waiting to be serviced, since there is a long motor
675 	 * startup delay whenever we switch.
676 	 */
677 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
678 		fd->sc_ops = 0;
679 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
680 		if (BUFQ_NEXT(bp) != NULL) {
681 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
682 		} else
683 			fd->sc_active = 0;
684 	}
685 	bp->b_resid = fd->sc_bcount;
686 	fd->sc_skip = 0;
687 	BUFQ_REMOVE(&fd->sc_q, bp);
688 
689 	biodone(bp);
690 	/* turn off motor 5s from now */
691 	timeout(fd_motor_off, fd, 5 * hz);
692 	fdc->sc_state = DEVIDLE;
693 }
694 
695 void
696 fdc_reset(fdc)
697 	struct fdc_softc *fdc;
698 {
699 	fdc->sc_fcr = 0;
700 	FCR_REG_SYNC();
701 
702 	*fdc->sc_reg_drs = DRS_RESET;
703 	delay(10);
704 	*fdc->sc_reg_drs = 0;
705 
706 #ifdef FD_DEBUG
707 	if (fdc_debug)
708 		printf("fdc reset\n");
709 #endif
710 }
711 
712 void
713 fd_set_motor(fdc)
714 	struct fdc_softc *fdc;
715 {
716 	struct fd_softc *fd;
717 	int n;
718 
719 	int on = 0;
720 
721 	for (n = 0; n < 4; n++)
722 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
723 			on = 1;
724 	if (on) {
725 		fdc->sc_fcr |= FCR_DSEL(0)|FCR_MTRON; /* XXX */
726 	} else {
727 		fdc->sc_fcr &= ~(FCR_DSEL(0)|FCR_MTRON); /* XXX */
728 	}
729 	FCR_REG_SYNC();
730 }
731 
732 void
733 fd_motor_off(arg)
734 	void *arg;
735 {
736 	struct fd_softc *fd = arg;
737 	int s;
738 
739 	s = splbio();
740 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
741 	fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
742 	splx(s);
743 }
744 
745 void
746 fd_motor_on(arg)
747 	void *arg;
748 {
749 	struct fd_softc *fd = arg;
750 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
751 	int s;
752 
753 	s = splbio();
754 	fd->sc_flags &= ~FD_MOTOR_WAIT;
755 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
756 		(void) fdcstate(fdc);
757 	splx(s);
758 }
759 
760 int
761 fdcresult(fdc)
762 	struct fdc_softc *fdc;
763 {
764 	u_char i;
765 	int j = 100000,
766 	    n = 0;
767 
768 	for (; j; j--) {
769 		i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
770 		if (i == NE7_RQM)
771 			return (fdc->sc_nstat = n);
772 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
773 			if (n >= sizeof(fdc->sc_status)) {
774 				log(LOG_ERR, "fdcresult: overrun\n");
775 				return (-1);
776 			}
777 			fdc->sc_status[n++] = *fdc->sc_reg_fifo;
778 		} else
779 			delay(10);
780 	}
781 	log(LOG_ERR, "fdcresult: timeout\n");
782 	return (fdc->sc_nstat = -1);
783 }
784 
785 int
786 out_fdc(fdc, x)
787 	struct fdc_softc *fdc;
788 	u_char x;
789 {
790 	int i = 100000;
791 
792 	while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0)
793 		delay(1);
794 	if (i <= 0)
795 		return (-1);
796 
797 	*fdc->sc_reg_fifo = x;
798 	return (0);
799 }
800 
801 int
802 fdopen(dev, flags, fmt, p)
803 	dev_t dev;
804 	int flags, fmt;
805 	struct proc *p;
806 {
807  	int unit, pmask;
808 	struct fd_softc *fd;
809 	struct fd_type *type;
810 
811 	unit = FDUNIT(dev);
812 	if (unit >= fd_cd.cd_ndevs)
813 		return (ENXIO);
814 	fd = fd_cd.cd_devs[unit];
815 	if (fd == 0)
816 		return (ENXIO);
817 	type = fd_dev_to_type(fd, dev);
818 	if (type == NULL)
819 		return (ENXIO);
820 
821 	if ((fd->sc_flags & FD_OPEN) != 0 &&
822 	    fd->sc_type != type)
823 		return (EBUSY);
824 
825 	fd->sc_type = type;
826 	fd->sc_cylin = -1;
827 	fd->sc_flags |= FD_OPEN;
828 
829 	/*
830 	 * Only update the disklabel if we're not open anywhere else.
831 	 */
832 	if (fd->sc_dk.dk_openmask == 0)
833 		fdgetdisklabel(dev);
834 
835 	pmask = (1 << DISKPART(dev));
836 
837 	switch (fmt) {
838 	case S_IFCHR:
839 		fd->sc_dk.dk_copenmask |= pmask;
840 		break;
841 
842 	case S_IFBLK:
843 		fd->sc_dk.dk_bopenmask |= pmask;
844 		break;
845 	}
846 	fd->sc_dk.dk_openmask =
847 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
848 
849 	return (0);
850 }
851 
852 int
853 fdclose(dev, flags, fmt, p)
854 	dev_t dev;
855 	int flags, fmt;
856 	struct proc *p;
857 {
858 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
859 	int pmask = (1 << DISKPART(dev));
860 
861 	fd->sc_flags &= ~FD_OPEN;
862 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
863 
864 	switch (fmt) {
865 	case S_IFCHR:
866 		fd->sc_dk.dk_copenmask &= ~pmask;
867 		break;
868 
869 	case S_IFBLK:
870 		fd->sc_dk.dk_bopenmask &= ~pmask;
871 		break;
872 	}
873 	fd->sc_dk.dk_openmask =
874 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
875 
876 	return (0);
877 }
878 
879 int
880 fdread(dev, uio, flag)
881         dev_t dev;
882         struct uio *uio;
883 	int flag;
884 {
885 
886         return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
887 }
888 
889 int
890 fdwrite(dev, uio, flag)
891         dev_t dev;
892         struct uio *uio;
893 	int flag;
894 {
895 
896         return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
897 }
898 
899 void
900 fdcstart(fdc)
901 	struct fdc_softc *fdc;
902 {
903 
904 #ifdef DIAGNOSTIC
905 	/* only got here if controller's drive queue was inactive; should
906 	   be in idle state */
907 	if (fdc->sc_state != DEVIDLE) {
908 		printf("fdcstart: not idle\n");
909 		return;
910 	}
911 #endif
912 	(void) fdcstate(fdc);
913 }
914 
915 void
916 fdcstatus(dv, n, s)
917 	struct device *dv;
918 	int n;
919 	char *s;
920 {
921 	struct fdc_softc *fdc = (void *)dv->dv_parent;
922 	char bits[64];
923 #if 0
924 	/*
925 	 * A 82072 seems to return <invalid command> on
926 	 * gratuitous Sense Interrupt commands.
927 	 */
928 	if (n == 0 && (fdc->sc_flags & FDC_82077)) {
929 		out_fdc(fdc, NE7CMD_SENSEI);
930 		(void) fdcresult(fdc);
931 		n = 2;
932 	}
933 #endif
934 
935 	/* Just print last status */
936 	n = fdc->sc_nstat;
937 
938 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
939 
940 	switch (n) {
941 	case 0:
942 		printf("\n");
943 		break;
944 	case 2:
945 		printf(" (st0 %s cyl %d)\n",
946 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
947 		    bits, sizeof(bits)), fdc->sc_status[1]);
948 		break;
949 	case 7:
950 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
951 		    NE7_ST0BITS, bits, sizeof(bits)));
952 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
953 		    NE7_ST1BITS, bits, sizeof(bits)));
954 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
955 		    NE7_ST2BITS, bits, sizeof(bits)));
956 		printf(" cyl %d head %d sec %d)\n",
957 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
958 		break;
959 #ifdef DIAGNOSTIC
960 	default:
961 		printf(" fdcstatus: weird size: %d\n", n);
962 		break;
963 #endif
964 	}
965 }
966 
967 void
968 fdctimeout(arg)
969 	void *arg;
970 {
971 	struct fdc_softc *fdc = arg;
972 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
973 	int s;
974 
975 	s = splbio();
976 	fdcstatus(&fd->sc_dv, 0, "timeout");
977 
978 	if (BUFQ_FIRST(&fd->sc_q) != NULL)
979 		fdc->sc_state++;
980 	else
981 		fdc->sc_state = DEVIDLE;
982 
983 	(void) fdcstate(fdc);
984 	splx(s);
985 }
986 
987 void
988 fdcpseudointr(arg)
989 	void *arg;
990 {
991 	struct fdc_softc *fdc = arg;
992 	int s;
993 
994 	/* Just ensure it has the right spl. */
995 	s = splbio();
996 	(void) fdcstate(fdc);
997 	splx(s);
998 }
999 
1000 
1001 /*
1002  * hardware interrupt entry point: must be converted to `fast'
1003  * (in-window) handler.
1004  */
1005 int
1006 fdchwintr(arg)
1007 	void *arg;
1008 {
1009 	struct fdc_softc *fdc = arg;
1010 
1011 	/*
1012 	 * This code was reverse engineered from the SPARC bsd_fdintr.s.
1013 	 */
1014 	switch (fdc->sc_istate) {
1015 	case ISTATE_IDLE:
1016 		return (0);
1017 	case ISTATE_SENSEI:
1018 		out_fdc(fdc, NE7CMD_SENSEI);
1019 		fdcresult(fdc);
1020 		fdc->sc_istate = ISTATE_DONE;
1021 		FD_SET_SWINTR();
1022 		return (1);
1023 	case ISTATE_DMA:
1024 		break;
1025 	default:
1026 		log(LOG_ERR, "fdc: stray hard interrupt.\n");
1027 		fdc->sc_fcr &= ~(FCR_DSEL(0));	/* Does this help? */
1028 		fdc->sc_istate = ISTATE_SPURIOUS;
1029 		FD_SET_SWINTR();
1030 		return (1);
1031 	}
1032 
1033 	for (;;) {
1034 		register int msr;
1035 
1036 		msr = *fdc->sc_reg_msr;
1037 
1038 		if ((msr & NE7_RQM) == 0)
1039 			break;
1040 
1041 		if ((msr & NE7_NDM) == 0) {
1042 			fdcresult(fdc);
1043 			fdc->sc_istate = ISTATE_DONE;
1044 			FD_SET_SWINTR();
1045 			log(LOG_ERR, "fdc: overrun: tc = %d\n", fdc->sc_tc);
1046 			break;
1047 		}
1048 
1049 		if (msr & NE7_DIO) {
1050 			*fdc->sc_data++ = *fdc->sc_reg_fifo;
1051 		} else {
1052 			*fdc->sc_reg_fifo = *fdc->sc_data++;
1053 		}
1054 		if (--fdc->sc_tc == 0) {
1055 			fdc->sc_fcr |= FCR_TC;
1056 			FCR_REG_SYNC();
1057 			fdc->sc_istate = ISTATE_DONE;
1058 			delay(10);
1059 			fdc->sc_fcr &= ~FCR_TC;
1060 			FCR_REG_SYNC();
1061 			fdcresult(fdc);
1062 			FD_SET_SWINTR();
1063 			break;
1064 		}
1065 	}
1066 	return (1);
1067 }
1068 
1069 int
1070 fdcswintr(arg)
1071 	void *arg;
1072 {
1073 	struct fdc_softc *fdc = arg;
1074 	int s;
1075 
1076 	if (fdc_softpend == 0)
1077 		return (0);
1078 
1079 	isr_soft_clear(FDC_SOFTPRI);
1080 	fdc_softpend = 0;
1081 
1082 	if (fdc->sc_istate != ISTATE_DONE)
1083 		return (0);
1084 
1085 	fdc->sc_istate = ISTATE_IDLE;
1086 	s = splbio();
1087 	fdcstate(fdc);
1088 	splx(s);
1089 	return (1);
1090 }
1091 
1092 int
1093 fdcstate(fdc)
1094 	struct fdc_softc *fdc;
1095 {
1096 #define	st0	fdc->sc_status[0]
1097 #define	st1	fdc->sc_status[1]
1098 #define	cyl	fdc->sc_status[1]
1099 #define OUT_FDC(fdc, c, s) \
1100     do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
1101 
1102 	struct fd_softc *fd;
1103 	struct buf *bp;
1104 	int read, head, sec, nblks;
1105 	struct fd_type *type;
1106 	struct ne7_fd_formb *finfo = NULL;
1107 
1108 
1109 	if (fdc->sc_istate != ISTATE_IDLE) {
1110 		/* Trouble... */
1111 		printf("fdc: spurious interrupt: state %d, istate=%d\n",
1112 			fdc->sc_state, fdc->sc_istate);
1113 		fdc->sc_istate = ISTATE_IDLE;
1114 		if (fdc->sc_state == RESETCOMPLETE ||
1115 		    fdc->sc_state == RESETTIMEDOUT) {
1116 			panic("fdcintr: spurious interrupt can't be cleared");
1117 		}
1118 		goto doreset;
1119 	}
1120 
1121 loop:
1122 	/* Is there a drive for the controller to do a transfer with? */
1123 	fd = fdc->sc_drives.tqh_first;
1124 	if (fd == NULL) {
1125 		fdc->sc_state = DEVIDLE;
1126  		return (0);
1127 	}
1128 
1129 	/* Is there a transfer to this drive?  If not, deactivate drive. */
1130 	bp = BUFQ_FIRST(&fd->sc_q);
1131 	if (bp == NULL) {
1132 		fd->sc_ops = 0;
1133 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1134 		fd->sc_active = 0;
1135 		goto loop;
1136 	}
1137 
1138 	if (bp->b_flags & B_FORMAT)
1139 		finfo = (struct ne7_fd_formb *)bp->b_data;
1140 
1141 	switch (fdc->sc_state) {
1142 	case DEVIDLE:
1143 		fdc->sc_errors = 0;
1144 		fd->sc_skip = 0;
1145 		fd->sc_bcount = bp->b_bcount;
1146 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1147 		untimeout(fd_motor_off, fd);
1148 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1149 			fdc->sc_state = MOTORWAIT;
1150 			return (1);
1151 		}
1152 		if ((fd->sc_flags & FD_MOTOR) == 0) {
1153 			/* Turn on the motor, being careful about pairing. */
1154 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1155 			if (ofd && ofd->sc_flags & FD_MOTOR) {
1156 				untimeout(fd_motor_off, ofd);
1157 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1158 			}
1159 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1160 			fd_set_motor(fdc);
1161 			fdc->sc_state = MOTORWAIT;
1162 			if (fdc->sc_flags & FDC_82077) { /* XXX */
1163 				/* Allow .25s for motor to stabilize. */
1164 				timeout(fd_motor_on, fd, hz / 4);
1165 			} else {
1166 				fd->sc_flags &= ~FD_MOTOR_WAIT;
1167 				goto loop;
1168 			}
1169 			return (1);
1170 		}
1171 		/* Make sure the right drive is selected. */
1172 		fd_set_motor(fdc);
1173 
1174 		/*FALLTHROUGH*/
1175 	case DOSEEK:
1176 	doseek:
1177 		if ((fdc->sc_flags & FDC_EIS) &&
1178 		    (bp->b_flags & B_FORMAT) == 0) {
1179 			fd->sc_cylin = bp->b_cylinder;
1180 			/* We use implied seek */
1181 			goto doio;
1182 		}
1183 
1184 		if (fd->sc_cylin == bp->b_cylinder)
1185 			goto doio;
1186 
1187 		/* specify command */
1188 		OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
1189 		OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
1190 		OUT_FDC(fdc, 6|1, SEEKTIMEDOUT);	/* XXX head load time == 6ms */
1191 
1192 		fdc->sc_istate = ISTATE_SENSEI;
1193 		/* seek function */
1194 		OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
1195 		OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
1196 		OUT_FDC(fdc, bp->b_cylinder * fd->sc_type->step, SEEKTIMEDOUT);
1197 
1198 		fd->sc_cylin = -1;
1199 		fdc->sc_state = SEEKWAIT;
1200 		fdc->sc_nstat = 0;
1201 
1202 		fd->sc_dk.dk_seek++;
1203 		disk_busy(&fd->sc_dk);
1204 
1205 		timeout(fdctimeout, fdc, 4 * hz);
1206 		return (1);
1207 
1208 	case DOIO:
1209 	doio:
1210 #ifdef	NOTYET
1211 		/* Check to see if the disk has changed */
1212 		if (fdc->sc_reg_dir & FDI_DCHG) {
1213 			/*
1214 			 * The disk in the drive has changed since
1215 			 * the last transfer.  We need to see if its geometry
1216 			 * has changed.
1217 			 */
1218 		}
1219 #endif	/* NOTYET */
1220 
1221 		if (finfo)
1222 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1223 				      (char *)finfo;
1224 		type = fd->sc_type;
1225 		sec = fd->sc_blkno % type->seccyl;
1226 		nblks = type->seccyl - sec;
1227 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1228 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
1229 		fd->sc_nblks = nblks;
1230 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
1231 		head = sec / type->sectrac;
1232 		sec -= head * type->sectrac;
1233 #ifdef DIAGNOSTIC
1234 		{int block;
1235 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1236 		 if (block != fd->sc_blkno) {
1237 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1238 #ifdef DDB
1239 			 Debugger();
1240 #endif
1241 		 }}
1242 #endif
1243 		read = bp->b_flags & B_READ;
1244 
1245 		/* Setup for pseudo DMA */
1246 		fdc->sc_data = bp->b_data + fd->sc_skip;
1247 		fdc->sc_tc = fd->sc_nbytes;
1248 
1249 		*fdc->sc_reg_drs = type->rate;
1250 #ifdef FD_DEBUG
1251 		if (fdc_debug > 1)
1252 			printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1253 				read ? "read" : "write", fd->sc_drive,
1254 				fd->sc_cylin, head, sec, nblks);
1255 #endif
1256 		fdc->sc_state = IOCOMPLETE;
1257 		fdc->sc_istate = ISTATE_DMA;
1258 		fdc->sc_nstat = 0;
1259 		if (finfo) {
1260 			/* formatting */
1261 			OUT_FDC(fdc, NE7CMD_FORMAT, IOTIMEDOUT);
1262 			OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
1263 			OUT_FDC(fdc, finfo->fd_formb_secshift, IOTIMEDOUT);
1264 			OUT_FDC(fdc, finfo->fd_formb_nsecs, IOTIMEDOUT);
1265 			OUT_FDC(fdc, finfo->fd_formb_gaplen, IOTIMEDOUT);
1266 			OUT_FDC(fdc, finfo->fd_formb_fillbyte, IOTIMEDOUT);
1267 		} else {
1268 			if (read)
1269 				OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT);
1270 			else
1271 				OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT);
1272 			OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
1273 			OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT);	/*track*/
1274 			OUT_FDC(fdc, head, IOTIMEDOUT);
1275 			OUT_FDC(fdc, sec + 1, IOTIMEDOUT);	/*sector+1*/
1276 			OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/*sector size*/
1277 			OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/*secs/track*/
1278 			OUT_FDC(fdc, type->gap1, IOTIMEDOUT);	/*gap1 size*/
1279 			OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/*data length*/
1280 		}
1281 
1282 		disk_busy(&fd->sc_dk);
1283 
1284 		/* allow 2 seconds for operation */
1285 		timeout(fdctimeout, fdc, 2 * hz);
1286 		return (1);				/* will return later */
1287 
1288 	case SEEKWAIT:
1289 		untimeout(fdctimeout, fdc);
1290 		fdc->sc_state = SEEKCOMPLETE;
1291 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1292 			/* allow 1/50 second for heads to settle */
1293 			timeout(fdcpseudointr, fdc, hz / 50);
1294 			return (1);		/* will return later */
1295 		}
1296 		/*FALLTHROUGH*/
1297 	case SEEKCOMPLETE:
1298 		disk_unbusy(&fd->sc_dk, 0);	/* no data on seek */
1299 
1300 		/* Make sure seek really happened. */
1301 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1302 		    cyl != bp->b_cylinder * fd->sc_type->step) {
1303 #ifdef FD_DEBUG
1304 			if (fdc_debug)
1305 				fdcstatus(&fd->sc_dv, 2, "seek failed");
1306 #endif
1307 			fdcretry(fdc);
1308 			goto loop;
1309 		}
1310 		fd->sc_cylin = bp->b_cylinder;
1311 		goto doio;
1312 
1313 	case IOTIMEDOUT:
1314 		fdc->sc_fcr |= FCR_TC;
1315 		FCR_REG_SYNC();
1316 		delay(10);
1317 		fdc->sc_fcr &= ~FCR_TC;
1318 		FCR_REG_SYNC();
1319 		(void)fdcresult(fdc);
1320 		/*FALLTHROUGH*/
1321 	case SEEKTIMEDOUT:
1322 	case RECALTIMEDOUT:
1323 	case RESETTIMEDOUT:
1324 		fdcretry(fdc);
1325 		goto loop;
1326 
1327 	case IOCOMPLETE: /* IO DONE, post-analyze */
1328 		untimeout(fdctimeout, fdc);
1329 
1330 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1331 
1332 		if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
1333 #ifdef FD_DEBUG
1334 			if (fdc_debug) {
1335 				fdcstatus(&fd->sc_dv, 7,
1336 					bp->b_flags & B_READ
1337 					? "read failed" : "write failed");
1338 				printf("blkno %d nblks %d tc %d\n",
1339 				       fd->sc_blkno, fd->sc_nblks, fdc->sc_tc);
1340 			}
1341 #endif
1342 			if (fdc->sc_nstat == 7 &&
1343 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1344 
1345 				/*
1346 				 * Silently retry overruns if no other
1347 				 * error bit is set. Adjust threshold.
1348 				 */
1349 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1350 				if (thr < 15) {
1351 					thr++;
1352 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1353 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1354 #ifdef FD_DEBUG
1355 					if (fdc_debug)
1356 						printf("fdc: %d -> threshold\n", thr);
1357 #endif
1358 					fdconf(fdc);
1359 					fdc->sc_overruns = 0;
1360 				}
1361 				if (++fdc->sc_overruns < 3) {
1362 					fdc->sc_state = DOIO;
1363 					goto loop;
1364 				}
1365 			}
1366 			fdcretry(fdc);
1367 			goto loop;
1368 		}
1369 		if (fdc->sc_errors) {
1370 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
1371 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1372 			printf("\n");
1373 			fdc->sc_errors = 0;
1374 		} else {
1375 			if (--fdc->sc_overruns < -20) {
1376 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1377 				if (thr > 0) {
1378 					thr--;
1379 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1380 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1381 #ifdef FD_DEBUG
1382 					if (fdc_debug)
1383 						printf("fdc: %d -> threshold\n", thr);
1384 #endif
1385 					fdconf(fdc);
1386 				}
1387 				fdc->sc_overruns = 0;
1388 			}
1389 		}
1390 		fd->sc_blkno += fd->sc_nblks;
1391 		fd->sc_skip += fd->sc_nbytes;
1392 		fd->sc_bcount -= fd->sc_nbytes;
1393 		if (!finfo && fd->sc_bcount > 0) {
1394 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1395 			goto doseek;
1396 		}
1397 		fdfinish(fd, bp);
1398 		goto loop;
1399 
1400 	case DORESET:
1401 	doreset:
1402 		/* try a reset, keep motor on */
1403 		fd_set_motor(fdc);
1404 		delay(100);
1405 		fdc_reset(fdc);
1406 		fdc->sc_nstat = 0;
1407 		fdc->sc_istate = ISTATE_SENSEI;
1408 		fdc->sc_state = RESETCOMPLETE;
1409 		timeout(fdctimeout, fdc, hz / 2);
1410 		return (1);			/* will return later */
1411 
1412 	case RESETCOMPLETE:
1413 		untimeout(fdctimeout, fdc);
1414 		fdconf(fdc);
1415 
1416 		/* fall through */
1417 	case DORECAL:
1418 		fdc->sc_state = RECALWAIT;
1419 		fdc->sc_istate = ISTATE_SENSEI;
1420 		fdc->sc_nstat = 0;
1421 		/* recalibrate function */
1422 		OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
1423 		OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
1424 		timeout(fdctimeout, fdc, 5 * hz);
1425 		return (1);			/* will return later */
1426 
1427 	case RECALWAIT:
1428 		untimeout(fdctimeout, fdc);
1429 		fdc->sc_state = RECALCOMPLETE;
1430 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1431 			/* allow 1/30 second for heads to settle */
1432 			timeout(fdcpseudointr, fdc, hz / 30);
1433 			return (1);		/* will return later */
1434 		}
1435 
1436 	case RECALCOMPLETE:
1437 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1438 #ifdef FD_DEBUG
1439 			if (fdc_debug)
1440 				fdcstatus(&fd->sc_dv, 2, "recalibrate failed");
1441 #endif
1442 			fdcretry(fdc);
1443 			goto loop;
1444 		}
1445 		fd->sc_cylin = 0;
1446 		goto doseek;
1447 
1448 	case MOTORWAIT:
1449 		if (fd->sc_flags & FD_MOTOR_WAIT)
1450 			return (1);		/* time's not up yet */
1451 		goto doseek;
1452 
1453 	default:
1454 		fdcstatus(&fd->sc_dv, 0, "stray interrupt");
1455 		return (1);
1456 	}
1457 #ifdef DIAGNOSTIC
1458 	panic("fdcintr: impossible");
1459 #endif
1460 #undef	st0
1461 #undef	st1
1462 #undef	cyl
1463 }
1464 
1465 void
1466 fdcretry(fdc)
1467 	struct fdc_softc *fdc;
1468 {
1469 	char bits[64];
1470 	struct fd_softc *fd;
1471 	struct buf *bp;
1472 
1473 	fd = fdc->sc_drives.tqh_first;
1474 	bp = BUFQ_FIRST(&fd->sc_q);
1475 
1476 	fdc->sc_overruns = 0;
1477 	if (fd->sc_opts & FDOPT_NORETRY)
1478 		goto fail;
1479 
1480 	switch (fdc->sc_errors) {
1481 	case 0:
1482 		/* try again */
1483 		fdc->sc_state =
1484 			(fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
1485 		break;
1486 
1487 	case 1: case 2: case 3:
1488 		/* didn't work; try recalibrating */
1489 		fdc->sc_state = DORECAL;
1490 		break;
1491 
1492 	case 4:
1493 		/* still no go; reset the bastard */
1494 		fdc->sc_state = DORESET;
1495 		break;
1496 
1497 	default:
1498 	fail:
1499 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1500 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
1501 				fd->sc_skip / FDC_BSIZE,
1502 				(struct disklabel *)NULL);
1503 
1504 			printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1505 				NE7_ST0BITS, bits, sizeof(bits)));
1506 			printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1507 				NE7_ST1BITS, bits, sizeof(bits)));
1508 			printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1509 				NE7_ST2BITS, bits, sizeof(bits)));
1510 			printf(" cyl %d head %d sec %d)\n",
1511 				fdc->sc_status[3], fdc->sc_status[4],
1512 				fdc->sc_status[5]);
1513 		}
1514 
1515 		bp->b_flags |= B_ERROR;
1516 		bp->b_error = EIO;
1517 		fdfinish(fd, bp);
1518 	}
1519 	fdc->sc_errors++;
1520 }
1521 
1522 int
1523 fdsize(dev)
1524 	dev_t dev;
1525 {
1526 
1527 	/* Swapping to floppies would not make sense. */
1528 	return (-1);
1529 }
1530 
1531 int
1532 fddump(dev, blkno, va, size)
1533 	dev_t dev;
1534 	daddr_t blkno;
1535 	caddr_t va;
1536 	size_t size;
1537 {
1538 
1539 	/* Not implemented. */
1540 	return (EINVAL);
1541 }
1542 
1543 int
1544 fdioctl(dev, cmd, addr, flag, p)
1545 	dev_t dev;
1546 	u_long cmd;
1547 	caddr_t addr;
1548 	int flag;
1549 	struct proc *p;
1550 {
1551 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1552 	struct fdformat_parms *form_parms;
1553 	struct fdformat_cmd *form_cmd;
1554 	struct ne7_fd_formb fd_formb;
1555 	int il[FD_MAX_NSEC + 1];
1556 	int i, j;
1557 	int error;
1558 
1559 	switch (cmd) {
1560 	case DIOCGDINFO:
1561 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1562 		return 0;
1563 
1564 	case DIOCWLABEL:
1565 		if ((flag & FWRITE) == 0)
1566 			return EBADF;
1567 		/* XXX do something */
1568 		return (0);
1569 
1570 	case DIOCWDINFO:
1571 		if ((flag & FWRITE) == 0)
1572 			return (EBADF);
1573 
1574 		error = setdisklabel(fd->sc_dk.dk_label,
1575 				    (struct disklabel *)addr, 0,
1576 				    fd->sc_dk.dk_cpulabel);
1577 		if (error)
1578 			return (error);
1579 
1580 		error = writedisklabel(dev, fdstrategy,
1581 				       fd->sc_dk.dk_label,
1582 				       fd->sc_dk.dk_cpulabel);
1583 		return (error);
1584 
1585 	case DIOCLOCK:
1586 		/*
1587 		 * Nothing to do here, really.
1588 		 */
1589 		return (0);
1590 
1591 	case DIOCEJECT:
1592 		if (*(int *)addr == 0) {
1593 			int part = DISKPART(dev);
1594 			/*
1595 			 * Don't force eject: check that we are the only
1596 			 * partition open. If so, unlock it.
1597 			 */
1598 			if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
1599 			    fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
1600 			    fd->sc_dk.dk_openmask) {
1601 				return (EBUSY);
1602 			}
1603 		}
1604 		/* FALLTHROUGH */
1605 	case ODIOCEJECT:
1606 		fd_do_eject((void *)fd->sc_dv.dv_parent, fd->sc_drive);
1607 		return (0);
1608 
1609 	case FDIOCGETFORMAT:
1610 		form_parms = (struct fdformat_parms *)addr;
1611 		form_parms->fdformat_version = FDFORMAT_VERSION;
1612 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1613 		form_parms->ncyl = fd->sc_type->tracks;
1614 		form_parms->nspt = fd->sc_type->sectrac;
1615 		form_parms->ntrk = fd->sc_type->heads;
1616 		form_parms->stepspercyl = fd->sc_type->step;
1617 		form_parms->gaplen = fd->sc_type->gap2;
1618 		form_parms->fillbyte = fd->sc_type->fillbyte;
1619 		form_parms->interleave = fd->sc_type->interleave;
1620 		switch (fd->sc_type->rate) {
1621 		case FDC_500KBPS:
1622 			form_parms->xfer_rate = 500 * 1024;
1623 			break;
1624 		case FDC_300KBPS:
1625 			form_parms->xfer_rate = 300 * 1024;
1626 			break;
1627 		case FDC_250KBPS:
1628 			form_parms->xfer_rate = 250 * 1024;
1629 			break;
1630 		default:
1631 			return (EINVAL);
1632 		}
1633 		return (0);
1634 
1635 	case FDIOCSETFORMAT:
1636 		if ((flag & FWRITE) == 0)
1637 			return (EBADF);	/* must be opened for writing */
1638 
1639 		form_parms = (struct fdformat_parms *)addr;
1640 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
1641 			return (EINVAL);/* wrong version of formatting prog */
1642 
1643 		i = form_parms->nbps >> 7;
1644 		if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
1645 		    i & ~(1 << (ffs(i)-1)))
1646 			/* not a power-of-two multiple of 128 */
1647 			return (EINVAL);
1648 
1649 		switch (form_parms->xfer_rate) {
1650 		case 500 * 1024:
1651 			fd->sc_type->rate = FDC_500KBPS;
1652 			break;
1653 		case 300 * 1024:
1654 			fd->sc_type->rate = FDC_300KBPS;
1655 			break;
1656 		case 250 * 1024:
1657 			fd->sc_type->rate = FDC_250KBPS;
1658 			break;
1659 		default:
1660 			return (EINVAL);
1661 		}
1662 
1663 		if (form_parms->nspt > FD_MAX_NSEC ||
1664 		    form_parms->fillbyte > 0xff ||
1665 		    form_parms->interleave > 0xff)
1666 			return EINVAL;
1667 		fd->sc_type->sectrac = form_parms->nspt;
1668 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
1669 			return EINVAL;
1670 		fd->sc_type->heads = form_parms->ntrk;
1671 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1672 		fd->sc_type->secsize = ffs(i)-1;
1673 		fd->sc_type->gap2 = form_parms->gaplen;
1674 		fd->sc_type->tracks = form_parms->ncyl;
1675 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1676 			form_parms->nbps / DEV_BSIZE;
1677 		fd->sc_type->step = form_parms->stepspercyl;
1678 		fd->sc_type->fillbyte = form_parms->fillbyte;
1679 		fd->sc_type->interleave = form_parms->interleave;
1680 		return (0);
1681 
1682 	case FDIOCFORMAT_TRACK:
1683 		if((flag & FWRITE) == 0)
1684 			/* must be opened for writing */
1685 			return (EBADF);
1686 		form_cmd = (struct fdformat_cmd *)addr;
1687 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
1688 			/* wrong version of formatting prog */
1689 			return (EINVAL);
1690 
1691 		if (form_cmd->head >= fd->sc_type->heads ||
1692 		    form_cmd->cylinder >= fd->sc_type->tracks) {
1693 			return (EINVAL);
1694 		}
1695 
1696 		fd_formb.head = form_cmd->head;
1697 		fd_formb.cyl = form_cmd->cylinder;
1698 		fd_formb.transfer_rate = fd->sc_type->rate;
1699 		fd_formb.fd_formb_secshift = fd->sc_type->secsize;
1700 		fd_formb.fd_formb_nsecs = fd->sc_type->sectrac;
1701 		fd_formb.fd_formb_gaplen = fd->sc_type->gap2;
1702 		fd_formb.fd_formb_fillbyte = fd->sc_type->fillbyte;
1703 
1704 		bzero(il, sizeof(il));
1705 		for (j = 0, i = 1; i <= fd_formb.fd_formb_nsecs; i++) {
1706 			while (il[(j%fd_formb.fd_formb_nsecs) + 1])
1707 				j++;
1708 			il[(j%fd_formb.fd_formb_nsecs) + 1] = i;
1709 			j += fd->sc_type->interleave;
1710 		}
1711 		for (i = 0; i < fd_formb.fd_formb_nsecs; i++) {
1712 			fd_formb.fd_formb_cylno(i) = form_cmd->cylinder;
1713 			fd_formb.fd_formb_headno(i) = form_cmd->head;
1714 			fd_formb.fd_formb_secno(i) = il[i+1];
1715 			fd_formb.fd_formb_secsize(i) = fd->sc_type->secsize;
1716 		}
1717 
1718 		return fdformat(dev, &fd_formb, p);
1719 
1720 	case FDIOCGETOPTS:		/* get drive options */
1721 		*(int *)addr = fd->sc_opts;
1722 		return (0);
1723 
1724 	case FDIOCSETOPTS:		/* set drive options */
1725 		fd->sc_opts = *(int *)addr;
1726 		return (0);
1727 
1728 #ifdef DEBUG
1729 	case _IO('f', 100):
1730 		{
1731 		int i;
1732 		struct fdc_softc *fdc = (struct fdc_softc *)
1733 					fd->sc_dv.dv_parent;
1734 
1735 		out_fdc(fdc, NE7CMD_DUMPREG);
1736 		fdcresult(fdc);
1737 		printf("dumpreg(%d regs): <", fdc->sc_nstat);
1738 		for (i = 0; i < fdc->sc_nstat; i++)
1739 			printf(" %x", fdc->sc_status[i]);
1740 		printf(">\n");
1741 		}
1742 
1743 		return (0);
1744 	case _IOW('f', 101, int):
1745 		((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg &=
1746 			~CFG_THRHLD_MASK;
1747 		((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg |=
1748 			(*(int *)addr & CFG_THRHLD_MASK);
1749 		fdconf((struct fdc_softc *) fd->sc_dv.dv_parent);
1750 		return (0);
1751 	case _IO('f', 102):
1752 		{
1753 		int i;
1754 		struct fdc_softc *fdc = (struct fdc_softc *)
1755 					fd->sc_dv.dv_parent;
1756 		out_fdc(fdc, NE7CMD_SENSEI);
1757 		fdcresult(fdc);
1758 		printf("sensei(%d regs): <", fdc->sc_nstat);
1759 		for (i=0; i< fdc->sc_nstat; i++)
1760 			printf(" 0x%x", fdc->sc_status[i]);
1761 		}
1762 		printf(">\n");
1763 		return (0);
1764 #endif
1765 	default:
1766 		return (ENOTTY);
1767 	}
1768 
1769 #ifdef DIAGNOSTIC
1770 	panic("fdioctl: impossible");
1771 #endif
1772 }
1773 
1774 int
1775 fdformat(dev, finfo, p)
1776 	dev_t dev;
1777 	struct ne7_fd_formb *finfo;
1778 	struct proc *p;
1779 {
1780 	int rv = 0, s;
1781 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1782 	struct fd_type *type = fd->sc_type;
1783 	struct buf *bp;
1784 
1785 	/* set up a buffer header for fdstrategy() */
1786 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1787 	if (bp == 0)
1788 		return (ENOBUFS);
1789 
1790 	PHOLD(p);
1791 	bzero((void *)bp, sizeof(struct buf));
1792 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1793 	bp->b_proc = p;
1794 	bp->b_dev = dev;
1795 
1796 	/*
1797 	 * Calculate a fake blkno, so fdstrategy() would initiate a
1798 	 * seek to the requested cylinder.
1799 	 */
1800 	bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1801 		       + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
1802 
1803 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1804 	bp->b_data = (caddr_t)finfo;
1805 
1806 #ifdef FD_DEBUG
1807 	if (fdc_debug)
1808 		printf("fdformat: blkno %x count %ld\n",
1809 			bp->b_blkno, bp->b_bcount);
1810 #endif
1811 
1812 	/* now do the format */
1813 	fdstrategy(bp);
1814 
1815 	/* ...and wait for it to complete */
1816 	s = splbio();
1817 	while (!(bp->b_flags & B_DONE)) {
1818 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1819 		if (rv == EWOULDBLOCK)
1820 			break;
1821 	}
1822 	splx(s);
1823 
1824 	if (rv == EWOULDBLOCK) {
1825 		/* timed out */
1826 		rv = EIO;
1827 		biodone(bp);
1828 	}
1829 	if (bp->b_flags & B_ERROR) {
1830 		rv = bp->b_error;
1831 	}
1832 	PRELE(p);
1833 	free(bp, M_TEMP);
1834 	return (rv);
1835 }
1836 
1837 void
1838 fdgetdisklabel(dev)
1839 	dev_t dev;
1840 {
1841 	int unit = FDUNIT(dev), i;
1842 	struct fd_softc *fd = fd_cd.cd_devs[unit];
1843 	struct disklabel *lp = fd->sc_dk.dk_label;
1844 	struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
1845 
1846 	bzero(lp, sizeof(struct disklabel));
1847 	bzero(lp, sizeof(struct cpu_disklabel));
1848 
1849 	lp->d_type = DTYPE_FLOPPY;
1850 	lp->d_secsize = FDC_BSIZE;
1851 	lp->d_secpercyl = fd->sc_type->seccyl;
1852 	lp->d_nsectors = fd->sc_type->sectrac;
1853 	lp->d_ncylinders = fd->sc_type->tracks;
1854 	lp->d_ntracks = fd->sc_type->heads;	/* Go figure... */
1855 	lp->d_rpm = 3600;	/* XXX like it matters... */
1856 
1857 	strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
1858 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1859 	lp->d_interleave = 1;
1860 
1861 	lp->d_partitions[RAW_PART].p_offset = 0;
1862 	lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
1863 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1864 	lp->d_npartitions = RAW_PART + 1;
1865 
1866 	lp->d_magic = DISKMAGIC;
1867 	lp->d_magic2 = DISKMAGIC;
1868 	lp->d_checksum = dkcksum(lp);
1869 
1870 	/*
1871 	 * Call the generic disklabel extraction routine.  If there's
1872 	 * not a label there, fake it.
1873 	 */
1874 	if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
1875 		strncpy(lp->d_packname, "default label",
1876 		    sizeof(lp->d_packname));
1877 		/*
1878 		 * Reset the partition info; it might have gotten
1879 		 * trashed in readdisklabel().
1880 		 *
1881 		 * XXX Why do we have to do this?  readdisklabel()
1882 		 * should be safe...
1883 		 */
1884 		for (i = 0; i < MAXPARTITIONS; ++i) {
1885 			lp->d_partitions[i].p_offset = 0;
1886 			if (i == RAW_PART) {
1887 				lp->d_partitions[i].p_size =
1888 				    lp->d_secpercyl * lp->d_ncylinders;
1889 				lp->d_partitions[i].p_fstype = FS_BSDFFS;
1890 			} else {
1891 				lp->d_partitions[i].p_size = 0;
1892 				lp->d_partitions[i].p_fstype = FS_UNUSED;
1893 			}
1894 		}
1895 		lp->d_npartitions = RAW_PART + 1;
1896 	}
1897 }
1898 
1899 void
1900 fd_do_eject(fdc, unit)
1901 	struct fdc_softc *fdc;
1902 	int unit;
1903 {
1904 	fdc->sc_fcr |= FCR_DSEL(unit)|FCR_EJECT;
1905 	FCR_REG_SYNC();
1906 	delay(10);
1907 	fdc->sc_fcr &= ~(FCR_DSEL(unit)|FCR_EJECT);
1908 	FCR_REG_SYNC();
1909 }
1910 
1911 #ifdef MEMORY_DISK_HOOKS_sun3x_not_yet
1912 int	fd_read_md_image __P((size_t *, caddr_t *));
1913 #endif
1914 
1915 /* ARGSUSED */
1916 void
1917 fd_mountroot_hook(dev)
1918 	struct device *dev;
1919 {
1920 	int c;
1921 
1922 	fd_do_eject(fdc_cd.cd_devs[0], 0); /* XXX - doesn't check ``dev'' */
1923 	printf("Insert filesystem floppy and press return.");
1924 	for (;;) {
1925 		c = cngetc();
1926 		if ((c == '\r') || (c == '\n')) {
1927 			printf("\n");
1928 			break;
1929 		}
1930 	}
1931 #ifdef MEMORY_DISK_HOOKS_sun3x_not_yet
1932 	{
1933 	extern int (*md_read_image) __P((size_t *, caddr_t *));
1934 	md_read_image = fd_read_md_image;
1935 	}
1936 #endif
1937 }
1938 
1939 #ifdef MEMORY_DISK_HOOKS_sun3x_not_yet
1940 
1941 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
1942 
1943 int
1944 fd_read_md_image(sizep, addrp)
1945 	size_t	*sizep;
1946 	caddr_t	*addrp;
1947 {
1948 	struct buf buf, *bp = &buf;
1949 	dev_t dev;
1950 	off_t offset;
1951 	caddr_t addr;
1952 
1953 	dev = makedev(54,0);	/* XXX */
1954 
1955 	MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
1956 	*addrp = addr;
1957 
1958 	if (fdopen(dev, 0, S_IFCHR, NULL))
1959 		panic("fd: mountroot: fdopen");
1960 
1961 	offset = 0;
1962 
1963 	for (;;) {
1964 		bp->b_dev = dev;
1965 		bp->b_error = 0;
1966 		bp->b_resid = 0;
1967 		bp->b_proc = NULL;
1968 		bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
1969 		bp->b_blkno = btodb(offset);
1970 		bp->b_bcount = DEV_BSIZE;
1971 		bp->b_data = addr;
1972 		fdstrategy(bp);
1973 		while ((bp->b_flags & B_DONE) == 0) {
1974 			tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0);
1975 		}
1976 		if (bp->b_error)
1977 			panic("fd: mountroot: fdread error %d", bp->b_error);
1978 
1979 		if (bp->b_resid != 0)
1980 			break;
1981 
1982 		addr += DEV_BSIZE;
1983 		offset += DEV_BSIZE;
1984 		if (offset + DEV_BSIZE > FDMICROROOTSIZE)
1985 			break;
1986 	}
1987 	(void)fdclose(dev, 0, S_IFCHR, NULL);
1988 	*sizep = offset;
1989 	fd_do_eject(fdc_cd.cd_devs[0], FDUNIT(dev)); /* XXX */
1990 	return (0);
1991 }
1992 #endif
1993