jcs's openbsd hax
openbsd
1/* $OpenBSD: fd.c,v 1.111 2025/11/17 14:27:43 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! */
84struct 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) */
97struct 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 */
128int fdprobe(struct device *, void *, void *);
129void fdattach(struct device *, struct device *, void *);
130int fdactivate(struct device *, int);
131
132const struct cfattach fd_ca = {
133 sizeof(struct fd_softc), fdprobe, fdattach, NULL, fdactivate
134};
135
136struct cfdriver fd_cd = {
137 NULL, "fd", DV_DISK
138};
139
140int fdgetdisklabel(dev_t, struct fd_softc *, struct disklabel *, int);
141void fdstrategy(struct buf *);
142void fdstart(struct fd_softc *);
143int fdintr(struct fdc_softc *);
144
145void fd_set_motor(struct fdc_softc *fdc, int reset);
146void fd_motor_off(void *arg);
147void fd_motor_on(void *arg);
148void fdfinish(struct fd_softc *fd, struct buf *bp);
149int fdformat(dev_t, struct fd_formb *, struct proc *);
150static __inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
151void fdretry(struct fd_softc *);
152void fdtimeout(void *);
153
154int
155fdgetdisklabel(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
183int
184fdprobe(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 */
243void
244fdattach(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
309int
310fdactivate(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 */
338struct fd_type *
339fd_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
372static __inline struct fd_type *
373fd_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
382void
383fdstrategy(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
448bad:
449 bp->b_flags |= B_ERROR;
450done:
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
458void
459fdstart(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
473void
474fdfinish(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
504int
505fdread(dev_t dev, struct uio *uio, int flags)
506{
507 return (physio(fdstrategy, dev, B_READ, minphys, uio));
508}
509
510int
511fdwrite(dev_t dev, struct uio *uio, int flags)
512{
513 return (physio(fdstrategy, dev, B_WRITE, minphys, uio));
514}
515
516void
517fd_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
536void
537fd_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
548void
549fd_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
563int
564fdopen(dev_t dev, int flags, int fmt, struct proc *p)
565{
566 uint64_t pmask;
567 int unit;
568 struct fd_softc *fd;
569 struct fd_type *type;
570
571 unit = FDUNIT(dev);
572 if (unit >= fd_cd.cd_ndevs)
573 return ENXIO;
574 fd = fd_cd.cd_devs[unit];
575 if (fd == 0)
576 return ENXIO;
577 type = fd_dev_to_type(fd, dev);
578 if (type == NULL)
579 return ENXIO;
580
581 if ((fd->sc_flags & FD_OPEN) != 0 &&
582 fd->sc_type != type)
583 return EBUSY;
584
585 fd->sc_type = type;
586 fd->sc_cylin = -1;
587 fd->sc_flags |= FD_OPEN;
588
589 /*
590 * Only update the disklabel if we're not open anywhere else.
591 */
592 if (fd->sc_dk.dk_openmask == 0)
593 fdgetdisklabel(dev, fd, fd->sc_dk.dk_label, 0);
594
595 pmask = (1ULL << FDPART(dev));
596
597 switch (fmt) {
598 case S_IFCHR:
599 fd->sc_dk.dk_copenmask |= pmask;
600 break;
601
602 case S_IFBLK:
603 fd->sc_dk.dk_bopenmask |= pmask;
604 break;
605 }
606 fd->sc_dk.dk_openmask =
607 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
608
609 return 0;
610}
611
612int
613fdclose(dev_t dev, int flags, int fmt, struct proc *p)
614{
615 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
616 uint64_t pmask = (1ULL << FDPART(dev));
617
618 fd->sc_flags &= ~FD_OPEN;
619 fd->sc_opts &= ~FDOPT_NORETRY;
620
621 switch (fmt) {
622 case S_IFCHR:
623 fd->sc_dk.dk_copenmask &= ~pmask;
624 break;
625
626 case S_IFBLK:
627 fd->sc_dk.dk_bopenmask &= ~pmask;
628 break;
629 }
630 fd->sc_dk.dk_openmask =
631 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
632
633 return (0);
634}
635
636daddr_t
637fdsize(dev_t dev)
638{
639 /* Swapping to floppies would not make sense. */
640 return -1;
641}
642
643int
644fddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
645{
646 /* Not implemented. */
647 return ENXIO;
648}
649
650/*
651 * Called from the controller.
652 */
653int
654fdintr(struct fdc_softc *fdc)
655{
656#define st0 fdc->sc_status[0]
657#define cyl fdc->sc_status[1]
658 struct fd_softc *fd;
659 struct buf *bp;
660 bus_space_tag_t iot = fdc->sc_iot;
661 bus_space_handle_t ioh = fdc->sc_ioh;
662 bus_space_handle_t ioh_ctl = fdc->sc_ioh_ctl;
663 int read, head, sec, i, nblks, cylin;
664 struct fd_type *type;
665 struct fd_formb *finfo = NULL;
666 int fd_bsize;
667
668loop:
669 /* Is there a transfer to this drive? If not, deactivate drive. */
670 fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives);
671 if (fd == NULL) {
672 fdc->sc_state = DEVIDLE;
673 return 1;
674 }
675 fd_bsize = FD_BSIZE(fd);
676
677 bp = fd->sc_bp;
678 if (bp == NULL) {
679 fd->sc_ops = 0;
680 TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
681 goto loop;
682 }
683
684 if (bp->b_flags & B_FORMAT)
685 finfo = (struct fd_formb *)bp->b_data;
686
687 cylin = ((bp->b_blkno * DEV_BSIZE) + (bp->b_bcount - bp->b_resid)) /
688 (fd_bsize * fd->sc_type->seccyl);
689
690 switch (fdc->sc_state) {
691 case DEVIDLE:
692 fdc->sc_errors = 0;
693 fd->sc_skip = 0;
694 fd->sc_bcount = bp->b_bcount;
695 fd->sc_blkno = bp->b_blkno / (fd_bsize / DEV_BSIZE);
696 timeout_del(&fd->fd_motor_off_to);
697 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
698 fdc->sc_state = MOTORWAIT;
699 return 1;
700 }
701 if ((fd->sc_flags & FD_MOTOR) == 0) {
702 /* Turn on the motor, being careful about pairing. */
703 struct fd_softc *ofd =
704 fdc->sc_link.fdlink.sc_fd[fd->sc_drive ^ 1];
705 if (ofd && ofd->sc_flags & FD_MOTOR) {
706 timeout_del(&ofd->fd_motor_off_to);
707 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
708 }
709 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
710 fd_set_motor(fdc, 0);
711 fdc->sc_state = MOTORWAIT;
712 /* Allow .25s for motor to stabilize. */
713 timeout_add_msec(&fd->fd_motor_on_to, 250);
714 return 1;
715 }
716 /* Make sure the right drive is selected. */
717 fd_set_motor(fdc, 0);
718
719 /* FALLTHROUGH */
720 case DOSEEK:
721 doseek:
722 if (fd->sc_cylin == cylin)
723 goto doio;
724
725 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
726 out_fdc(iot, ioh, fd->sc_type->steprate);
727 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
728
729 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
730 out_fdc(iot, ioh, fd->sc_drive); /* drive number */
731 out_fdc(iot, ioh, cylin * fd->sc_type->step);
732
733 fd->sc_cylin = -1;
734 fdc->sc_state = SEEKWAIT;
735
736 fd->sc_dk.dk_seek++;
737 disk_busy(&fd->sc_dk);
738
739 timeout_add_sec(&fd->fdtimeout_to, 4);
740 return 1;
741
742 case DOIO:
743 doio:
744 type = fd->sc_type;
745 if (finfo)
746 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
747 (char *)finfo;
748 sec = fd->sc_blkno % type->seccyl;
749 nblks = type->seccyl - sec;
750 nblks = min(nblks, fd->sc_bcount / fd_bsize);
751 nblks = min(nblks, FDC_MAXIOSIZE / fd_bsize);
752 fd->sc_nblks = nblks;
753 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * fd_bsize;
754 head = sec / type->sectrac;
755 sec -= head * type->sectrac;
756#ifdef DIAGNOSTIC
757 {int block;
758 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
759 if (block != fd->sc_blkno) {
760 panic("fdintr: block %d != blkno %llu", block, fd->sc_blkno);
761 }}
762#endif
763 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
764 isadma_start(bp->b_data + fd->sc_skip, fd->sc_nbytes,
765 fdc->sc_drq, read);
766 bus_space_write_1(iot, ioh_ctl, fdctl, type->rate);
767#ifdef FD_DEBUG
768 printf("fdintr: %s drive %d track %d head %d sec %d nblks %d\n",
769 read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
770 sec, nblks);
771#endif
772 if (finfo) {
773 /* formatting */
774 if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
775 fdc->sc_errors = 4;
776 fdretry(fd);
777 goto loop;
778 }
779 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
780 out_fdc(iot, ioh, finfo->fd_formb_secshift);
781 out_fdc(iot, ioh, finfo->fd_formb_nsecs);
782 out_fdc(iot, ioh, finfo->fd_formb_gaplen);
783 out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
784 } else {
785 if (read)
786 out_fdc(iot, ioh, NE7CMD_READ); /* READ */
787 else
788 out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
789 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
790 out_fdc(iot, ioh, fd->sc_cylin); /* track */
791 out_fdc(iot, ioh, head);
792 out_fdc(iot, ioh, sec + 1); /* sec +1 */
793 out_fdc(iot, ioh, type->secsize); /* sec size */
794 out_fdc(iot, ioh, type->sectrac); /* secs/track */
795 out_fdc(iot, ioh, type->gap1); /* gap1 size */
796 out_fdc(iot, ioh, type->datalen); /* data len */
797 }
798 fdc->sc_state = IOCOMPLETE;
799
800 disk_busy(&fd->sc_dk);
801
802 /* allow 2 seconds for operation */
803 timeout_add_sec(&fd->fdtimeout_to, 2);
804 return 1; /* will return later */
805
806 case SEEKWAIT:
807 timeout_del(&fd->fdtimeout_to);
808 fdc->sc_state = SEEKCOMPLETE;
809 /* allow 1/50 second for heads to settle */
810 timeout_add_msec(&fdc->fdcpseudointr_to, 20);
811 return 1;
812
813 case SEEKCOMPLETE:
814 disk_unbusy(&fd->sc_dk, 0, 0, 0); /* no data on seek */
815
816 /* Make sure seek really happened. */
817 out_fdc(iot, ioh, NE7CMD_SENSEI);
818 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
819 cyl != cylin * fd->sc_type->step) {
820#ifdef FD_DEBUG
821 fdcstatus(&fd->sc_dev, 2, "seek failed");
822#endif
823 fdretry(fd);
824 goto loop;
825 }
826 fd->sc_cylin = cylin;
827 goto doio;
828
829 case IOTIMEDOUT:
830 isadma_abort(fdc->sc_drq);
831 case SEEKTIMEDOUT:
832 case RECALTIMEDOUT:
833 case RESETTIMEDOUT:
834 fdretry(fd);
835 goto loop;
836
837 case IOCOMPLETE: /* IO DONE, post-analyze */
838 timeout_del(&fd->fdtimeout_to);
839
840 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
841 fd->sc_blkno, (bp->b_flags & B_READ));
842
843 if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
844 isadma_abort(fdc->sc_drq);
845#ifdef FD_DEBUG
846 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
847 "read failed" : "write failed");
848 printf("blkno %lld nblks %d\n",
849 (long long)fd->sc_blkno, fd->sc_nblks);
850#endif
851 fdretry(fd);
852 goto loop;
853 }
854 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
855 isadma_done(fdc->sc_drq);
856 if (fdc->sc_errors) {
857 diskerr(bp, "fd", "soft error", LOG_PRINTF,
858 fd->sc_skip / fd_bsize, (struct disklabel *)NULL);
859 printf("\n");
860 fdc->sc_errors = 0;
861 }
862
863 fd->sc_blkno += fd->sc_nblks;
864 fd->sc_skip += fd->sc_nbytes;
865 fd->sc_bcount -= fd->sc_nbytes;
866 bp->b_resid -= fd->sc_nbytes;
867 if (!finfo && fd->sc_bcount > 0) {
868 cylin = fd->sc_blkno / fd->sc_type->seccyl;
869 goto doseek;
870 }
871 fdfinish(fd, bp);
872 goto loop;
873
874 case DORESET:
875 /* try a reset, keep motor on */
876 fd_set_motor(fdc, 1);
877 delay(100);
878 fd_set_motor(fdc, 0);
879 fdc->sc_state = RESETCOMPLETE;
880 timeout_add_msec(&fd->fdtimeout_to, 500);
881 return 1; /* will return later */
882
883 case RESETCOMPLETE:
884 timeout_del(&fd->fdtimeout_to);
885 /* clear the controller output buffer */
886 for (i = 0; i < 4; i++) {
887 out_fdc(iot, ioh, NE7CMD_SENSEI);
888 (void) fdcresult(fdc);
889 }
890
891 /* FALLTHROUGH */
892 case DORECAL:
893 out_fdc(iot, ioh, NE7CMD_RECAL); /* recal function */
894 out_fdc(iot, ioh, fd->sc_drive);
895 fdc->sc_state = RECALWAIT;
896 timeout_add_sec(&fd->fdtimeout_to, 5);
897 return 1; /* will return later */
898
899 case RECALWAIT:
900 timeout_del(&fd->fdtimeout_to);
901 fdc->sc_state = RECALCOMPLETE;
902 /* allow 1/30 second for heads to settle */
903 timeout_add_msec(&fdc->fdcpseudointr_to, 1000 / 30);
904 return 1; /* will return later */
905
906 case RECALCOMPLETE:
907 out_fdc(iot, ioh, NE7CMD_SENSEI);
908 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
909#ifdef FD_DEBUG
910 fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
911#endif
912 fdretry(fd);
913 goto loop;
914 }
915 fd->sc_cylin = 0;
916 goto doseek;
917
918 case MOTORWAIT:
919 if (fd->sc_flags & FD_MOTOR_WAIT)
920 return 1; /* time's not up yet */
921 goto doseek;
922
923 default:
924 fdcstatus(&fd->sc_dev, 0, "stray interrupt");
925 return 1;
926 }
927#ifdef DIAGNOSTIC
928 panic("fdintr: impossible");
929#endif
930#undef st0
931#undef cyl
932}
933
934void
935fdtimeout(void *arg)
936{
937 struct fd_softc *fd = arg;
938 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
939 int s;
940
941 s = splbio();
942#ifdef DEBUG
943 log(LOG_ERR,"fdtimeout: state %d\n", fdc->sc_state);
944#endif
945 fdcstatus(&fd->sc_dev, 0, "timeout");
946
947 if (fd->sc_bp != NULL)
948 fdc->sc_state++;
949 else
950 fdc->sc_state = DEVIDLE;
951
952 (void) fdintr(fdc);
953 splx(s);
954}
955
956void
957fdretry(struct fd_softc *fd)
958{
959 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
960 struct buf *bp = fd->sc_bp;
961
962 if (fd->sc_opts & FDOPT_NORETRY)
963 goto fail;
964 switch (fdc->sc_errors) {
965 case 0:
966 /* try again */
967 fdc->sc_state = DOSEEK;
968 break;
969
970 case 1: case 2: case 3:
971 /* didn't work; try recalibrating */
972 fdc->sc_state = DORECAL;
973 break;
974
975 case 4:
976 /* still no go; reset the bastard */
977 fdc->sc_state = DORESET;
978 break;
979
980 default:
981 fail:
982 diskerr(bp, "fd", "hard error", LOG_PRINTF,
983 fd->sc_skip / FD_BSIZE(fd), (struct disklabel *)NULL);
984 printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
985 fdc->sc_status[0], NE7_ST0BITS,
986 fdc->sc_status[1], NE7_ST1BITS,
987 fdc->sc_status[2], NE7_ST2BITS,
988 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
989
990 bp->b_flags |= B_ERROR;
991 bp->b_error = EIO;
992 bp->b_resid = bp->b_bcount;
993 fdfinish(fd, bp);
994 }
995 fdc->sc_errors++;
996}
997
998int
999fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1000{
1001 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1002 struct disklabel *lp;
1003 int error;
1004
1005 switch (cmd) {
1006 case MTIOCTOP:
1007 if (((struct mtop *)addr)->mt_op != MTOFFL)
1008 return EIO;
1009 return (0);
1010
1011 case DIOCRLDINFO:
1012 lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
1013 fdgetdisklabel(dev, fd, lp, 0);
1014 bcopy(lp, fd->sc_dk.dk_label, sizeof(*lp));
1015 free(lp, M_TEMP, sizeof(*lp));
1016 return 0;
1017
1018 case DIOCGPDINFO:
1019 fdgetdisklabel(dev, fd, (struct disklabel *)addr, 1);
1020 return 0;
1021
1022 case DIOCGDINFO:
1023 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1024 return 0;
1025
1026 case DIOCGPART:
1027 ((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
1028 ((struct partinfo *)addr)->part =
1029 &fd->sc_dk.dk_label->d_partitions[FDPART(dev)];
1030 return 0;
1031
1032 case DIOCWDINFO:
1033 case DIOCSDINFO:
1034 if ((flag & FWRITE) == 0)
1035 return EBADF;
1036
1037 error = setdisklabel(fd->sc_dk.dk_label,
1038 (struct disklabel *)addr, 0);
1039 if (error == 0) {
1040 if (cmd == DIOCWDINFO)
1041 error = writedisklabel(DISKLABELDEV(dev),
1042 fdstrategy, fd->sc_dk.dk_label);
1043 }
1044 return error;
1045
1046 case FD_FORM:
1047 if((flag & FWRITE) == 0)
1048 return EBADF; /* must be opened for writing */
1049 else if(((struct fd_formb *)addr)->format_version !=
1050 FD_FORMAT_VERSION)
1051 return EINVAL; /* wrong version of formatting prog */
1052 else
1053 return fdformat(dev, (struct fd_formb *)addr, p);
1054 break;
1055
1056 case FD_GTYPE: /* get drive type */
1057 *(struct fd_type *)addr = *fd->sc_type;
1058 return 0;
1059
1060 case FD_GOPTS: /* get drive options */
1061 *(int *)addr = fd->sc_opts;
1062 return 0;
1063
1064 case FD_SOPTS: /* set drive options */
1065 fd->sc_opts = *(int *)addr;
1066 return 0;
1067
1068 default:
1069 return ENOTTY;
1070 }
1071
1072#ifdef DIAGNOSTIC
1073 panic("fdioctl: impossible");
1074#endif
1075}
1076
1077int
1078fdformat(dev_t dev, struct fd_formb *finfo, struct proc *p)
1079{
1080 int rv = 0;
1081 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1082 struct fd_type *type = fd->sc_type;
1083 struct buf *bp;
1084 int fd_bsize = FD_BSIZE(fd);
1085
1086 /* set up a buffer header for fdstrategy() */
1087 bp = malloc(sizeof(*bp), M_TEMP, M_NOWAIT | M_ZERO);
1088 if (bp == NULL)
1089 return ENOBUFS;
1090
1091 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT | B_RAW;
1092 bp->b_proc = p;
1093 bp->b_dev = dev;
1094
1095 /*
1096 * calculate a fake blkno, so fdstrategy() would initiate a
1097 * seek to the requested cylinder
1098 */
1099 bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1100 + finfo->head * type->sectrac) * fd_bsize / DEV_BSIZE;
1101
1102 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1103 bp->b_data = (caddr_t)finfo;
1104
1105#ifdef DEBUG
1106 printf("fdformat: blkno %llx count %lx\n", bp->b_blkno, bp->b_bcount);
1107#endif
1108
1109 /* now do the format */
1110 fdstrategy(bp);
1111
1112 /* ...and wait for it to complete */
1113 rv = biowait(bp);
1114 free(bp, M_TEMP, sizeof(*bp));
1115 return (rv);
1116}