xref: /openbsd/sys/dev/isa/fd.c (revision 0f9e9ec2)
1 /*	$OpenBSD: fd.c,v 1.109 2024/05/13 01:15:50 jsg Exp $	*/
2 /*	$NetBSD: fd.c,v 1.90 1996/05/12 23:12:03 mycroft Exp $	*/
3 
4 /*-
5  * Copyright (c) 1993, 1994, 1995, 1996 Charles Hannum.
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  * Portions Copyright (c) 1993, 1994 by
13  *  jc@irbs.UUCP (John Capo)
14  *  vak@zebub.msk.su (Serge Vakulenko)
15  *  ache@astral.msk.su (Andrew A. Chernov)
16  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/fcntl.h>
49 #include <sys/ioctl.h>
50 #include <sys/device.h>
51 #include <sys/disklabel.h>
52 #include <sys/disk.h>
53 #include <sys/buf.h>
54 #include <sys/malloc.h>
55 #include <sys/uio.h>
56 #include <sys/mtio.h>
57 #include <sys/syslog.h>
58 #include <sys/queue.h>
59 #include <sys/stat.h>
60 #include <sys/timeout.h>
61 #include <sys/dkio.h>
62 
63 #include <machine/cpu.h>
64 #include <machine/bus.h>
65 #include <machine/intr.h>
66 #include <machine/ioctl_fd.h>
67 
68 #include <dev/isa/isavar.h>
69 #include <dev/isa/isadmavar.h>
70 #include <dev/isa/fdreg.h>
71 
72 #if defined(__i386__) || defined(__amd64__)	/* XXX */
73 #include <i386/isa/nvram.h>
74 #endif
75 
76 #include <dev/isa/fdlink.h>
77 
78 /* XXX misuse a flag to identify format operation */
79 #define B_FORMAT B_XXX
80 
81 /* fd_type struct now in ioctl_fd.h */
82 
83 /* The order of entries in the following table is important -- BEWARE! */
84 struct fd_type fd_types[] = {
85         { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    }, /* 1.44MB diskette */
86         { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB"    }, /* 1.2 MB AT-diskettes */
87         {  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" }, /* 360kB in 1.2MB drive */
88         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" }, /* 360kB PC diskettes */
89         {  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB"    }, /* 3.5" 720kB diskette */
90         {  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x"  }, /* 720kB in 1.2MB drive */
91         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x"  }, /* 360kB in 720kB drive */
92 	{ 36,2,72,2,0xff,0xaf,0x1b,0x54,80,5760,1,FDC_500KBPS,"2.88MB"    },  /* 2.88MB diskette */
93 	{  8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,"1.2MB/[1024bytes/sector]" }	/* 1.2 MB japanese format */
94 };
95 
96 /* software state, per disk (with up to 4 disks per ctlr) */
97 struct fd_softc {
98 	struct device sc_dev;
99 	struct disk sc_dk;
100 
101 	struct fd_type *sc_deftype;	/* default type descriptor */
102 	struct fd_type *sc_type;	/* current type descriptor */
103 
104 	daddr_t	sc_blkno;	/* starting block number */
105 	int sc_bcount;		/* byte count left */
106  	int sc_opts;			/* user-set options */
107 	int sc_skip;		/* bytes already transferred */
108 	int sc_nblks;		/* number of blocks currently transferring */
109 	int sc_nbytes;		/* number of bytes currently transferring */
110 
111 	int sc_drive;		/* physical unit number */
112 	int sc_flags;
113 #define	FD_OPEN		0x01		/* it's open */
114 #define	FD_MOTOR	0x02		/* motor should be on */
115 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
116 	int sc_cylin;		/* where we think the head is */
117 
118 	TAILQ_ENTRY(fd_softc) sc_drivechain;
119 	int sc_ops;		/* I/O ops since last switch */
120 	struct bufq sc_bufq;	/* pending I/O */
121 	struct buf *sc_bp;	/* the current I/O */
122 	struct timeout fd_motor_on_to;
123 	struct timeout fd_motor_off_to;
124 	struct timeout fdtimeout_to;
125 };
126 
127 /* floppy driver configuration */
128 int fdprobe(struct device *, void *, void *);
129 void fdattach(struct device *, struct device *, void *);
130 int fdactivate(struct device *, int);
131 
132 const struct cfattach fd_ca = {
133 	sizeof(struct fd_softc), fdprobe, fdattach, NULL, fdactivate
134 };
135 
136 struct cfdriver fd_cd = {
137 	NULL, "fd", DV_DISK
138 };
139 
140 int fdgetdisklabel(dev_t, struct fd_softc *, struct disklabel *, int);
141 void fdstrategy(struct buf *);
142 void fdstart(struct fd_softc *);
143 int fdintr(struct fdc_softc *);
144 
145 void fd_set_motor(struct fdc_softc *fdc, int reset);
146 void fd_motor_off(void *arg);
147 void fd_motor_on(void *arg);
148 void fdfinish(struct fd_softc *fd, struct buf *bp);
149 int fdformat(dev_t, struct fd_formb *, struct proc *);
150 static __inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
151 void fdretry(struct fd_softc *);
152 void fdtimeout(void *);
153 
154 int
fdgetdisklabel(dev_t dev,struct fd_softc * fd,struct disklabel * lp,int spoofonly)155 fdgetdisklabel(dev_t dev, struct fd_softc *fd, struct disklabel *lp,
156     int spoofonly)
157 {
158 	bzero(lp, sizeof(struct disklabel));
159 
160 	lp->d_type = DTYPE_FLOPPY;
161 	lp->d_secsize = FD_BSIZE(fd);
162 	lp->d_secpercyl = fd->sc_type->seccyl;
163 	lp->d_nsectors = fd->sc_type->sectrac;
164 	lp->d_ncylinders = fd->sc_type->tracks;
165 	lp->d_ntracks = fd->sc_type->heads;	/* Go figure... */
166 	DL_SETDSIZE(lp, fd->sc_type->size);
167 
168 	strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
169 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
170 	lp->d_version = 1;
171 
172 	lp->d_magic = DISKMAGIC;
173 	lp->d_magic2 = DISKMAGIC;
174 	lp->d_checksum = dkcksum(lp);
175 
176 	/*
177 	 * Call the generic disklabel extraction routine.  If there's
178 	 * not a label there, fake it.
179 	 */
180 	return readdisklabel(DISKLABELDEV(dev), fdstrategy, lp, spoofonly);
181 }
182 
183 int
fdprobe(struct device * parent,void * match,void * aux)184 fdprobe(struct device *parent, void *match, void *aux)
185 {
186 	struct fdc_softc *fdc = (void *)parent;
187 	struct cfdata *cf = match;
188 	struct fdc_attach_args *fa = aux;
189 	int drive = fa->fa_drive;
190 	bus_space_tag_t iot = fdc->sc_iot;
191 	bus_space_handle_t ioh = fdc->sc_ioh;
192 	int n;
193 
194 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive)
195 		return 0;
196 	/*
197 	 * XXX
198 	 * This is to work around some odd interactions between this driver
199 	 * and SMC Ethernet cards.
200 	 */
201 	if (cf->cf_loc[0] == -1 && drive >= 2)
202 		return 0;
203 
204 	/*
205 	 * We want to keep the flags config gave us.
206 	 */
207 	fa->fa_flags = cf->cf_flags;
208 
209 	/* select drive and turn on motor */
210 	bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
211 	/* wait for motor to spin up */
212 	tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(250));
213 	out_fdc(iot, ioh, NE7CMD_RECAL);
214 	out_fdc(iot, ioh, drive);
215 	/* wait for recalibrate */
216 	tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(2000));
217 	out_fdc(iot, ioh, NE7CMD_SENSEI);
218 	n = fdcresult(fdc);
219 #ifdef FD_DEBUG
220 	{
221 		int i;
222 		printf("fdprobe: status");
223 		for (i = 0; i < n; i++)
224 			printf(" %x", fdc->sc_status[i]);
225 		printf("\n");
226 	}
227 #endif
228 
229 	/* turn off motor */
230 	tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(250));
231 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
232 
233 	/* flags & 0x20 forces the drive to be found even if it won't probe */
234 	if (!(fa->fa_flags & 0x20) && (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20))
235 		return 0;
236 
237 	return 1;
238 }
239 
240 /*
241  * Controller is working, and drive responded.  Attach it.
242  */
243 void
fdattach(struct device * parent,struct device * self,void * aux)244 fdattach(struct device *parent, struct device *self, void *aux)
245 {
246 	struct fdc_softc *fdc = (void *)parent;
247 	struct fd_softc *fd = (void *)self;
248 	struct fdc_attach_args *fa = aux;
249 	struct fd_type *type = fa->fa_deftype;
250 	int drive = fa->fa_drive;
251 
252 	if (!type || (fa->fa_flags & 0x10)) {
253 		/* The config has overridden this. */
254 		switch (fa->fa_flags & 0x07) {
255 		case 1:	/* 2.88MB */
256 			type = &fd_types[7];
257 			break;
258 		case 2:	/* 1.44MB */
259 			type = &fd_types[0];
260 			break;
261 		case 3: /* 1.2MB */
262 			type = &fd_types[1];
263 			break;
264 		case 4: /* 720K */
265 			type = &fd_types[4];
266 			break;
267 		case 5: /* 360K */
268 			type = &fd_types[3];
269 			break;
270 		case 6:	/* 1.2 MB japanese format */
271 			type = &fd_types[8];
272 			break;
273 #ifdef __alpha__
274 		default:
275 			/* 1.44MB, how to detect others?
276 			 * idea from NetBSD -- jay@rootaction.net
277                          */
278 			type = &fd_types[0];
279 #endif
280 		}
281 	}
282 
283 	if (type)
284 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
285 		    type->tracks, type->heads, type->sectrac);
286 	else
287 		printf(": density unknown\n");
288 
289 	fd->sc_cylin = -1;
290 	fd->sc_drive = drive;
291 	fd->sc_deftype = type;
292 	fdc->sc_type[drive] = FDC_TYPE_DISK;
293 	fdc->sc_link.fdlink.sc_fd[drive] = fd;
294 
295 	/*
296 	 * Initialize and attach the disk structure.
297 	 */
298 	fd->sc_dk.dk_flags = DKF_NOLABELREAD;
299 	fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
300 	bufq_init(&fd->sc_bufq, BUFQ_DEFAULT);
301 	disk_attach(&fd->sc_dev, &fd->sc_dk);
302 
303 	/* Setup timeout structures */
304 	timeout_set(&fd->fd_motor_on_to, fd_motor_on, fd);
305 	timeout_set(&fd->fd_motor_off_to, fd_motor_off, fd);
306 	timeout_set(&fd->fdtimeout_to, fdtimeout, fd);
307 }
308 
309 int
fdactivate(struct device * self,int act)310 fdactivate(struct device *self, int act)
311 {
312 	struct fd_softc *fd = (void *)self;
313 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
314 	int rv = 0;
315 
316 	switch (act) {
317 	case DVACT_SUSPEND:
318 		if (fdc->sc_state != DEVIDLE) {
319 			timeout_del(&fd->fd_motor_on_to);
320 			timeout_del(&fd->fd_motor_off_to);
321 			timeout_del(&fd->fdtimeout_to);
322 			fdc->sc_state = IOTIMEDOUT;
323 			fdc->sc_errors = 4;
324 		}
325 		break;
326 	case DVACT_POWERDOWN:
327 		fd_motor_off(self);
328 		break;
329 	}
330 
331 	return (rv);
332 }
333 
334 /*
335  * Translate nvram type into internal data structure.  Return NULL for
336  * none/unknown/unusable.
337  */
338 struct fd_type *
fd_nvtotype(char * fdc,int nvraminfo,int drive)339 fd_nvtotype(char *fdc, int nvraminfo, int drive)
340 {
341 #ifdef __alpha__
342 	/* Alpha:  assume 1.44MB, idea from NetBSD sys/dev/isa/fd.c
343 	 * -- jay@rootaction.net
344 	 */
345 	return &fd_types[0]; /* 1.44MB */
346 #else
347 	int type;
348 
349 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
350 	switch (type) {
351 	case NVRAM_DISKETTE_NONE:
352 		return NULL;
353 	case NVRAM_DISKETTE_12M:
354 		return &fd_types[1];
355 	case NVRAM_DISKETTE_TYPE5:
356 	case NVRAM_DISKETTE_TYPE6:
357 		return &fd_types[7];
358 	case NVRAM_DISKETTE_144M:
359 		return &fd_types[0];
360 	case NVRAM_DISKETTE_360K:
361 		return &fd_types[3];
362 	case NVRAM_DISKETTE_720K:
363 		return &fd_types[4];
364 	default:
365 		printf("%s: drive %d: unknown device type 0x%x\n",
366 		    fdc, drive, type);
367 		return NULL;
368 	}
369 #endif
370 }
371 
372 static __inline struct fd_type *
fd_dev_to_type(struct fd_softc * fd,dev_t dev)373 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
374 {
375 	int type = FDTYPE(dev);
376 
377 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
378 		return NULL;
379 	return type ? &fd_types[type - 1] : fd->sc_deftype;
380 }
381 
382 void
fdstrategy(struct buf * bp)383 fdstrategy(struct buf *bp)
384 {
385 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(bp->b_dev)];
386 	int sz;
387  	int s;
388 	int fd_bsize = FD_BSIZE(fd);
389 	int bf = fd_bsize / DEV_BSIZE;
390 
391 	/* Valid unit, controller, and request? */
392 	if (bp->b_blkno < 0 ||
393 	    (((bp->b_blkno % bf) != 0 ||
394 	      (bp->b_bcount % fd_bsize) != 0) &&
395 	     (bp->b_flags & B_FORMAT) == 0)) {
396 		bp->b_error = EINVAL;
397 		goto bad;
398 	}
399 
400 	/* If it's a null transfer, return immediately. */
401 	if (bp->b_bcount == 0)
402 		goto done;
403 
404 	sz = howmany(bp->b_bcount, DEV_BSIZE);
405 
406 	if (bp->b_blkno + sz > fd->sc_type->size * bf) {
407 		sz = fd->sc_type->size * bf - bp->b_blkno;
408 		if (sz == 0)
409 			/* If exactly at end of disk, return EOF. */
410 			goto done;
411 		if (sz < 0) {
412 			/* If past end of disk, return EINVAL. */
413 			bp->b_error = EINVAL;
414 			goto bad;
415 		}
416 		/* Otherwise, truncate request. */
417 		bp->b_bcount = sz << DEV_BSHIFT;
418 	}
419 
420 	bp->b_resid = bp->b_bcount;
421 
422 #ifdef FD_DEBUG
423 	printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld sz %d\n",
424 	    (long long)bp->b_blkno, bp->b_bcount,
425 	    (long long)fd->sc_blkno, sz);
426 #endif
427 
428 	/* Queue I/O */
429 	bufq_queue(&fd->sc_bufq, bp);
430 
431 	/* Queue transfer on drive, activate drive and controller if idle. */
432 	s = splbio();
433 	timeout_del(&fd->fd_motor_off_to); /* a good idea */
434 	if (fd->sc_bp == NULL)
435 		fdstart(fd);
436 #ifdef DIAGNOSTIC
437 	else {
438 		struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
439 		if (fdc->sc_state == DEVIDLE) {
440 			printf("fdstrategy: controller inactive\n");
441 			fdcstart(fdc);
442 		}
443 	}
444 #endif
445 	splx(s);
446 	return;
447 
448 bad:
449 	bp->b_flags |= B_ERROR;
450 done:
451 	/* Toss transfer; we're done early. */
452 	bp->b_resid = bp->b_bcount;
453 	s = splbio();
454 	biodone(bp);
455 	splx(s);
456 }
457 
458 void
fdstart(struct fd_softc * fd)459 fdstart(struct fd_softc *fd)
460 {
461 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
462 	int active = !TAILQ_EMPTY(&fdc->sc_link.fdlink.sc_drives);
463 
464 	/* Link into controller queue. */
465 	fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
466 	TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
467 
468 	/* If controller not already active, start it. */
469 	if (!active)
470 		fdcstart(fdc);
471 }
472 
473 void
fdfinish(struct fd_softc * fd,struct buf * bp)474 fdfinish(struct fd_softc *fd, struct buf *bp)
475 {
476 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
477 
478 	splassert(IPL_BIO);
479 
480 	fd->sc_skip = 0;
481 	fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
482 
483 	/*
484 	 * Move this drive to the end of the queue to give others a `fair'
485 	 * chance.  We only force a switch if N operations are completed while
486 	 * another drive is waiting to be serviced, since there is a long motor
487 	 * startup delay whenever we switch.
488 	 */
489 	if (TAILQ_NEXT(fd, sc_drivechain) != NULL && ++fd->sc_ops >= 8) {
490 		fd->sc_ops = 0;
491 		TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
492 		if (fd->sc_bp != NULL) {
493 			TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd,
494 					  sc_drivechain);
495 		}
496 	}
497 
498 	biodone(bp);
499 	/* turn off motor 5s from now */
500 	timeout_add_sec(&fd->fd_motor_off_to, 5);
501 	fdc->sc_state = DEVIDLE;
502 }
503 
504 int
fdread(dev_t dev,struct uio * uio,int flags)505 fdread(dev_t dev, struct uio *uio, int flags)
506 {
507 	return (physio(fdstrategy, dev, B_READ, minphys, uio));
508 }
509 
510 int
fdwrite(dev_t dev,struct uio * uio,int flags)511 fdwrite(dev_t dev, struct uio *uio, int flags)
512 {
513 	return (physio(fdstrategy, dev, B_WRITE, minphys, uio));
514 }
515 
516 void
fd_set_motor(struct fdc_softc * fdc,int reset)517 fd_set_motor(struct fdc_softc *fdc, int reset)
518 {
519 	struct fd_softc *fd;
520 	u_char status;
521 	int n;
522 
523 	if ((fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives)) != NULL)
524 		status = fd->sc_drive;
525 	else
526 		status = 0;
527 	if (!reset)
528 		status |= FDO_FRST | FDO_FDMAEN;
529 	for (n = 0; n < 4; n++)
530 		if ((fd = fdc->sc_link.fdlink.sc_fd[n])
531 		    && (fd->sc_flags & FD_MOTOR))
532 			status |= FDO_MOEN(n);
533 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
534 }
535 
536 void
fd_motor_off(void * arg)537 fd_motor_off(void *arg)
538 {
539 	struct fd_softc *fd = arg;
540 	int s;
541 
542 	s = splbio();
543 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
544 	fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
545 	splx(s);
546 }
547 
548 void
fd_motor_on(void * arg)549 fd_motor_on(void *arg)
550 {
551 	struct fd_softc *fd = arg;
552 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
553 	int s;
554 
555 	s = splbio();
556 	fd->sc_flags &= ~FD_MOTOR_WAIT;
557 	if ((TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives) == fd)
558 	    && (fdc->sc_state == MOTORWAIT))
559 		(void) fdintr(fdc);
560 	splx(s);
561 }
562 
563 int
fdopen(dev_t dev,int flags,int fmt,struct proc * p)564 fdopen(dev_t dev, int flags, int fmt, struct proc *p)
565 {
566  	int unit, pmask;
567 	struct fd_softc *fd;
568 	struct fd_type *type;
569 
570 	unit = FDUNIT(dev);
571 	if (unit >= fd_cd.cd_ndevs)
572 		return ENXIO;
573 	fd = fd_cd.cd_devs[unit];
574 	if (fd == 0)
575 		return ENXIO;
576 	type = fd_dev_to_type(fd, dev);
577 	if (type == NULL)
578 		return ENXIO;
579 
580 	if ((fd->sc_flags & FD_OPEN) != 0 &&
581 	    fd->sc_type != type)
582 		return EBUSY;
583 
584 	fd->sc_type = type;
585 	fd->sc_cylin = -1;
586 	fd->sc_flags |= FD_OPEN;
587 
588 	/*
589 	 * Only update the disklabel if we're not open anywhere else.
590 	 */
591 	if (fd->sc_dk.dk_openmask == 0)
592 		fdgetdisklabel(dev, fd, fd->sc_dk.dk_label, 0);
593 
594 	pmask = (1 << FDPART(dev));
595 
596 	switch (fmt) {
597 	case S_IFCHR:
598 		fd->sc_dk.dk_copenmask |= pmask;
599 		break;
600 
601 	case S_IFBLK:
602 		fd->sc_dk.dk_bopenmask |= pmask;
603 		break;
604 	}
605 	fd->sc_dk.dk_openmask =
606 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
607 
608 	return 0;
609 }
610 
611 int
fdclose(dev_t dev,int flags,int fmt,struct proc * p)612 fdclose(dev_t dev, int flags, int fmt, struct proc *p)
613 {
614 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
615 	int pmask = (1 << FDPART(dev));
616 
617 	fd->sc_flags &= ~FD_OPEN;
618 	fd->sc_opts &= ~FDOPT_NORETRY;
619 
620 	switch (fmt) {
621 	case S_IFCHR:
622 		fd->sc_dk.dk_copenmask &= ~pmask;
623 		break;
624 
625 	case S_IFBLK:
626 		fd->sc_dk.dk_bopenmask &= ~pmask;
627 		break;
628 	}
629 	fd->sc_dk.dk_openmask =
630 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
631 
632 	return (0);
633 }
634 
635 daddr_t
fdsize(dev_t dev)636 fdsize(dev_t dev)
637 {
638 	/* Swapping to floppies would not make sense. */
639 	return -1;
640 }
641 
642 int
fddump(dev_t dev,daddr_t blkno,caddr_t va,size_t size)643 fddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
644 {
645 	/* Not implemented. */
646 	return ENXIO;
647 }
648 
649 /*
650  * Called from the controller.
651  */
652 int
fdintr(struct fdc_softc * fdc)653 fdintr(struct fdc_softc *fdc)
654 {
655 #define	st0	fdc->sc_status[0]
656 #define	cyl	fdc->sc_status[1]
657 	struct fd_softc *fd;
658 	struct buf *bp;
659 	bus_space_tag_t iot = fdc->sc_iot;
660 	bus_space_handle_t ioh = fdc->sc_ioh;
661 	bus_space_handle_t ioh_ctl = fdc->sc_ioh_ctl;
662 	int read, head, sec, i, nblks, cylin;
663 	struct fd_type *type;
664 	struct fd_formb *finfo = NULL;
665 	int fd_bsize;
666 
667 loop:
668 	/* Is there a transfer to this drive?  If not, deactivate drive. */
669 	fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives);
670 	if (fd == NULL) {
671 		fdc->sc_state = DEVIDLE;
672 		return 1;
673 	}
674 	fd_bsize = FD_BSIZE(fd);
675 
676 	bp = fd->sc_bp;
677 	if (bp == NULL) {
678 		fd->sc_ops = 0;
679 		TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
680 		goto loop;
681 	}
682 
683 	if (bp->b_flags & B_FORMAT)
684 	    finfo = (struct fd_formb *)bp->b_data;
685 
686 	cylin = ((bp->b_blkno * DEV_BSIZE) + (bp->b_bcount - bp->b_resid)) /
687 	    (fd_bsize * fd->sc_type->seccyl);
688 
689 	switch (fdc->sc_state) {
690 	case DEVIDLE:
691 		fdc->sc_errors = 0;
692 		fd->sc_skip = 0;
693 		fd->sc_bcount = bp->b_bcount;
694 		fd->sc_blkno = bp->b_blkno / (fd_bsize / DEV_BSIZE);
695 		timeout_del(&fd->fd_motor_off_to);
696 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
697 			fdc->sc_state = MOTORWAIT;
698 			return 1;
699 		}
700 		if ((fd->sc_flags & FD_MOTOR) == 0) {
701 			/* Turn on the motor, being careful about pairing. */
702 			struct fd_softc *ofd =
703 				fdc->sc_link.fdlink.sc_fd[fd->sc_drive ^ 1];
704 			if (ofd && ofd->sc_flags & FD_MOTOR) {
705 				timeout_del(&ofd->fd_motor_off_to);
706 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
707 			}
708 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
709 			fd_set_motor(fdc, 0);
710 			fdc->sc_state = MOTORWAIT;
711 			/* Allow .25s for motor to stabilize. */
712 			timeout_add_msec(&fd->fd_motor_on_to, 250);
713 			return 1;
714 		}
715 		/* Make sure the right drive is selected. */
716 		fd_set_motor(fdc, 0);
717 
718 		/* FALLTHROUGH */
719 	case DOSEEK:
720 	doseek:
721 		if (fd->sc_cylin == cylin)
722 			goto doio;
723 
724 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
725 		out_fdc(iot, ioh, fd->sc_type->steprate);
726 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
727 
728 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
729 		out_fdc(iot, ioh, fd->sc_drive);	/* drive number */
730 		out_fdc(iot, ioh, cylin * fd->sc_type->step);
731 
732 		fd->sc_cylin = -1;
733 		fdc->sc_state = SEEKWAIT;
734 
735 		fd->sc_dk.dk_seek++;
736 		disk_busy(&fd->sc_dk);
737 
738 		timeout_add_sec(&fd->fdtimeout_to, 4);
739 		return 1;
740 
741 	case DOIO:
742 	doio:
743 		type = fd->sc_type;
744 		if (finfo)
745 		    fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
746 			(char *)finfo;
747 		sec = fd->sc_blkno % type->seccyl;
748 		nblks = type->seccyl - sec;
749 		nblks = min(nblks, fd->sc_bcount / fd_bsize);
750 		nblks = min(nblks, FDC_MAXIOSIZE / fd_bsize);
751 		fd->sc_nblks = nblks;
752 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * fd_bsize;
753 		head = sec / type->sectrac;
754 		sec -= head * type->sectrac;
755 #ifdef DIAGNOSTIC
756 		{int block;
757 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
758 		 if (block != fd->sc_blkno) {
759 			 panic("fdintr: block %d != blkno %llu", block, fd->sc_blkno);
760 		 }}
761 #endif
762 		read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
763 		isadma_start(bp->b_data + fd->sc_skip, fd->sc_nbytes,
764 		    fdc->sc_drq, read);
765 		bus_space_write_1(iot, ioh_ctl, fdctl, type->rate);
766 #ifdef FD_DEBUG
767 		printf("fdintr: %s drive %d track %d head %d sec %d nblks %d\n",
768 		    read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
769 		    sec, nblks);
770 #endif
771 		if (finfo) {
772                         /* formatting */
773 			if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
774 			    fdc->sc_errors = 4;
775 			    fdretry(fd);
776 			    goto loop;
777 			}
778                         out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
779                         out_fdc(iot, ioh, finfo->fd_formb_secshift);
780                         out_fdc(iot, ioh, finfo->fd_formb_nsecs);
781                         out_fdc(iot, ioh, finfo->fd_formb_gaplen);
782                         out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
783 		} else {
784 			if (read)
785 				out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
786 			else
787 				out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
788 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
789 			out_fdc(iot, ioh, fd->sc_cylin);	/* track */
790 			out_fdc(iot, ioh, head);
791 			out_fdc(iot, ioh, sec + 1);		/* sec +1 */
792 			out_fdc(iot, ioh, type->secsize);	/* sec size */
793 			out_fdc(iot, ioh, type->sectrac);	/* secs/track */
794 			out_fdc(iot, ioh, type->gap1);		/* gap1 size */
795 			out_fdc(iot, ioh, type->datalen);	/* data len */
796 		}
797 		fdc->sc_state = IOCOMPLETE;
798 
799 		disk_busy(&fd->sc_dk);
800 
801 		/* allow 2 seconds for operation */
802 		timeout_add_sec(&fd->fdtimeout_to, 2);
803 		return 1;				/* will return later */
804 
805 	case SEEKWAIT:
806 		timeout_del(&fd->fdtimeout_to);
807 		fdc->sc_state = SEEKCOMPLETE;
808 		/* allow 1/50 second for heads to settle */
809 		timeout_add_msec(&fdc->fdcpseudointr_to, 20);
810 		return 1;
811 
812 	case SEEKCOMPLETE:
813 		disk_unbusy(&fd->sc_dk, 0, 0, 0);	/* no data on seek */
814 
815 		/* Make sure seek really happened. */
816 		out_fdc(iot, ioh, NE7CMD_SENSEI);
817 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
818 		    cyl != cylin * fd->sc_type->step) {
819 #ifdef FD_DEBUG
820 			fdcstatus(&fd->sc_dev, 2, "seek failed");
821 #endif
822 			fdretry(fd);
823 			goto loop;
824 		}
825 		fd->sc_cylin = cylin;
826 		goto doio;
827 
828 	case IOTIMEDOUT:
829 		isadma_abort(fdc->sc_drq);
830 	case SEEKTIMEDOUT:
831 	case RECALTIMEDOUT:
832 	case RESETTIMEDOUT:
833 		fdretry(fd);
834 		goto loop;
835 
836 	case IOCOMPLETE: /* IO DONE, post-analyze */
837 		timeout_del(&fd->fdtimeout_to);
838 
839 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
840 		    fd->sc_blkno, (bp->b_flags & B_READ));
841 
842 		if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
843 			isadma_abort(fdc->sc_drq);
844 #ifdef FD_DEBUG
845 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
846 			    "read failed" : "write failed");
847 			printf("blkno %lld nblks %d\n",
848 			    (long long)fd->sc_blkno, fd->sc_nblks);
849 #endif
850 			fdretry(fd);
851 			goto loop;
852 		}
853 		read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
854 		isadma_done(fdc->sc_drq);
855 		if (fdc->sc_errors) {
856 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
857 			    fd->sc_skip / fd_bsize, (struct disklabel *)NULL);
858 			printf("\n");
859 			fdc->sc_errors = 0;
860 		}
861 
862 		fd->sc_blkno += fd->sc_nblks;
863 		fd->sc_skip += fd->sc_nbytes;
864 		fd->sc_bcount -= fd->sc_nbytes;
865 		bp->b_resid -= fd->sc_nbytes;
866 		if (!finfo && fd->sc_bcount > 0) {
867 			cylin = fd->sc_blkno / fd->sc_type->seccyl;
868 			goto doseek;
869 		}
870 		fdfinish(fd, bp);
871 		goto loop;
872 
873 	case DORESET:
874 		/* try a reset, keep motor on */
875 		fd_set_motor(fdc, 1);
876 		delay(100);
877 		fd_set_motor(fdc, 0);
878 		fdc->sc_state = RESETCOMPLETE;
879 		timeout_add_msec(&fd->fdtimeout_to, 500);
880 		return 1;			/* will return later */
881 
882 	case RESETCOMPLETE:
883 		timeout_del(&fd->fdtimeout_to);
884 		/* clear the controller output buffer */
885 		for (i = 0; i < 4; i++) {
886 			out_fdc(iot, ioh, NE7CMD_SENSEI);
887 			(void) fdcresult(fdc);
888 		}
889 
890 		/* FALLTHROUGH */
891 	case DORECAL:
892 		out_fdc(iot, ioh, NE7CMD_RECAL);	/* recal function */
893 		out_fdc(iot, ioh, fd->sc_drive);
894 		fdc->sc_state = RECALWAIT;
895 		timeout_add_sec(&fd->fdtimeout_to, 5);
896 		return 1;			/* will return later */
897 
898 	case RECALWAIT:
899 		timeout_del(&fd->fdtimeout_to);
900 		fdc->sc_state = RECALCOMPLETE;
901 		/* allow 1/30 second for heads to settle */
902 		timeout_add_msec(&fdc->fdcpseudointr_to, 1000 / 30);
903 		return 1;			/* will return later */
904 
905 	case RECALCOMPLETE:
906 		out_fdc(iot, ioh, NE7CMD_SENSEI);
907 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
908 #ifdef FD_DEBUG
909 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
910 #endif
911 			fdretry(fd);
912 			goto loop;
913 		}
914 		fd->sc_cylin = 0;
915 		goto doseek;
916 
917 	case MOTORWAIT:
918 		if (fd->sc_flags & FD_MOTOR_WAIT)
919 			return 1;		/* time's not up yet */
920 		goto doseek;
921 
922 	default:
923 		fdcstatus(&fd->sc_dev, 0, "stray interrupt");
924 		return 1;
925 	}
926 #ifdef DIAGNOSTIC
927 	panic("fdintr: impossible");
928 #endif
929 #undef	st0
930 #undef	cyl
931 }
932 
933 void
fdtimeout(void * arg)934 fdtimeout(void *arg)
935 {
936 	struct fd_softc *fd = arg;
937 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
938 	int s;
939 
940 	s = splbio();
941 #ifdef DEBUG
942 	log(LOG_ERR,"fdtimeout: state %d\n", fdc->sc_state);
943 #endif
944 	fdcstatus(&fd->sc_dev, 0, "timeout");
945 
946 	if (fd->sc_bp != NULL)
947 		fdc->sc_state++;
948 	else
949 		fdc->sc_state = DEVIDLE;
950 
951 	(void) fdintr(fdc);
952 	splx(s);
953 }
954 
955 void
fdretry(struct fd_softc * fd)956 fdretry(struct fd_softc *fd)
957 {
958 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
959 	struct buf *bp = fd->sc_bp;
960 
961 	if (fd->sc_opts & FDOPT_NORETRY)
962 	    goto fail;
963 	switch (fdc->sc_errors) {
964 	case 0:
965 		/* try again */
966 		fdc->sc_state = DOSEEK;
967 		break;
968 
969 	case 1: case 2: case 3:
970 		/* didn't work; try recalibrating */
971 		fdc->sc_state = DORECAL;
972 		break;
973 
974 	case 4:
975 		/* still no go; reset the bastard */
976 		fdc->sc_state = DORESET;
977 		break;
978 
979 	default:
980 	fail:
981 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
982 		    fd->sc_skip / FD_BSIZE(fd), (struct disklabel *)NULL);
983 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
984 		    fdc->sc_status[0], NE7_ST0BITS,
985 		    fdc->sc_status[1], NE7_ST1BITS,
986 		    fdc->sc_status[2], NE7_ST2BITS,
987 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
988 
989 		bp->b_flags |= B_ERROR;
990 		bp->b_error = EIO;
991 		bp->b_resid = bp->b_bcount;
992 		fdfinish(fd, bp);
993 	}
994 	fdc->sc_errors++;
995 }
996 
997 int
fdioctl(dev_t dev,u_long cmd,caddr_t addr,int flag,struct proc * p)998 fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
999 {
1000 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1001 	struct disklabel *lp;
1002 	int error;
1003 
1004 	switch (cmd) {
1005 	case MTIOCTOP:
1006 		if (((struct mtop *)addr)->mt_op != MTOFFL)
1007 			return EIO;
1008 		return (0);
1009 
1010 	case DIOCRLDINFO:
1011 		lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
1012 		fdgetdisklabel(dev, fd, lp, 0);
1013 		bcopy(lp, fd->sc_dk.dk_label, sizeof(*lp));
1014 		free(lp, M_TEMP, sizeof(*lp));
1015 		return 0;
1016 
1017 	case DIOCGPDINFO:
1018 		fdgetdisklabel(dev, fd, (struct disklabel *)addr, 1);
1019 		return 0;
1020 
1021 	case DIOCGDINFO:
1022 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1023 		return 0;
1024 
1025 	case DIOCGPART:
1026 		((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
1027 		((struct partinfo *)addr)->part =
1028 		    &fd->sc_dk.dk_label->d_partitions[FDPART(dev)];
1029 		return 0;
1030 
1031 	case DIOCWDINFO:
1032 	case DIOCSDINFO:
1033 		if ((flag & FWRITE) == 0)
1034 			return EBADF;
1035 
1036 		error = setdisklabel(fd->sc_dk.dk_label,
1037 		    (struct disklabel *)addr, 0);
1038 		if (error == 0) {
1039 			if (cmd == DIOCWDINFO)
1040 				error = writedisklabel(DISKLABELDEV(dev),
1041 				    fdstrategy, fd->sc_dk.dk_label);
1042 		}
1043 		return error;
1044 
1045         case FD_FORM:
1046                 if((flag & FWRITE) == 0)
1047                         return EBADF;  /* must be opened for writing */
1048                 else if(((struct fd_formb *)addr)->format_version !=
1049                         FD_FORMAT_VERSION)
1050                         return EINVAL; /* wrong version of formatting prog */
1051                 else
1052                         return fdformat(dev, (struct fd_formb *)addr, p);
1053                 break;
1054 
1055         case FD_GTYPE:                  /* get drive type */
1056                 *(struct fd_type *)addr = *fd->sc_type;
1057 		return 0;
1058 
1059         case FD_GOPTS:                  /* get drive options */
1060                 *(int *)addr = fd->sc_opts;
1061                 return 0;
1062 
1063         case FD_SOPTS:                  /* set drive options */
1064                 fd->sc_opts = *(int *)addr;
1065 		return 0;
1066 
1067 	default:
1068 		return ENOTTY;
1069 	}
1070 
1071 #ifdef DIAGNOSTIC
1072 	panic("fdioctl: impossible");
1073 #endif
1074 }
1075 
1076 int
fdformat(dev_t dev,struct fd_formb * finfo,struct proc * p)1077 fdformat(dev_t dev, struct fd_formb *finfo, struct proc *p)
1078 {
1079         int rv = 0;
1080 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1081 	struct fd_type *type = fd->sc_type;
1082         struct buf *bp;
1083 	int fd_bsize = FD_BSIZE(fd);
1084 
1085         /* set up a buffer header for fdstrategy() */
1086         bp = malloc(sizeof(*bp), M_TEMP, M_NOWAIT | M_ZERO);
1087         if (bp == NULL)
1088                 return ENOBUFS;
1089 
1090         bp->b_flags = B_BUSY | B_PHYS | B_FORMAT | B_RAW;
1091         bp->b_proc = p;
1092         bp->b_dev = dev;
1093 
1094         /*
1095          * calculate a fake blkno, so fdstrategy() would initiate a
1096          * seek to the requested cylinder
1097          */
1098         bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1099                 + finfo->head * type->sectrac) * fd_bsize / DEV_BSIZE;
1100 
1101         bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1102         bp->b_data = (caddr_t)finfo;
1103 
1104 #ifdef DEBUG
1105 	printf("fdformat: blkno %llx count %lx\n", bp->b_blkno, bp->b_bcount);
1106 #endif
1107 
1108         /* now do the format */
1109         fdstrategy(bp);
1110 
1111         /* ...and wait for it to complete */
1112 	rv = biowait(bp);
1113         free(bp, M_TEMP, sizeof(*bp));
1114         return (rv);
1115 }
1116