xref: /netbsd-src/sys/arch/x68k/dev/fd.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: fd.c,v 1.8 1996/10/13 03:34:44 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Don Ahn.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/conf.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/ioctl.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
51 #include <sys/disklabel.h>
52 #include <sys/dkstat.h>
53 #include <sys/disk.h>
54 #include <sys/buf.h>
55 #include <sys/uio.h>
56 #include <sys/syslog.h>
57 #include <sys/queue.h>
58 
59 #include <machine/cpu.h>
60 
61 #include <x68k/x68k/iodevice.h>
62 #include <x68k/dev/dmavar.h>
63 #include <x68k/dev/fdreg.h>
64 #include <x68k/dev/opmreg.h>
65 
66 #define infdc   (IODEVbase->io_fdc)
67 
68 #ifdef DEBUG
69 #define DPRINTF(x)      if (fddebug) printf x
70 int     fddebug = 0;
71 #else
72 #define DPRINTF(x)
73 #endif
74 
75 #define FDUNIT(dev)	(minor(dev) / 8)
76 #define FDTYPE(dev)	(minor(dev) % 8)
77 
78 #define b_cylin b_resid
79 
80 enum fdc_state {
81 	DEVIDLE = 0,
82 	MOTORWAIT,
83 	DOSEEK,
84 	SEEKWAIT,
85 	SEEKTIMEDOUT,
86 	SEEKCOMPLETE,
87 	DOIO,
88 	IOCOMPLETE,
89 	IOTIMEDOUT,
90 	DORESET,
91 	RESETCOMPLETE,
92 	RESETTIMEDOUT,
93 	DORECAL,
94 	RECALWAIT,
95 	RECALTIMEDOUT,
96 	RECALCOMPLETE,
97 	DOCOPY,
98 	DOIOHALF,
99 	COPYCOMPLETE,
100 };
101 
102 /* software state, per controller */
103 struct fdc_softc {
104 	struct device sc_dev;		/* boilerplate */
105 	u_char	sc_flags;
106 
107 	struct fd_softc *sc_fd[4];	/* pointers to children */
108 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
109 	enum fdc_state sc_state;
110 	int sc_errors;			/* number of retries so far */
111 	u_char sc_status[7];		/* copy of registers */
112 } fdc_softc;
113 
114 /* controller driver configuration */
115 int fdcinit();
116 void fdcstart();
117 void fdcgo();
118 int fdcintr ();
119 void fdcdone();
120 void fdcreset();
121 
122 /* controller driver configuration */
123 int fdcprobe __P((struct device *, void *, void *));
124 void fdcattach __P((struct device *, struct device *, void *));
125 
126 struct cfattach fdc_ca = {
127 	sizeof(struct fdc_softc), fdcprobe, fdcattach
128 };
129 
130 struct cfdriver fdc_cd = {
131 	NULL, "fdc", DV_DULL
132 };
133 
134 /*
135  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
136  * we tell them apart.
137  */
138 struct fd_type {
139 	int	sectrac;	/* sectors per track */
140 	int	heads;		/* number of heads */
141 	int	seccyl;		/* sectors per cylinder */
142 	int	secsize;	/* size code for sectors */
143 	int	datalen;	/* data len when secsize = 0 */
144 	int	steprate;	/* step rate and head unload time */
145 	int	gap1;		/* gap len between sectors */
146 	int	gap2;		/* formatting gap */
147 	int	tracks;		/* total num of tracks */
148 	int	size;		/* size of disk in sectors */
149 	int	step;		/* steps per cylinder */
150 	int	rate;		/* transfer speed code */
151 	char	*name;
152 };
153 
154 /* The order of entries in the following table is important -- BEWARE! */
155 struct fd_type fd_types[] = {
156         {  8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS, "1.2MB/[1024bytes/sector]"    }, /* 1.2 MB japanese format */
157         { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    }, /* 1.44MB diskette */
158         { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB"    }, /* 1.2 MB AT-diskettes */
159         {  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" }, /* 360kB in 1.2MB drive */
160         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" }, /* 360kB PC diskettes */
161         {  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB"    }, /* 3.5" 720kB diskette */
162         {  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x"  }, /* 720kB in 1.2MB drive */
163         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x"  }, /* 360kB in 720kB drive */
164 };
165 
166 /* software state, per disk (with up to 4 disks per ctlr) */
167 struct fd_softc {
168 	struct device sc_dev;
169 	struct disk sc_dk;
170 
171 	struct fd_type *sc_deftype;	/* default type descriptor */
172 	struct fd_type *sc_type;	/* current type descriptor */
173 
174 	daddr_t	sc_blkno;	/* starting block number */
175 	int sc_bcount;		/* byte count left */
176 	int sc_skip;		/* bytes already transferred */
177 	int sc_nblks;		/* number of blocks currently tranferring */
178 	int sc_nbytes;		/* number of bytes currently tranferring */
179 
180 	int sc_drive;		/* physical unit number */
181 	int sc_flags;
182 #define	FD_BOPEN	0x01		/* it's open */
183 #define	FD_COPEN	0x02		/* it's open */
184 #define	FD_OPEN		(FD_BOPEN|FD_COPEN)	/* it's open */
185 #define	FD_MOTOR	0x04		/* motor should be on */
186 #define	FD_MOTOR_WAIT	0x08		/* motor coming up */
187 #define	FD_ALIVE	0x10		/* alive */
188 	int sc_cylin;		/* where we think the head is */
189 
190 	TAILQ_ENTRY(fd_softc) sc_drivechain;
191 	int sc_ops;		/* I/O ops since last switch */
192 	struct buf sc_q;	/* head of buf chain */
193 	u_char *sc_copybuf;	/* for secsize >=3 */
194 	u_char sc_part;		/* for secsize >=3 */
195 #define	SEC_P10	0x02		/* first part */
196 #define	SEC_P01	0x01		/* second part */
197 #define	SEC_P11	0x03		/* both part */
198 };
199 
200 /* floppy driver configuration */
201 int fdprobe __P((struct device *, void *, void *));
202 void fdattach __P((struct device *, struct device *, void *));
203 
204 struct cfattach fd_ca = {
205 	sizeof(struct fd_softc), fdprobe, fdattach
206 };
207 
208 struct cfdriver fd_cd = {
209 	NULL, "fd", DV_DISK
210 };
211 
212 /* floppy driver configuration */
213 void fdstart __P((struct fd_softc *fd));
214 void fdgo();
215 void fdintr();
216 
217 void fdstrategy __P((struct buf *));
218 
219 struct dkdriver fddkdriver = { fdstrategy };
220 
221 void fd_set_motor __P((struct fdc_softc *fdc, int reset));
222 void fd_motor_off __P((void *arg));
223 void fd_motor_on __P((void *arg));
224 int fdcresult __P((struct fdc_softc *fdc));
225 int out_fdc __P((u_char x));
226 void fdcstart __P((struct fdc_softc *fdc));
227 void fdcstatus __P((struct device *dv, int n, char *s));
228 void fdctimeout __P((void *arg));
229 void fdcpseudointr __P((void *arg));
230 void fdcretry __P((struct fdc_softc *fdc));
231 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
232 static int fdgetdisklabel __P((struct fd_softc *, dev_t));
233 static void fd_do_eject __P((int));
234 void fd_mountroot_hook __P((struct device *));
235 
236 #define FDDI_EN	0x02
237 #define FDCI_EN	0x04
238 #define	FDD_INT	0x40
239 #define	FDC_INT	0x80
240 
241 #define DMA_BRD	0x01
242 #define	DMA_BWR	0x02
243 
244 #define DRQ 0
245 
246 static u_char *fdc_dmabuf;
247 
248 static inline void
249 fdc_dmastart(read, addr, count)
250 	int read;
251 	caddr_t addr;
252 	int count;
253 {
254 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
255 
256 	DPRINTF(("fdc_dmastart: (%s, addr = %p, count = %d\n",
257 		 read ? "read" : "write", addr, count));
258 	if (dmarangecheck((vm_offset_t)addr, count)) {
259 		dma_bouncebytes[DRQ] = count;
260 		dma_dataaddr[DRQ] = addr;
261 		if (!(read)) {
262 			bcopy(addr, dma_bouncebuf[DRQ], count);
263 			dma_bounced[DRQ] = DMA_BWR;
264 		} else {
265 			dma_bounced[DRQ] = DMA_BRD;
266 		}
267 		addr = dma_bouncebuf[DRQ];
268 	} else {
269 		dma_bounced[DRQ] = 0;
270 	}
271 
272 	dmac->csr = 0xff;
273 	dmac->ocr = read ? 0xb2 : 0x32;
274 	dmac->mtc = (unsigned short)count;
275 	asm("nop");
276 	asm("nop");
277 	dmac->mar = (unsigned long)kvtop(addr);
278 #if defined(M68040)
279 		/*
280 		 * Push back dirty cache lines
281 		 */
282 		if (mmutype == MMU_68040)
283 			DCFP(kvtop(addr));
284 #endif
285 	dmac->ccr = 0x88;
286 }
287 
288 void
289 fdcdmaintr()
290 {
291 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
292 	dmac->csr = 0xff;
293 	PCIA(); /* XXX? by oki */
294 	if (dma_bounced[DRQ] == DMA_BRD) {
295 		bcopy(dma_bouncebuf[DRQ], dma_dataaddr[DRQ], dma_bouncebytes[DRQ]);
296 	}
297 	dma_bounced[DRQ] = 0;
298 }
299 
300 void
301 fdcdmaerrintr()
302 {
303 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
304 	printf("fdcdmaerrintr: csr=%x, cer=%x\n", dmac->csr, dmac->cer);
305 	dmac->csr = 0xff;
306 }
307 
308 int
309 fdcprobe(parent, match, aux)
310 	struct device *parent;
311 	void *match, *aux;
312 {
313 	if (strcmp("fdc", aux) != 0)
314 		return 0;
315 	return 1;
316 }
317 
318 /*
319  * Arguments passed between fdcattach and fdprobe.
320  */
321 struct fdc_attach_args {
322 	int fa_drive;
323 	struct fd_type *fa_deftype;
324 };
325 
326 /*
327  * Print the location of a disk drive (called just before attaching the
328  * the drive).  If `fdc' is not NULL, the drive was found but was not
329  * in the system config file; print the drive name as well.
330  * Return QUIET (config_find ignores this if the device was configured) to
331  * avoid printing `fdN not configured' messages.
332  */
333 int
334 fdprint(aux, fdc)
335 	void *aux;
336 	const char *fdc;
337 {
338 	register struct fdc_attach_args *fa = aux;
339 
340 	if (!fdc)
341 		printf(" drive %d", fa->fa_drive);
342 	return QUIET;
343 }
344 
345 void
346 fdcattach(parent, self, aux)
347 	struct device *parent, *self;
348 	void *aux;
349 {
350 	struct fdc_softc *fdc = (void *)self;
351 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
352 	struct fdc_attach_args fa;
353 
354 	fdc->sc_state = DEVIDLE;
355 	TAILQ_INIT(&fdc->sc_drives);
356 
357 	fdc->sc_flags  = 0;
358 
359 	/* reset */
360 	ioctlr.intr &= (~FDDI_EN);
361 	ioctlr.intr |= FDCI_EN;
362 	fdcresult(fdc);
363 	fdcreset();
364 
365 	/* Initialize DMAC channel */
366 	dmac->dcr = 0x80;
367 	dmac->scr = 0x04;
368 	dmac->csr = 0xff;
369 	dmac->cpr = 0x00;
370 	dmac->dar = (unsigned long) kvtop((void *)&infdc.data);
371 	dmac->mfc = 0x05;
372 	dmac->dfc = 0x05;
373 	dmac->bfc = 0x05;
374 	dmac->niv = 0x64;
375 	dmac->eiv = 0x65;
376 
377 	printf(": uPD72065 FDC\n");
378 	out_fdc(NE7CMD_SPECIFY);/* specify command */
379 	out_fdc(0xd0);
380 	out_fdc(0x10);
381 
382 	fdc_dmabuf = (u_char *)malloc(NBPG, M_DEVBUF, M_WAITOK);
383 	if (fdc_dmabuf == 0)
384 		printf("fdcinit: WARNING!! malloc() failed.\n");
385 	dma_bouncebuf[DRQ] = fdc_dmabuf;
386 
387 	/* physical limit: four drives per controller. */
388 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
389 		(void)config_found(self, (void *)&fa, fdprint);
390 	}
391 }
392 
393 void
394 fdcreset()
395 {
396 	infdc.stat = FDC_RESET;
397 }
398 
399 static int
400 fdcpoll(fdc)
401 	struct fdc_softc *fdc;
402 {
403 	int i = 25000;
404 	while (--i > 0) {
405 		if ((ioctlr.intr & 0x80)) {
406 			out_fdc(NE7CMD_SENSEI);
407 			fdcresult(fdc);
408 			break;
409 		}
410 		DELAY(100);
411 	}
412 	return i;
413 }
414 
415 int
416 fdprobe(parent, match, aux)
417 	struct device *parent;
418 	void *match, *aux;
419 {
420 	struct fdc_softc *fdc = (void *)parent;
421 	struct cfdata *cf = match;
422 	struct fd_type *type;
423 	int drive = cf->cf_unit;
424 	int n;
425 	int found = 0;
426 	int i;
427 
428 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive)
429 		return 0;
430 
431 	type = &fd_types[0];	/* XXX 1.2MB */
432 
433 	ioctlr.intr &= (~FDCI_EN);
434 
435 	/* select drive and turn on motor */
436 	infdc.select = 0x80 | (type->rate << 4)| drive;
437 	fdc_force_ready(FDCRDY);
438 	fdcpoll(fdc);
439 
440 retry:
441 	out_fdc(NE7CMD_RECAL);
442 	out_fdc(drive);
443 
444 	i = 25000;
445 	while (--i > 0) {
446 		if ((ioctlr.intr & 0x80)) {
447 			out_fdc(NE7CMD_SENSEI);
448 			n = fdcresult(fdc);
449 			break;
450 		}
451 		DELAY(100);
452 	}
453 
454 #ifdef FDDEBUG
455 	{
456 		int i;
457 		printf("fdprobe: status");
458 		for (i = 0; i < n; i++)
459 			printf(" %x", fdc->sc_status[i]);
460 		printf("\n");
461 	}
462 #endif
463 
464 	if (n == 2) {
465 		if ((fdc->sc_status[0] & 0xf0) == 0x20) {
466 			found = 1;
467 		} else if ((fdc->sc_status[0] & 0xf0) == 0xc0) {
468 			goto retry;
469 		}
470 	}
471 
472 	/* turn off motor */
473 	infdc.select = (type->rate << 4)| drive;
474 	fdc_force_ready(FDCSTBY);
475 	if (!found) {
476 		ioctlr.intr |= FDCI_EN;
477 		return 0;
478 	}
479 
480 	return 1;
481 }
482 
483 void
484 fdattach(parent, self, aux)
485 	struct device *parent;
486 	struct device *self;
487 	void *aux;
488 {
489 	struct fdc_softc *fdc = (void *)parent;
490 	register struct fd_softc *fd = (void *)self;
491 	struct fdc_attach_args *fa = aux;
492 	int drive = fa->fa_drive;
493 	struct fd_type *type = &fd_types[0];	/* XXX 1.2MB */
494 
495 	fd->sc_flags = 0;
496 
497 	ioctlr.intr |= FDCI_EN;
498 
499 	if (type)
500 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
501 			type->tracks, type->heads, type->sectrac);
502 	else
503 		printf(": density unknown\n");
504 
505 	fd->sc_cylin = -1;
506 	fd->sc_drive = drive;
507 	fd->sc_deftype = type;
508 	fdc->sc_fd[drive] = fd;
509 
510 	fd->sc_copybuf = (u_char *)malloc(NBPG, M_DEVBUF, M_WAITOK);
511 	if (fd->sc_copybuf == 0)
512 		printf("fdprobe: WARNING!! malloc() failed.\n");
513 	fd->sc_flags |= FD_ALIVE;
514 
515 	/*
516 	 * Initialize and attach the disk structure.
517 	 */
518 	fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
519 	fd->sc_dk.dk_driver = &fddkdriver;
520 	disk_attach(&fd->sc_dk);
521 
522 	/*
523 	 * Establish a mountroot_hook anyway in case we booted
524 	 * with RB_ASKNAME and get selected as the boot device.
525 	 */
526 	mountroot_hook_establish(fd_mountroot_hook, &fd->sc_dev);
527 }
528 
529 inline struct fd_type *
530 fd_dev_to_type(fd, dev)
531 	struct fd_softc *fd;
532 	dev_t dev;
533 {
534 	int type = FDTYPE(dev);
535 
536 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
537 		return NULL;
538 	return &fd_types[type];
539 }
540 
541 void
542 fdstrategy(bp)
543 	register struct buf *bp;	/* IO operation to perform */
544 {
545 	struct fd_softc *fd;
546 	int unit = FDUNIT(bp->b_dev);
547 	int sz;
548  	int s;
549 
550 	if (unit >= fd_cd.cd_ndevs ||
551 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
552 	    bp->b_blkno < 0 ||
553 	    (bp->b_bcount % FDC_BSIZE) != 0) {
554 #ifdef FDDEBUG
555 		printf("fdstrategy: unit=%d, blkno=%d, bcount=%d\n", unit,
556 		       bp->b_blkno, bp->b_bcount);
557 #endif
558 		bp->b_error = EINVAL;
559 		goto bad;
560 	}
561 
562 	/* If it's a null transfer, return immediately. */
563 	if (bp->b_bcount == 0)
564 		goto done;
565 
566 	sz = howmany(bp->b_bcount, FDC_BSIZE);
567 
568 	if (bp->b_blkno + sz > (fd->sc_type->size << (fd->sc_type->secsize - 2))) {
569 		sz = (fd->sc_type->size << (fd->sc_type->secsize - 2)) - bp->b_blkno;
570 		if (sz == 0) {
571 			/* If exactly at end of disk, return EOF. */
572 			bp->b_resid = bp->b_bcount;
573 			goto done;
574 		}
575 		if (sz < 0) {
576 			/* If past end of disk, return EINVAL. */
577 			bp->b_error = EINVAL;
578 			goto bad;
579 		}
580 		/* Otherwise, truncate request. */
581 		bp->b_bcount = sz << DEV_BSHIFT;
582 	}
583 
584  	bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE)
585 		/ (fd->sc_type->seccyl * (1 << (fd->sc_type->secsize - 2)));
586 
587 	DPRINTF(("fdstrategy: %s b_blkno %d b_bcount %ld cylin %ld\n",
588 		 bp->b_flags & B_READ ? "read" : "write",
589 		 bp->b_blkno, bp->b_bcount, bp->b_cylin));
590 	/* Queue transfer on drive, activate drive and controller if idle. */
591 	s = splbio();
592 	disksort(&fd->sc_q, bp);
593 	untimeout(fd_motor_off, fd); /* a good idea */
594 	if (!fd->sc_q.b_active)
595 		fdstart(fd);
596 #ifdef DIAGNOSTIC
597 	else {
598 		struct fdc_softc *fdc = fdc_cd.cd_devs[0];	/* XXX */
599 		if (fdc->sc_state == DEVIDLE) {
600 			printf("fdstrategy: controller inactive\n");
601 			fdcstart(fdc);
602 		}
603 	}
604 #endif
605 	splx(s);
606 	return;
607 
608 bad:
609 	bp->b_flags |= B_ERROR;
610 done:
611 	/* Toss transfer; we're done early. */
612 	biodone(bp);
613 }
614 
615 void
616 fdstart(fd)
617 	struct fd_softc *fd;
618 {
619 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
620 	int active = fdc->sc_drives.tqh_first != 0;
621 
622 	/* Link into controller queue. */
623 	fd->sc_q.b_active = 1;
624 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
625 
626 	/* If controller not already active, start it. */
627 	if (!active)
628 		fdcstart(fdc);
629 }
630 
631 void
632 fdfinish(fd, bp)
633 	struct fd_softc *fd;
634 	struct buf *bp;
635 {
636 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
637 
638 	/*
639 	 * Move this drive to the end of the queue to give others a `fair'
640 	 * chance.  We only force a switch if N operations are completed while
641 	 * another drive is waiting to be serviced, since there is a long motor
642 	 * startup delay whenever we switch.
643 	 */
644 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
645 		fd->sc_ops = 0;
646 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
647 		if (bp->b_actf) {
648 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
649 		} else
650 			fd->sc_q.b_active = 0;
651 	}
652 	bp->b_resid = fd->sc_bcount;
653 	fd->sc_skip = 0;
654 	fd->sc_q.b_actf = bp->b_actf;
655 	biodone(bp);
656 	/* turn off motor 5s from now */
657 	timeout(fd_motor_off, fd, 5 * hz);
658 	fdc->sc_state = DEVIDLE;
659 }
660 
661 int
662 fdread(dev, uio)
663 	dev_t dev;
664 	struct uio *uio;
665 {
666 
667 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
668 }
669 
670 int
671 fdwrite(dev, uio)
672 	dev_t dev;
673 	struct uio *uio;
674 {
675 
676 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
677 }
678 
679 void
680 fd_set_motor(fdc, reset)
681 	struct fdc_softc *fdc;
682 	int reset;
683 {
684 	struct fd_softc *fd;
685 	int n;
686 
687 	DPRINTF(("fd_set_motor:\n"));
688 	for (n = 0; n < 4; n++)
689 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) {
690 			infdc.select = 0x80 | (fd->sc_type->rate << 4)| n;
691 		}
692 }
693 
694 void
695 fd_motor_off(arg)
696 	void *arg;
697 {
698 	struct fd_softc *fd = arg;
699 	int s;
700 
701 	DPRINTF(("fd_motor_off:\n"));
702 
703 	s = splbio();
704 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
705 	infdc.select = (fd->sc_type->rate << 4) | fd->sc_drive;
706 #if 0
707 	fd_set_motor((struct fdc_softc *)&fdc_softc[0], 0); /* XXX */
708 #endif
709 	splx(s);
710 }
711 
712 void
713 fd_motor_on(arg)
714 	void *arg;
715 {
716 	struct fd_softc *fd = arg;
717 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
718 	int s;
719 
720 	DPRINTF(("fd_motor_on:\n"));
721 
722 	s = splbio();
723 	fd->sc_flags &= ~FD_MOTOR_WAIT;
724 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
725 		(void) fdcintr();
726 	splx(s);
727 }
728 
729 int
730 fdcresult(fdc)
731 	struct fdc_softc *fdc;
732 {
733 	u_char i;
734 	int j = 100000,
735 	    n = 0;
736 
737 	for (; j; j--) {
738 
739 		i = infdc.stat & (NE7_DIO | NE7_RQM | NE7_CB);
740 
741 
742 		if (i == NE7_RQM)
743 			return n;
744 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
745 			if (n >= sizeof(fdc->sc_status)) {
746 				log(LOG_ERR, "fdcresult: overrun\n");
747 				return -1;
748 			}
749 			fdc->sc_status[n++] = infdc.data;
750 		}
751 	}
752 	log(LOG_ERR, "fdcresult: timeout\n");
753 	return -1;
754 }
755 
756 int
757 out_fdc(x)
758 	u_char x;
759 {
760 	int i = 100000;
761 
762 	while ((infdc.stat & NE7_DIO) && i-- > 0);
763 	if (i <= 0)
764 		return -1;
765 	while ((infdc.stat & NE7_RQM) == 0 && i-- > 0);
766 	if (i <= 0)
767 		return -1;
768 
769 	infdc.data = x;
770 
771 	return 0;
772 }
773 
774 int
775 Fdopen(dev, flags, fmt)
776 	dev_t dev;
777 	int flags, fmt;
778 {
779  	int unit;
780 	struct fd_softc *fd;
781 	struct fd_type *type;
782 
783 	unit = FDUNIT(dev);
784 	if (unit >= fd_cd.cd_ndevs)
785 		return ENXIO;
786 	fd = fd_cd.cd_devs[unit];
787 	if (fd == 0)
788 		return ENXIO;
789 	type = fd_dev_to_type(fd, dev);
790 	if (type == NULL)
791 		return ENXIO;
792 
793 	if ((fd->sc_flags & FD_OPEN) != 0 &&
794 	    fd->sc_type != type)
795 		return EBUSY;
796 
797 	if ((fd->sc_flags & FD_OPEN) == 0) {
798 		/* Lock eject button */
799 		infdc.drvstat = 0x40 | ( 1 << unit);
800 		infdc.drvstat = 0x40;
801 	}
802 
803 	fd->sc_type = type;
804 	fd->sc_cylin = -1;
805 
806 	switch (fmt) {
807 	case S_IFCHR:
808 		fd->sc_flags |= FD_COPEN;
809 		break;
810 	case S_IFBLK:
811 		fd->sc_flags |= FD_BOPEN;
812 		break;
813 	}
814 
815 	fdgetdisklabel(fd, dev);
816 
817 	return 0;
818 }
819 
820 int
821 fdclose(dev, flags, fmt)
822 	dev_t dev;
823 	int flags, fmt;
824 {
825  	int unit = FDUNIT(dev);
826 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
827 
828 	DPRINTF(("fdclose %d\n", unit));
829 
830 	switch (fmt) {
831 	case S_IFCHR:
832 		fd->sc_flags &= ~FD_COPEN;
833 		break;
834 	case S_IFBLK:
835 		fd->sc_flags &= ~FD_BOPEN;
836 		break;
837 	}
838 
839 	if ((fd->sc_flags & FD_OPEN) == 0) {
840 		infdc.drvstat = ( 1 << unit);
841 		infdc.drvstat = 0x00;
842 	}
843 	return 0;
844 }
845 
846 void
847 fdcstart(fdc)
848 	struct fdc_softc *fdc;
849 {
850 
851 #ifdef DIAGNOSTIC
852 	/* only got here if controller's drive queue was inactive; should
853 	   be in idle state */
854 	if (fdc->sc_state != DEVIDLE) {
855 		printf("fdcstart: not idle\n");
856 		return;
857 	}
858 #endif
859 	(void) fdcintr();
860 }
861 
862 void
863 fdcstatus(dv, n, s)
864 	struct device *dv;
865 	int n;
866 	char *s;
867 {
868 	struct fdc_softc *fdc = (void *)dv->dv_parent;
869 
870 	if (n == 0) {
871 		out_fdc(NE7CMD_SENSEI);
872 		(void) fdcresult(fdc);
873 		n = 2;
874 	}
875 
876 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
877 
878 	switch (n) {
879 	case 0:
880 		printf("\n");
881 		break;
882 	case 2:
883 		printf(" (st0 %b cyl %d)\n",
884 		    fdc->sc_status[0], NE7_ST0BITS,
885 		    fdc->sc_status[1]);
886 		break;
887 	case 7:
888 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d\n",
889 		    fdc->sc_status[0], NE7_ST0BITS,
890 		    fdc->sc_status[1], NE7_ST1BITS,
891 		    fdc->sc_status[2], NE7_ST2BITS,
892 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
893 		break;
894 #ifdef DIAGNOSTIC
895 	default:
896 		printf(" fdcstatus: weird size: %d\n", n);
897 		break;
898 #endif
899 	}
900 }
901 
902 void
903 fdctimeout(arg)
904 	void *arg;
905 {
906 	struct fdc_softc *fdc = arg;
907 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
908 	int s;
909 
910 	s = splbio();
911 	fdcstatus(&fd->sc_dev, 0, "timeout");
912 
913 	if (fd->sc_q.b_actf)
914 		fdc->sc_state++;
915 	else
916 		fdc->sc_state = DEVIDLE;
917 
918 	(void) fdcintr();
919 	splx(s);
920 }
921 
922 void
923 fdcpseudointr(arg)
924 	void *arg;
925 {
926 	int s;
927 
928 	/* just ensure it has the right spl */
929 	s = splbio();
930 	(void) fdcintr();
931 	splx(s);
932 }
933 
934 int
935 fdcintr()
936 {
937 	struct fdc_softc *fdc = fdc_cd.cd_devs[0];	/* XXX */
938 #define	st0	fdc->sc_status[0]
939 #define	cyl	fdc->sc_status[1]
940 	struct fd_softc *fd;
941 	struct buf *bp;
942 	int read, head, sec, pos, i, sectrac, nblks;
943 	int	tmp;
944 	struct fd_type *type;
945 
946 loop:
947 	fd = fdc->sc_drives.tqh_first;
948 	if (fd == NULL) {
949 		DPRINTF(("fdcintr: set DEVIDLE\n"));
950 		if (fdc->sc_state == DEVIDLE) {
951 			if ((ioctlr.intr & 0x80)) {
952 				out_fdc(NE7CMD_SENSEI);
953 				if ((tmp = fdcresult(fdc)) != 2 || (st0 & 0xf8) != 0x20) {
954 					goto loop;
955 				}
956 			}
957 		}
958 		/* no drives waiting; end */
959 		fdc->sc_state = DEVIDLE;
960  		return 1;
961 	}
962 
963 	/* Is there a transfer to this drive?  If not, deactivate drive. */
964 	bp = fd->sc_q.b_actf;
965 	if (bp == NULL) {
966 		fd->sc_ops = 0;
967 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
968 		fd->sc_q.b_active = 0;
969 		goto loop;
970 	}
971 
972 	switch (fdc->sc_state) {
973 	case DEVIDLE:
974 		DPRINTF(("fdcintr: in DEVIDLE\n"));
975 		fdc->sc_errors = 0;
976 		fd->sc_skip = 0;
977 		fd->sc_bcount = bp->b_bcount;
978 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
979 		untimeout(fd_motor_off, fd);
980 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
981 			fdc->sc_state = MOTORWAIT;
982 			return 1;
983 		}
984 		if ((fd->sc_flags & FD_MOTOR) == 0) {
985 			/* Turn on the motor */
986 			/* being careful about other drives. */
987 			for (i = 0; i < 4; i++) {
988 				struct fd_softc *ofd = fdc->sc_fd[i];
989 				if (ofd && ofd->sc_flags & FD_MOTOR) {
990 					untimeout(fd_motor_off, ofd);
991 					ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
992 					break;
993 				}
994 			}
995 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
996 			fd_set_motor(fdc, 0);
997 			fdc->sc_state = MOTORWAIT;
998 			/* allow .5s for motor to stabilize */
999 			timeout(fd_motor_on, fd, hz / 2);
1000 			return 1;
1001 		}
1002 		/* Make sure the right drive is selected. */
1003 		fd_set_motor(fdc, 0);
1004 
1005 		/* fall through */
1006 	case DOSEEK:
1007 	doseek:
1008 		DPRINTF(("fdcintr: in DOSEEK\n"));
1009 		if (fd->sc_cylin == bp->b_cylin)
1010 			goto doio;
1011 
1012 		out_fdc(NE7CMD_SPECIFY);/* specify command */
1013 		out_fdc(0xd0);		/* XXX const */
1014 		out_fdc(0x10);
1015 
1016 		out_fdc(NE7CMD_SEEK);	/* seek function */
1017 		out_fdc(fd->sc_drive);	/* drive number */
1018 		out_fdc(bp->b_cylin * fd->sc_type->step);
1019 
1020 		fd->sc_cylin = -1;
1021 		fdc->sc_state = SEEKWAIT;
1022 
1023 		fd->sc_dk.dk_seek++;
1024 		disk_busy(&fd->sc_dk);
1025 
1026 		timeout(fdctimeout, fdc, 4 * hz);
1027 		return 1;
1028 
1029 	case DOIO:
1030 	doio:
1031 		DPRINTF(("fdcintr: DOIO: "));
1032 		type = fd->sc_type;
1033 		sectrac = type->sectrac;
1034 		pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
1035 		sec = pos / (1 << (type->secsize - 2));
1036 		if (type->secsize == 2) {
1037 			fd->sc_part = SEC_P11;
1038 			nblks = (sectrac - sec) << (type->secsize - 2);
1039 			nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1040 			DPRINTF(("nblks(0)"));
1041 		} else if ((fd->sc_blkno % 2) == 0) {
1042 			if (fd->sc_bcount & 0x00000200) {
1043 				if (fd->sc_bcount == FDC_BSIZE) {
1044 					fd->sc_part = SEC_P10;
1045 					nblks = 1;
1046 					DPRINTF(("nblks(1)"));
1047 				} else {
1048 					fd->sc_part = SEC_P11;
1049 					nblks = (sectrac - sec) * 2;
1050 					nblks = min(nblks, fd->sc_bcount
1051 						    / FDC_BSIZE - 1);
1052 					DPRINTF(("nblks(2)"));
1053 				}
1054 			} else {
1055 				fd->sc_part = SEC_P11;
1056 				nblks = (sectrac - sec)
1057 					<< (type->secsize - 2);
1058 				nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1059 				DPRINTF(("nblks(3)"));
1060 			}
1061 		} else {
1062 			fd->sc_part = SEC_P01;
1063 			nblks = 1;
1064 			DPRINTF(("nblks(4)"));
1065 		}
1066 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
1067 		DPRINTF((" %d\n", nblks));
1068 		fd->sc_nblks = nblks;
1069 		fd->sc_nbytes = nblks * FDC_BSIZE;
1070 		head = (fd->sc_blkno
1071 			% (type->seccyl * (1 << (type->secsize - 2))))
1072 			 / (type->sectrac * (1 << (type->secsize - 2)));
1073 
1074 #ifdef DIAGNOSTIC
1075 		{int block;
1076 		 block = ((fd->sc_cylin * type->heads + head) * type->sectrac
1077 			  + sec) * (1 << (type->secsize - 2));
1078 		 block += (fd->sc_part == SEC_P01) ? 1 : 0;
1079 		 if (block != fd->sc_blkno) {
1080 			 printf("C H R N: %d %d %d %d\n", fd->sc_cylin, head, sec, type->secsize);
1081 			 printf("fdcintr: doio: block %d != blkno %d\n", block, fd->sc_blkno);
1082 #ifdef DDB
1083 			 Debugger();
1084 #endif
1085 		 }}
1086 #endif
1087 		read = bp->b_flags & B_READ;
1088 		DPRINTF(("fdcintr: %s drive %d track %d head %d sec %d nblks %d, skip %d\n",
1089 			 read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
1090 			 head, sec, nblks, fd->sc_skip));
1091 		DPRINTF(("C H R N: %d %d %d %d\n", fd->sc_cylin, head, sec,
1092 			 type->secsize));
1093 
1094 		if (fd->sc_part != SEC_P11)
1095 			goto docopy;
1096 
1097 		fdc_dmastart(read, bp->b_data + fd->sc_skip, fd->sc_nbytes);
1098 		if (read)
1099 			out_fdc(NE7CMD_READ);	/* READ */
1100 		else
1101 			out_fdc(NE7CMD_WRITE);	/* WRITE */
1102 		out_fdc((head << 2) | fd->sc_drive);
1103 		out_fdc(bp->b_cylin);		/* cylinder */
1104 		out_fdc(head);
1105 		out_fdc(sec + 1);		/* sector +1 */
1106 		out_fdc(type->secsize);		/* sector size */
1107 		out_fdc(type->sectrac);		/* sectors/track */
1108 		out_fdc(type->gap1);		/* gap1 size */
1109 		out_fdc(type->datalen);		/* data length */
1110 		fdc->sc_state = IOCOMPLETE;
1111 
1112 		disk_busy(&fd->sc_dk);
1113 
1114 		/* allow 2 seconds for operation */
1115 		timeout(fdctimeout, fdc, 2 * hz);
1116 		return 1;				/* will return later */
1117 
1118 	case DOCOPY:
1119 	docopy:
1120 		DPRINTF(("fdcintr: DOCOPY:\n"));
1121 		fdc_dmastart(B_READ, fd->sc_copybuf, 1024);
1122 		out_fdc(NE7CMD_READ);	/* READ */
1123 		out_fdc((head << 2) | fd->sc_drive);
1124 		out_fdc(bp->b_cylin);		/* cylinder */
1125 		out_fdc(head);
1126 		out_fdc(sec + 1);		/* sector +1 */
1127 		out_fdc(type->secsize);		/* sector size */
1128 		out_fdc(type->sectrac);		/* sectors/track */
1129 		out_fdc(type->gap1);		/* gap1 size */
1130 		out_fdc(type->datalen);		/* data length */
1131 		fdc->sc_state = COPYCOMPLETE;
1132 		/* allow 2 seconds for operation */
1133 		timeout(fdctimeout, fdc, 2 * hz);
1134 		return 1;				/* will return later */
1135 
1136 	case DOIOHALF:
1137 	doiohalf:
1138 		DPRINTF((" DOIOHALF:\n"));
1139 
1140 #ifdef DIAGNOSTIC
1141 		type = fd->sc_type;
1142 		sectrac = type->sectrac;
1143 		pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
1144 		sec = pos / (1 << (type->secsize - 2));
1145 		head = (fd->sc_blkno
1146 			% (type->seccyl * (1 << (type->secsize - 2))))
1147 			 / (type->sectrac * (1 << (type->secsize - 2)));
1148 		{int block;
1149 		 block = ((fd->sc_cylin * type->heads + head) * type->sectrac + sec)
1150 			 * (1 << (type->secsize - 2));
1151 		 block += (fd->sc_part == SEC_P01) ? 1 : 0;
1152 		 if (block != fd->sc_blkno) {
1153 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1154 #ifdef DDB
1155 			 Debugger();
1156 #endif
1157 		 }}
1158 #endif
1159 		if (read = bp->b_flags & B_READ) {
1160 			bcopy(fd->sc_copybuf
1161 			      + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
1162 			      bp->b_data + fd->sc_skip,
1163 			      FDC_BSIZE);
1164 			fdc->sc_state = IOCOMPLETE;
1165 			goto iocomplete2;
1166 		} else {
1167 			bcopy(bp->b_data + fd->sc_skip,
1168 			      fd->sc_copybuf
1169 			      + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
1170 			      FDC_BSIZE);
1171 			fdc_dmastart(read, fd->sc_copybuf, 1024);
1172 		}
1173 		out_fdc(NE7CMD_WRITE);	/* WRITE */
1174 		out_fdc((head << 2) | fd->sc_drive);
1175 		out_fdc(bp->b_cylin);		/* cylinder */
1176 		out_fdc(head);
1177 		out_fdc(sec + 1);		/* sector +1 */
1178 		out_fdc(fd->sc_type->secsize);		/* sector size */
1179 		out_fdc(sectrac);		/* sectors/track */
1180 		out_fdc(fd->sc_type->gap1);		/* gap1 size */
1181 		out_fdc(fd->sc_type->datalen);		/* data length */
1182 		fdc->sc_state = IOCOMPLETE;
1183 		/* allow 2 seconds for operation */
1184 		timeout(fdctimeout, fdc, 2 * hz);
1185 		return 1;				/* will return later */
1186 
1187 	case SEEKWAIT:
1188 		untimeout(fdctimeout, fdc);
1189 		fdc->sc_state = SEEKCOMPLETE;
1190 		/* allow 1/50 second for heads to settle */
1191 /*		timeout(fdcpseudointr, fdc, hz / 50);*/
1192 		return 1;
1193 
1194 	case SEEKCOMPLETE:
1195 		/* Make sure seek really happened */
1196 		DPRINTF(("fdcintr: SEEKCOMPLETE: FDC status = %x\n",
1197 			 infdc.stat));
1198 		out_fdc(NE7CMD_SENSEI);
1199 		tmp = fdcresult(fdc);
1200 		if ((st0 & 0xf8) == 0xc0) {
1201 			DPRINTF(("fdcintr: first seek!\n"));
1202 			fdc->sc_state = DORECAL;
1203 			goto loop;
1204 		} else if (tmp != 2 || (st0 & 0xf8) != 0x20 || cyl != bp->b_cylin) {
1205 #ifdef FDDEBUG
1206 			fdcstatus(&fd->sc_dev, 2, "seek failed");
1207 #endif
1208 			fdcretry(fdc);
1209 			goto loop;
1210 		}
1211 		fd->sc_cylin = bp->b_cylin;
1212 		goto doio;
1213 
1214 	case IOTIMEDOUT:
1215 #if 0
1216 		isa_dmaabort(fdc->sc_drq);
1217 #endif
1218 	case SEEKTIMEDOUT:
1219 	case RECALTIMEDOUT:
1220 	case RESETTIMEDOUT:
1221 		fdcretry(fdc);
1222 		goto loop;
1223 
1224 	case IOCOMPLETE: /* IO DONE, post-analyze */
1225 		untimeout(fdctimeout, fdc);
1226 		DPRINTF(("fdcintr: in IOCOMPLETE\n"));
1227 		if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
1228 			printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
1229 #if 0
1230 			isa_dmaabort(fdc->sc_drq);
1231 #endif
1232 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
1233 				  "read failed" : "write failed");
1234 			printf("blkno %d nblks %d\n",
1235 			    fd->sc_blkno, fd->sc_nblks);
1236 			fdcretry(fdc);
1237 			goto loop;
1238 		}
1239 #if 0
1240 		isa_dmadone(bp->b_flags & B_READ, bp->b_data + fd->sc_skip,
1241 		    nblks * FDC_BSIZE, fdc->sc_drq);
1242 #endif
1243 	iocomplete2:
1244 		if (fdc->sc_errors) {
1245 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
1246 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1247 			printf("\n");
1248 			fdc->sc_errors = 0;
1249 		}
1250 		fd->sc_blkno += fd->sc_nblks;
1251 		fd->sc_skip += fd->sc_nbytes;
1252 		fd->sc_bcount -= fd->sc_nbytes;
1253 		DPRINTF(("fd->sc_bcount = %d\n", fd->sc_bcount));
1254 		if (fd->sc_bcount > 0) {
1255 			bp->b_cylin = fd->sc_blkno
1256 				/ (fd->sc_type->seccyl
1257 				   * (1 << (fd->sc_type->secsize - 2)));
1258 			goto doseek;
1259 		}
1260 		fdfinish(fd, bp);
1261 		goto loop;
1262 
1263 	case COPYCOMPLETE: /* IO DONE, post-analyze */
1264 		DPRINTF(("fdcintr: COPYCOMPLETE:"));
1265 		untimeout(fdctimeout, fdc);
1266 		if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
1267 			printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
1268 #if 0
1269 			isa_dmaabort(fdc->sc_drq);
1270 #endif
1271 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
1272 				  "read failed" : "write failed");
1273 			printf("blkno %d nblks %d\n",
1274 			    fd->sc_blkno, fd->sc_nblks);
1275 			fdcretry(fdc);
1276 			goto loop;
1277 		}
1278 		goto doiohalf;
1279 
1280 	case DORESET:
1281 		DPRINTF(("fdcintr: in DORESET\n"));
1282 		/* try a reset, keep motor on */
1283 		fd_set_motor(fdc, 1);
1284 		DELAY(100);
1285 		fd_set_motor(fdc, 0);
1286 		fdc->sc_state = RESETCOMPLETE;
1287 		timeout(fdctimeout, fdc, hz / 2);
1288 		return 1;			/* will return later */
1289 
1290 	case RESETCOMPLETE:
1291 		DPRINTF(("fdcintr: in RESETCOMPLETE\n"));
1292 		untimeout(fdctimeout, fdc);
1293 		/* clear the controller output buffer */
1294 		for (i = 0; i < 4; i++) {
1295 			out_fdc(NE7CMD_SENSEI);
1296 			(void) fdcresult(fdc);
1297 		}
1298 
1299 		/* fall through */
1300 	case DORECAL:
1301 		DPRINTF(("fdcintr: in DORECAL\n"));
1302 		out_fdc(NE7CMD_RECAL);	/* recalibrate function */
1303 		out_fdc(fd->sc_drive);
1304 		fdc->sc_state = RECALWAIT;
1305 		timeout(fdctimeout, fdc, 5 * hz);
1306 		return 1;			/* will return later */
1307 
1308 	case RECALWAIT:
1309 		DPRINTF(("fdcintr: in RECALWAIT\n"));
1310 		untimeout(fdctimeout, fdc);
1311 		fdc->sc_state = RECALCOMPLETE;
1312 		/* allow 1/30 second for heads to settle */
1313 /*		timeout(fdcpseudointr, fdc, hz / 30);*/
1314 		return 1;			/* will return later */
1315 
1316 	case RECALCOMPLETE:
1317 		DPRINTF(("fdcintr: in RECALCOMPLETE\n"));
1318 		out_fdc(NE7CMD_SENSEI);
1319 		tmp = fdcresult(fdc);
1320 		if ((st0 & 0xf8) == 0xc0) {
1321 			DPRINTF(("fdcintr: first seek!\n"));
1322 			fdc->sc_state = DORECAL;
1323 			goto loop;
1324 		} else if (tmp != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1325 #ifdef FDDEBUG
1326 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
1327 #endif
1328 			fdcretry(fdc);
1329 			goto loop;
1330 		}
1331 		fd->sc_cylin = 0;
1332 		goto doseek;
1333 
1334 	case MOTORWAIT:
1335 		if (fd->sc_flags & FD_MOTOR_WAIT)
1336 			return 1;		/* time's not up yet */
1337 		goto doseek;
1338 
1339 	default:
1340 		fdcstatus(&fd->sc_dev, 0, "stray interrupt");
1341 		return 1;
1342 	}
1343 #ifdef DIAGNOSTIC
1344 	panic("fdcintr: impossible");
1345 #endif
1346 #undef	st0
1347 #undef	cyl
1348 }
1349 
1350 void
1351 fdcretry(fdc)
1352 	struct fdc_softc *fdc;
1353 {
1354 	struct fd_softc *fd;
1355 	struct buf *bp;
1356 
1357 	DPRINTF(("fdcretry:\n"));
1358 	fd = fdc->sc_drives.tqh_first;
1359 	bp = fd->sc_q.b_actf;
1360 
1361 	switch (fdc->sc_errors) {
1362 	case 0:
1363 		/* try again */
1364 		fdc->sc_state = SEEKCOMPLETE;
1365 		break;
1366 
1367 	case 1: case 2: case 3:
1368 		/* didn't work; try recalibrating */
1369 		fdc->sc_state = DORECAL;
1370 		break;
1371 
1372 	case 4:
1373 		/* still no go; reset the bastard */
1374 		fdc->sc_state = DORESET;
1375 		break;
1376 
1377 	default:
1378 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
1379 			fd->sc_skip, (struct disklabel *)NULL);
1380 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
1381 		    fdc->sc_status[0], NE7_ST0BITS,
1382 		    fdc->sc_status[1], NE7_ST1BITS,
1383 		    fdc->sc_status[2], NE7_ST2BITS,
1384 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1385 
1386 		bp->b_flags |= B_ERROR;
1387 		bp->b_error = EIO;
1388 		fdfinish(fd, bp);
1389 	}
1390 	fdc->sc_errors++;
1391 }
1392 
1393 int
1394 fdsize(dev)
1395 	dev_t dev;
1396 {
1397 
1398 	/* Swapping to floppies would not make sense. */
1399 	return -1;
1400 }
1401 
1402 int
1403 fddump(dev, blkno, va, size)
1404 	dev_t dev;
1405 	daddr_t blkno;
1406 	caddr_t va;
1407 	size_t size;
1408 {
1409 
1410 	/* Not implemented. */
1411 	return ENXIO;
1412 }
1413 
1414 int
1415 fdioctl(dev, cmd, addr, flag)
1416 	dev_t dev;
1417 	u_long cmd;
1418 	caddr_t addr;
1419 	int flag;
1420 {
1421 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1422 	int unit = FDUNIT(dev);
1423 	struct disklabel buffer;
1424 	int error;
1425 
1426 	DPRINTF(("fdioctl:\n"));
1427 	switch (cmd) {
1428 	case DIOCGDINFO:
1429 #if 1
1430 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1431 		return(0);
1432 #else
1433 		bzero(&buffer, sizeof(buffer));
1434 
1435 		buffer.d_secpercyl = fd->sc_type->seccyl;
1436 		buffer.d_type = DTYPE_FLOPPY;
1437 		buffer.d_secsize = 128 << fd->sc_type->secsize;
1438 
1439 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1440 			return EINVAL;
1441 
1442 		*(struct disklabel *)addr = buffer;
1443 		return 0;
1444 #endif
1445 
1446 	case DIOCGPART:
1447 		((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
1448 		((struct partinfo *)addr)->part =
1449 		    &fd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
1450 		return(0);
1451 
1452 	case DIOCWLABEL:
1453 		if ((flag & FWRITE) == 0)
1454 			return EBADF;
1455 		/* XXX do something */
1456 		return 0;
1457 
1458 	case DIOCWDINFO:
1459 		if ((flag & FWRITE) == 0)
1460 			return EBADF;
1461 
1462 		error = setdisklabel(&buffer, (struct disklabel *)addr, 0, NULL);
1463 		if (error)
1464 			return error;
1465 
1466 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1467 		return error;
1468 
1469 	case DIOCLOCK:
1470 		/*
1471 		 * Nothing to do here, really.
1472 		 */
1473 		return 0; /* XXX */
1474 
1475 	case DIOCEJECT:
1476 		fd_do_eject(unit);
1477 		return 0;
1478 
1479 	default:
1480 		return ENOTTY;
1481 	}
1482 
1483 #ifdef DIAGNOSTIC
1484 	panic("fdioctl: impossible");
1485 #endif
1486 }
1487 
1488 void
1489 fd_do_eject(unit)
1490 	int unit;
1491 {
1492 	infdc.drvstat = 0x20 | ( 1 << unit);
1493 	DELAY(1); /* XXX */
1494 	infdc.drvstat = 0x20;
1495 }
1496 
1497 /*
1498  * Build disk label. For now we only create a label from what we know
1499  * from 'sc'.
1500  */
1501 static int
1502 fdgetdisklabel(sc, dev)
1503 	struct fd_softc *sc;
1504 	dev_t dev;
1505 {
1506 	struct disklabel *lp;
1507 	int part;
1508 
1509 #ifdef FDDEBUG
1510 	printf("fdgetdisklabel()\n");
1511 #endif
1512 
1513 	part = DISKPART(dev);
1514 	lp = sc->sc_dk.dk_label;
1515 	bzero(lp, sizeof(struct disklabel));
1516 
1517 	lp->d_secsize     = 128 << sc->sc_type->secsize;
1518 	lp->d_ntracks     = sc->sc_type->heads;
1519 	lp->d_nsectors    = sc->sc_type->sectrac;
1520 	lp->d_secpercyl   = lp->d_ntracks * lp->d_nsectors;
1521 	lp->d_ncylinders  = sc->sc_type->size / lp->d_secpercyl;
1522 	lp->d_secperunit  = sc->sc_type->size;
1523 
1524 	lp->d_type        = DTYPE_FLOPPY;
1525 	lp->d_rpm         = 300; 	/* XXX */
1526 	lp->d_interleave  = 1;		/* FIXME: is this OK?		*/
1527 	lp->d_bbsize      = 0;
1528 	lp->d_sbsize      = 0;
1529 	lp->d_npartitions = part + 1;
1530 #define STEP_DELAY	6000	/* 6ms (6000us) delay after stepping	*/
1531 	lp->d_trkseek     = STEP_DELAY; /* XXX */
1532 	lp->d_magic       = DISKMAGIC;
1533 	lp->d_magic2      = DISKMAGIC;
1534 	lp->d_checksum    = dkcksum(lp);
1535 	lp->d_partitions[part].p_size   = lp->d_secperunit;
1536 	lp->d_partitions[part].p_fstype = FS_UNUSED;
1537 	lp->d_partitions[part].p_fsize  = 1024;
1538 	lp->d_partitions[part].p_frag   = 8;
1539 
1540 	return(0);
1541 }
1542 
1543 /* ARGSUSED */
1544 void
1545 fd_mountroot_hook(dev)
1546 	struct device *dev;
1547 {
1548 	int c;
1549 
1550 	fd_do_eject(dev->dv_unit);
1551 	printf("Insert filesystem floppy and press return.");
1552 	for (;;) {
1553 		c = cngetc();
1554 		if ((c == '\r') || (c == '\n')) {
1555 			printf("\n");
1556 			return;
1557 		}
1558 	}
1559 }
1560