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