fd.c revision 1.18 1 /* $NetBSD: fd.c,v 1.18 1996/01/12 00:19:47 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995 Charles Hannum.
5 * Copyright (c) 1995 Paul Kranenburg.
6 * Copyright (c) 1990 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Don Ahn.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)fd.c 7.4 (Berkeley) 5/25/91
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/conf.h>
47 #include <sys/file.h>
48 #include <sys/ioctl.h>
49 #include <sys/device.h>
50 #include <sys/disklabel.h>
51 #include <sys/dkstat.h>
52 #include <sys/disk.h>
53 #include <sys/buf.h>
54 #include <sys/uio.h>
55 #include <sys/syslog.h>
56 #include <sys/queue.h>
57
58 #include <machine/cpu.h>
59 #include <machine/autoconf.h>
60 #include <sparc/sparc/auxreg.h>
61 #include <sparc/dev/fdreg.h>
62 #include <sparc/dev/fdvar.h>
63
64 #define FDUNIT(dev) (minor(dev) / 8)
65 #define FDTYPE(dev) (minor(dev) % 8)
66
67 #define b_cylin b_resid
68
69 #define FD_DEBUG
70 #ifdef FD_DEBUG
71 int fdc_debug = 0;
72 #endif
73
74 enum fdc_state {
75 DEVIDLE = 0,
76 MOTORWAIT,
77 DOSEEK,
78 SEEKWAIT,
79 SEEKTIMEDOUT,
80 SEEKCOMPLETE,
81 DOIO,
82 IOCOMPLETE,
83 IOTIMEDOUT,
84 DORESET,
85 RESETCOMPLETE,
86 RESETTIMEDOUT,
87 DORECAL,
88 RECALWAIT,
89 RECALTIMEDOUT,
90 RECALCOMPLETE,
91 };
92
93 /* software state, per controller */
94 struct fdc_softc {
95 struct device sc_dev; /* boilerplate */
96 struct intrhand sc_sih;
97 struct intrhand sc_hih;
98 caddr_t sc_reg;
99 struct fd_softc *sc_fd[4]; /* pointers to children */
100 TAILQ_HEAD(drivehead, fd_softc) sc_drives;
101 enum fdc_state sc_state;
102 int sc_flags;
103 #define FDC_82077 0x01
104 #define FDC_NEEDHEADSETTLE 0x02
105 #define FDC_EIS 0x04
106 int sc_errors; /* number of retries so far */
107 int sc_overruns; /* number of DMA overruns */
108 int sc_cfg; /* current configuration */
109 struct fdcio sc_io;
110 #define sc_reg_msr sc_io.fdcio_reg_msr
111 #define sc_reg_fifo sc_io.fdcio_reg_fifo
112 #define sc_reg_dor sc_io.fdcio_reg_dor
113 #define sc_reg_drs sc_io.fdcio_reg_msr
114 #define sc_istate sc_io.fdcio_istate
115 #define sc_data sc_io.fdcio_data
116 #define sc_tc sc_io.fdcio_tc
117 #define sc_nstat sc_io.fdcio_nstat
118 #define sc_status sc_io.fdcio_status
119 #define sc_intrcnt sc_io.fdcio_intrcnt
120 };
121
122 #ifndef FDC_C_HANDLER
123 extern struct fdcio *fdciop;
124 #endif
125
126 /* controller driver configuration */
127 int fdcmatch __P((struct device *, void *, void *));
128 void fdcattach __P((struct device *, struct device *, void *));
129
130 struct cfdriver fdccd = {
131 NULL, "fdc", fdcmatch, fdcattach, DV_DULL, sizeof(struct fdc_softc)
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 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" }, /* 1.44MB diskette */
157 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,"1.2MB" }, /* 1.2 MB AT-diskettes */
158 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,"360KB/AT" }, /* 360kB in 1.2MB drive */
159 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,"360KB/PC" }, /* 360kB PC diskettes */
160 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,"720KB" }, /* 3.5" 720kB diskette */
161 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,"720KB/x" }, /* 720kB in 1.2MB drive */
162 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,"360KB/x" }, /* 360kB in 720kB drive */
163 };
164
165 /* software state, per disk (with up to 4 disks per ctlr) */
166 struct fd_softc {
167 struct device sc_dv; /* generic device info */
168 struct disk sc_dk; /* generic disk info */
169
170 struct fd_type *sc_deftype; /* default type descriptor */
171 struct fd_type *sc_type; /* current type descriptor */
172
173 daddr_t sc_blkno; /* starting block number */
174 int sc_bcount; /* byte count left */
175 int sc_skip; /* bytes already transferred */
176 int sc_nblks; /* number of blocks currently tranferring */
177 int sc_nbytes; /* number of bytes currently tranferring */
178
179 int sc_drive; /* physical unit number */
180 int sc_flags;
181 #define FD_OPEN 0x01 /* it's open */
182 #define FD_MOTOR 0x02 /* motor should be on */
183 #define FD_MOTOR_WAIT 0x04 /* motor coming up */
184 int sc_cylin; /* where we think the head is */
185
186 TAILQ_ENTRY(fd_softc) sc_drivechain;
187 int sc_ops; /* I/O ops since last switch */
188 struct buf sc_q; /* head of buf chain */
189 };
190
191 /* floppy driver configuration */
192 int fdmatch __P((struct device *, void *, void *));
193 void fdattach __P((struct device *, struct device *, void *));
194
195 struct cfdriver fdcd = {
196 NULL, "fd", fdmatch, fdattach, DV_DISK, sizeof(struct fd_softc)
197 };
198
199 void fdgetdisklabel __P((struct fd_softc *));
200 int fd_get_parms __P((struct fd_softc *));
201 void fdstrategy __P((struct buf *));
202 void fdstart __P((struct fd_softc *));
203
204 struct dkdriver fddkdriver = { fdstrategy };
205
206 struct fd_type *fd_nvtotype __P((char *, int, int));
207 void fd_set_motor __P((struct fdc_softc *fdc));
208 void fd_motor_off __P((void *arg));
209 void fd_motor_on __P((void *arg));
210 int fdcresult __P((struct fdc_softc *fdc));
211 int out_fdc __P((struct fdc_softc *fdc, u_char x));
212 void fdcstart __P((struct fdc_softc *fdc));
213 void fdcstatus __P((struct device *dv, int n, char *s));
214 void fdc_reset __P((struct fdc_softc *fdc));
215 void fdctimeout __P((void *arg));
216 void fdcpseudointr __P((void *arg));
217 #ifdef FDC_C_HANDLER
218 int fdchwintr __P((struct fdc_softc *));
219 #else
220 void fdchwintr __P((void));
221 #endif
222 int fdcswintr __P((struct fdc_softc *));
223 void fdcretry __P((struct fdc_softc *fdc));
224 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
225
226 #if PIL_FDSOFT == 4
227 #define IE_FDSOFT IE_L4
228 #else
229 #error 4
230 #endif
231
232 #define OBP_FDNAME (cputyp == CPU_SUN4M ? "SUNW,fdtwo" : "fd")
233
234 int
235 fdcmatch(parent, match, aux)
236 struct device *parent;
237 void *match, *aux;
238 {
239 struct cfdata *cf = match;
240 register struct confargs *ca = aux;
241 register struct romaux *ra = &ca->ca_ra;
242
243 /* Sun PROMs call the controller an "fd" or "SUNW,fdtwo" */
244 if (strcmp(OBP_FDNAME, ra->ra_name))
245 return (0);
246 if (ca->ca_bustype == BUS_MAIN) {
247 if (ca->ca_ra.ra_vaddr &&
248 probeget(ca->ca_ra.ra_vaddr, 1) == -1) {
249 return (0);
250 }
251 return (1);
252 }
253
254 return (0);
255 }
256
257 /*
258 * Arguments passed between fdcattach and fdprobe.
259 */
260 struct fdc_attach_args {
261 int fa_drive;
262 int fa_bootdev;
263 struct fd_type *fa_deftype;
264 };
265
266 /*
267 * Print the location of a disk drive (called just before attaching the
268 * the drive). If `fdc' is not NULL, the drive was found but was not
269 * in the system config file; print the drive name as well.
270 * Return QUIET (config_find ignores this if the device was configured) to
271 * avoid printing `fdN not configured' messages.
272 */
273 int
274 fdprint(aux, fdc)
275 void *aux;
276 char *fdc;
277 {
278 register struct fdc_attach_args *fa = aux;
279
280 if (!fdc)
281 printf(" drive %d", fa->fa_drive);
282 return QUIET;
283 }
284
285 static void
286 fdconf(fdc)
287 struct fdc_softc *fdc;
288 {
289 int vroom;
290
291 if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
292 return;
293
294 /*
295 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
296 * the PROM thinks is appropriate.
297 */
298 if ((vroom = fdc->sc_status[7]) == 0)
299 vroom = 0x64;
300
301 /* Configure controller to use FIFO and Implied Seek */
302 out_fdc(fdc, NE7CMD_CFG);
303 out_fdc(fdc, vroom);
304 out_fdc(fdc, fdc->sc_cfg);
305 out_fdc(fdc, 0); /* PRETRK */
306 /* No result phase */
307 }
308
309 void
310 fdcattach(parent, self, aux)
311 struct device *parent, *self;
312 void *aux;
313 {
314 register struct confargs *ca = aux;
315 struct fdc_softc *fdc = (void *)self;
316 struct fdc_attach_args fa;
317 int n, pri;
318 char code;
319
320 if (ca->ca_ra.ra_vaddr)
321 fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr;
322 else
323 fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_reg, 0,
324 ca->ca_ra.ra_len,
325 ca->ca_bustype);
326
327 fdc->sc_state = DEVIDLE;
328 fdc->sc_istate = ISTATE_IDLE;
329 fdc->sc_flags |= FDC_EIS;
330 TAILQ_INIT(&fdc->sc_drives);
331
332 pri = ca->ca_ra.ra_intr[0].int_pri;
333 #ifdef FDC_C_HANDLER
334 fdc->sc_hih.ih_fun = (void *)fdchwintr;
335 fdc->sc_hih.ih_arg = fdc;
336 intr_establish(pri, &fdc->sc_hih);
337 #else
338 fdciop = &fdc->sc_io;
339 intr_fasttrap(pri, fdchwintr);
340 #endif
341 fdc->sc_sih.ih_fun = (void *)fdcswintr;
342 fdc->sc_sih.ih_arg = fdc;
343 intr_establish(PIL_FDSOFT, &fdc->sc_sih);
344
345 /* Assume a 82077 */
346 fdc->sc_reg_msr = &((struct fdreg_77 *)fdc->sc_reg)->fd_msr;
347 fdc->sc_reg_fifo = &((struct fdreg_77 *)fdc->sc_reg)->fd_fifo;
348 fdc->sc_reg_dor = &((struct fdreg_77 *)fdc->sc_reg)->fd_dor;
349
350 code = '7';
351 if (*fdc->sc_reg_dor == NE7_RQM) {
352 /*
353 * This hack from Chris Torek: apparently DOR really
354 * addresses MSR/DRS on a 82072.
355 * We used to rely on the VERSION command to tell the
356 * difference (which did not work).
357 */
358 *fdc->sc_reg_dor = FDC_250KBPS;
359 if (*fdc->sc_reg_dor == NE7_RQM)
360 code = '2';
361 }
362 if (code == '7') {
363 fdc->sc_flags |= FDC_82077;
364 } else {
365 fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr;
366 fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo;
367 fdc->sc_reg_dor = 0;
368 }
369
370 #ifdef FD_DEBUG
371 if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
372 fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
373 if (fdc_debug)
374 printf("[version cmd]");
375 }
376 #endif
377
378 /*
379 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
380 * Note: CFG_EFIFO is active-low, initial threshold value: 8
381 */
382 fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
383 fdconf(fdc);
384
385 if (fdc->sc_flags & FDC_82077) {
386 /* Lock configuration across soft resets. */
387 out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
388 if (fdcresult(fdc) != 1)
389 printf(" CFGLOCK: unexpected response");
390 }
391
392 evcnt_attach(&fdc->sc_dev, "intr", &fdc->sc_intrcnt);
393
394 printf(" pri %d, softpri %d: chip 8207%c\n", pri, PIL_FDSOFT, code);
395
396 /*
397 * Controller and drives are represented by one and the same
398 * Openprom node, so we can as well check for the floppy boots here.
399 */
400 if (ca->ca_ra.ra_bp &&
401 strcmp(ca->ca_ra.ra_bp->name, OBP_FDNAME) == 0 &&
402 ca->ca_ra.ra_bp->val[0] == 0 &&
403 ca->ca_ra.ra_bp->val[1] == 0)
404 fa.fa_bootdev = 1;
405 else
406 fa.fa_bootdev = 0;
407
408 /* physical limit: four drives per controller. */
409 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
410 fa.fa_deftype = NULL; /* unknown */
411 fa.fa_deftype = &fd_types[0]; /* XXX */
412 (void)config_found(self, (void *)&fa, fdprint);
413 }
414
415 bootpath_store(1, NULL);
416 }
417
418 int
419 fdmatch(parent, match, aux)
420 struct device *parent;
421 void *match, *aux;
422 {
423 struct fdc_softc *fdc = (void *)parent;
424 struct cfdata *cf = match;
425 struct fdc_attach_args *fa = aux;
426 int drive = fa->fa_drive;
427 int n;
428
429 if (drive > 0)
430 /* XXX - for now, punt > 1 drives */
431 return 0;
432
433 if (fdc->sc_flags & FDC_82077) {
434 /* select drive and turn on motor */
435 *fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
436 /* wait for motor to spin up */
437 delay(250000);
438 } else {
439 auxregbisc(AUXIO_FDS, 0);
440 }
441 fdc->sc_nstat = 0;
442 out_fdc(fdc, NE7CMD_RECAL);
443 out_fdc(fdc, drive);
444 /* wait for recalibrate */
445 for (n = 0; n < 100000; n++) {
446 delay(10);
447 if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
448 /* wait a bit longer till device *really* is ready */
449 delay(100000);
450 if (out_fdc(fdc, NE7CMD_SENSEI))
451 break;
452 if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
453 /*
454 * Got `invalid command'; we interpret it
455 * to mean that the re-calibrate hasn't in
456 * fact finished yet
457 */
458 continue;
459 break;
460 }
461 }
462 n = fdc->sc_nstat;
463 #ifdef FD_DEBUG
464 if (fdc_debug) {
465 int i;
466 printf("fdprobe: %d stati:", n);
467 for (i = 0; i < n; i++)
468 printf(" %x", fdc->sc_status[i]);
469 printf("\n");
470 }
471 #endif
472 if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
473 return 0;
474 /* turn off motor */
475 if (fdc->sc_flags & FDC_82077) {
476 /* select drive and turn on motor */
477 *fdc->sc_reg_dor = FDO_FRST;
478 } else {
479 auxregbisc(0, AUXIO_FDS);
480 }
481
482 return 1;
483 }
484
485 /*
486 * Controller is working, and drive responded. Attach it.
487 */
488 void
489 fdattach(parent, self, aux)
490 struct device *parent, *self;
491 void *aux;
492 {
493 struct fdc_softc *fdc = (void *)parent;
494 struct fd_softc *fd = (void *)self;
495 struct fdc_attach_args *fa = aux;
496 struct fd_type *type = fa->fa_deftype;
497 int drive = fa->fa_drive;
498
499 /* XXX Allow `flags' to override device type? */
500
501 if (type)
502 printf(": %s %d cyl, %d head, %d sec\n", type->name,
503 type->tracks, type->heads, type->sectrac);
504 else
505 printf(": density unknown\n");
506
507 fd->sc_cylin = -1;
508 fd->sc_drive = drive;
509 fd->sc_deftype = type;
510 fdc->sc_fd[drive] = fd;
511
512 /*
513 * Initialize and attach the disk structure.
514 */
515 fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
516 fd->sc_dk.dk_driver = &fddkdriver;
517 disk_attach(&fd->sc_dk);
518
519 /*
520 * We're told if we're the boot device in fdcattach().
521 */
522 if (fa->fa_bootdev)
523 bootdv = &fd->sc_dv;
524
525 /* XXX Need to do some more fiddling with sc_dk. */
526 dk_establish(&fd->sc_dk, &fd->sc_dv);
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 type ? &fd_types[type - 1] : fd->sc_deftype;
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 /* Valid unit, controller, and request? */
551 if (unit >= fdcd.cd_ndevs ||
552 (fd = fdcd.cd_devs[unit]) == 0 ||
553 bp->b_blkno < 0 ||
554 (bp->b_bcount % FDC_BSIZE) != 0) {
555 bp->b_error = EINVAL;
556 goto bad;
557 }
558
559 /* If it's a null transfer, return immediately. */
560 if (bp->b_bcount == 0)
561 goto done;
562
563 sz = howmany(bp->b_bcount, FDC_BSIZE);
564
565 if (bp->b_blkno + sz > fd->sc_type->size) {
566 sz = fd->sc_type->size - bp->b_blkno;
567 if (sz == 0) {
568 /* If exactly at end of disk, return EOF. */
569 bp->b_resid = bp->b_bcount;
570 goto done;
571 }
572 if (sz < 0) {
573 /* If past end of disk, return EINVAL. */
574 bp->b_error = EINVAL;
575 goto bad;
576 }
577 /* Otherwise, truncate request. */
578 bp->b_bcount = sz << DEV_BSHIFT;
579 }
580
581 bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
582
583 #ifdef FD_DEBUG
584 if (fdc_debug > 1)
585 printf("fdstrategy: b_blkno %d b_bcount %d blkno %d cylin %d\n",
586 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
587 #endif
588
589 /* Queue transfer on drive, activate drive and controller if idle. */
590 s = splbio();
591 disksort(&fd->sc_q, bp);
592 untimeout(fd_motor_off, fd); /* a good idea */
593 if (!fd->sc_q.b_active)
594 fdstart(fd);
595 #ifdef DIAGNOSTIC
596 else {
597 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
598 if (fdc->sc_state == DEVIDLE) {
599 printf("fdstrategy: controller inactive\n");
600 fdcstart(fdc);
601 }
602 }
603 #endif
604 splx(s);
605 return;
606
607 bad:
608 bp->b_flags |= B_ERROR;
609 done:
610 /* Toss transfer; we're done early. */
611 biodone(bp);
612 }
613
614 void
615 fdstart(fd)
616 struct fd_softc *fd;
617 {
618 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
619 int active = fdc->sc_drives.tqh_first != 0;
620
621 /* Link into controller queue. */
622 fd->sc_q.b_active = 1;
623 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
624
625 /* If controller not already active, start it. */
626 if (!active)
627 fdcstart(fdc);
628 }
629
630 void
631 fdfinish(fd, bp)
632 struct fd_softc *fd;
633 struct buf *bp;
634 {
635 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
636
637 /*
638 * Move this drive to the end of the queue to give others a `fair'
639 * chance. We only force a switch if N operations are completed while
640 * another drive is waiting to be serviced, since there is a long motor
641 * startup delay whenever we switch.
642 */
643 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
644 fd->sc_ops = 0;
645 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
646 if (bp->b_actf) {
647 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
648 } else
649 fd->sc_q.b_active = 0;
650 }
651 bp->b_resid = fd->sc_bcount;
652 fd->sc_skip = 0;
653 fd->sc_q.b_actf = bp->b_actf;
654
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 void
662 fdc_reset(fdc)
663 struct fdc_softc *fdc;
664 {
665 if (fdc->sc_flags & FDC_82077) {
666 *fdc->sc_reg_dor = FDO_MOEN(0);
667 }
668
669 *fdc->sc_reg_drs = DRS_RESET;
670 delay(10);
671 *fdc->sc_reg_drs = 0;
672 #ifdef FD_DEBUG
673 if (fdc_debug)
674 printf("fdc reset\n");
675 #endif
676 }
677
678 void
679 fd_set_motor(fdc)
680 struct fdc_softc *fdc;
681 {
682 struct fd_softc *fd;
683 u_char status;
684 int n;
685
686 if (fdc->sc_flags & FDC_82077) {
687 status = FDO_FRST | FDO_FDMAEN;
688 if (fd = fdc->sc_drives.tqh_first)
689 status |= fd->sc_drive;
690
691 for (n = 0; n < 4; n++)
692 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
693 status |= FDO_MOEN(n);
694 *fdc->sc_reg_dor = status;
695 } else {
696 int on = 0;
697
698 for (n = 0; n < 4; n++)
699 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
700 on = 1;
701 if (on) {
702 auxregbisc(AUXIO_FDS, 0);
703 } else {
704 auxregbisc(0, AUXIO_FDS);
705 }
706 }
707 }
708
709 void
710 fd_motor_off(arg)
711 void *arg;
712 {
713 struct fd_softc *fd = arg;
714 int s;
715
716 s = splbio();
717 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
718 fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
719 splx(s);
720 }
721
722 void
723 fd_motor_on(arg)
724 void *arg;
725 {
726 struct fd_softc *fd = arg;
727 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
728 int s;
729
730 s = splbio();
731 fd->sc_flags &= ~FD_MOTOR_WAIT;
732 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
733 (void) fdcswintr(fdc);
734 splx(s);
735 }
736
737 int
738 fdcresult(fdc)
739 struct fdc_softc *fdc;
740 {
741 u_char i;
742 int j = 100000,
743 n = 0;
744
745 for (; j; j--) {
746 i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
747 if (i == NE7_RQM)
748 return (fdc->sc_nstat = n);
749 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
750 if (n >= sizeof(fdc->sc_status)) {
751 log(LOG_ERR, "fdcresult: overrun\n");
752 return -1;
753 }
754 fdc->sc_status[n++] = *fdc->sc_reg_fifo;
755 }
756 }
757 log(LOG_ERR, "fdcresult: timeout\n");
758 return (fdc->sc_nstat = -1);
759 }
760
761 int
762 out_fdc(fdc, x)
763 struct fdc_softc *fdc;
764 u_char x;
765 {
766 int i = 100000;
767
768 while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0);
769 if (i <= 0)
770 return -1;
771
772 *fdc->sc_reg_fifo = x;
773 return 0;
774 }
775
776 int
777 Fdopen(dev, flags)
778 dev_t dev;
779 int flags;
780 {
781 int unit;
782 struct fd_softc *fd;
783 struct fd_type *type;
784
785 unit = FDUNIT(dev);
786 if (unit >= fdcd.cd_ndevs)
787 return ENXIO;
788 fd = fdcd.cd_devs[unit];
789 if (fd == 0)
790 return ENXIO;
791 type = fd_dev_to_type(fd, dev);
792 if (type == NULL)
793 return ENXIO;
794
795 if ((fd->sc_flags & FD_OPEN) != 0 &&
796 fd->sc_type != type)
797 return EBUSY;
798
799 fd->sc_type = type;
800 fd->sc_cylin = -1;
801 fd->sc_flags |= FD_OPEN;
802
803 return 0;
804 }
805
806 int
807 fdclose(dev, flags)
808 dev_t dev;
809 int flags;
810 {
811 struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
812
813 fd->sc_flags &= ~FD_OPEN;
814 return 0;
815 }
816
817 int
818 fdread(dev, uio)
819 dev_t dev;
820 struct uio *uio;
821 {
822
823 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
824 }
825
826 int
827 fdwrite(dev, uio)
828 dev_t dev;
829 struct uio *uio;
830 {
831
832 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
833 }
834
835 void
836 fdcstart(fdc)
837 struct fdc_softc *fdc;
838 {
839
840 #ifdef DIAGNOSTIC
841 /* only got here if controller's drive queue was inactive; should
842 be in idle state */
843 if (fdc->sc_state != DEVIDLE) {
844 printf("fdcstart: not idle\n");
845 return;
846 }
847 #endif
848 (void) fdcswintr(fdc);
849 }
850
851 void
852 fdcstatus(dv, n, s)
853 struct device *dv;
854 int n;
855 char *s;
856 {
857 struct fdc_softc *fdc = (void *)dv->dv_parent;
858
859 #if 0
860 /*
861 * A 82072 seems to return <invalid command> on
862 * gratuitous Sense Interrupt commands.
863 */
864 if (n == 0 && (fdc->sc_flags & FDC_82077)) {
865 out_fdc(fdc, NE7CMD_SENSEI);
866 (void) fdcresult(fdc);
867 n = 2;
868 }
869 #endif
870
871 /* Just print last status */
872 n = fdc->sc_nstat;
873
874 printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
875
876 switch (n) {
877 case 0:
878 printf("\n");
879 break;
880 case 2:
881 printf(" (st0 %b cyl %d)\n",
882 fdc->sc_status[0], NE7_ST0BITS,
883 fdc->sc_status[1]);
884 break;
885 case 7:
886 printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
887 fdc->sc_status[0], NE7_ST0BITS,
888 fdc->sc_status[1], NE7_ST1BITS,
889 fdc->sc_status[2], NE7_ST2BITS,
890 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
891 break;
892 #ifdef DIAGNOSTIC
893 default:
894 printf(" fdcstatus: weird size: %d\n", n);
895 break;
896 #endif
897 }
898 }
899
900 void
901 fdctimeout(arg)
902 void *arg;
903 {
904 struct fdc_softc *fdc = arg;
905 struct fd_softc *fd = fdc->sc_drives.tqh_first;
906 int s;
907
908 s = splbio();
909 fdcstatus(&fd->sc_dv, 0, "timeout");
910
911 if (fd->sc_q.b_actf)
912 fdc->sc_state++;
913 else
914 fdc->sc_state = DEVIDLE;
915
916 (void) fdcswintr(fdc);
917 splx(s);
918 }
919
920 void
921 fdcpseudointr(arg)
922 void *arg;
923 {
924 struct fdc_softc *fdc = arg;
925 int s;
926
927 /* Just ensure it has the right spl. */
928 s = splbio();
929 (void) fdcswintr(fdc);
930 splx(s);
931 }
932
933
934 #ifdef FDC_C_HANDLER
935 /*
936 * hardware interrupt entry point: must be converted to `fast'
937 * (in-window) handler.
938 */
939 int
940 fdchwintr(fdc)
941 struct fdc_softc *fdc;
942 {
943 struct buf *bp;
944 int read;
945
946 switch (fdc->sc_istate) {
947 case ISTATE_SENSEI:
948 out_fdc(fdc, NE7CMD_SENSEI);
949 fdcresult(fdc);
950 fdc->sc_istate = ISTATE_IDLE;
951 ienab_bis(IE_FDSOFT);
952 return 1;
953 case ISTATE_IDLE:
954 case ISTATE_SPURIOUS:
955 auxregbisc(0, AUXIO_FDS); /* Does this help? */
956 fdcresult(fdc);
957 fdc->sc_istate = ISTATE_SPURIOUS;
958 printf("fdc: stray hard interrupt... ");
959 ienab_bis(IE_FDSOFT);
960 return 1;
961 case ISTATE_DMA:
962 break;
963 default:
964 printf("fdc: goofed ...\n");
965 return 1;
966 }
967
968 read = bp->b_flags & B_READ;
969 for (;;) {
970 register int msr;
971
972 msr = *fdc->sc_reg_msr;
973
974 if ((msr & NE7_RQM) == 0)
975 break;
976
977 if ((msr & NE7_NDM) == 0) {
978 fdcresult(fdc);
979 fdc->sc_istate = ISTATE_IDLE;
980 ienab_bis(IE_FDSOFT);
981 printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
982 break;
983 }
984
985 if (msr & NE7_DIO) {
986 #ifdef DIAGNOSTIC
987 if (!read)
988 printf("fdxfer: false read\n");
989 #endif
990 *fdc->sc_data++ = *fdc->sc_reg_fifo;
991 } else {
992 #ifdef DIAGNOSTIC
993 if (read)
994 printf("fdxfer: false write\n");
995 #endif
996 *fdc->sc_reg_fifo = *fdc->sc_data++;
997 }
998 if (--fdc->sc_tc == 0) {
999 auxregbisc(AUXIO_FTC, 0);
1000 fdc->sc_istate = ISTATE_IDLE;
1001 delay(10);
1002 auxregbisc(0, AUXIO_FTC);
1003 fdcresult(fdc);
1004 ienab_bis(IE_FDSOFT);
1005 break;
1006 }
1007 }
1008 return 1;
1009 }
1010 #endif
1011
1012 int
1013 fdcswintr(fdc)
1014 struct fdc_softc *fdc;
1015 {
1016 #define st0 fdc->sc_status[0]
1017 #define st1 fdc->sc_status[1]
1018 #define cyl fdc->sc_status[1]
1019 #define OUT_FDC(fdc, c, s) \
1020 do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
1021
1022 struct fd_softc *fd;
1023 struct buf *bp;
1024 int read, head, trac, sec, i, s, nblks;
1025 struct fd_type *type;
1026
1027
1028 if (fdc->sc_istate != ISTATE_IDLE) {
1029 /* Trouble... */
1030 printf("fdc: spurious interrupt: state %d, istate=%d\n",
1031 fdc->sc_state, fdc->sc_istate);
1032 fdc->sc_istate = ISTATE_IDLE;
1033 if (fdc->sc_state == RESETCOMPLETE ||
1034 fdc->sc_state == RESETTIMEDOUT) {
1035 panic("fdcintr: spurious interrupt can't be cleared");
1036 }
1037 goto doreset;
1038 }
1039
1040 loop:
1041 /* Is there a drive for the controller to do a transfer with? */
1042 fd = fdc->sc_drives.tqh_first;
1043 if (fd == NULL) {
1044 fdc->sc_state = DEVIDLE;
1045 return 0;
1046 }
1047
1048 /* Is there a transfer to this drive? If not, deactivate drive. */
1049 bp = fd->sc_q.b_actf;
1050 if (bp == NULL) {
1051 fd->sc_ops = 0;
1052 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1053 fd->sc_q.b_active = 0;
1054 goto loop;
1055 }
1056
1057 switch (fdc->sc_state) {
1058 case DEVIDLE:
1059 fdc->sc_errors = 0;
1060 fd->sc_skip = 0;
1061 fd->sc_bcount = bp->b_bcount;
1062 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1063 untimeout(fd_motor_off, fd);
1064 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1065 fdc->sc_state = MOTORWAIT;
1066 return 1;
1067 }
1068 if ((fd->sc_flags & FD_MOTOR) == 0) {
1069 /* Turn on the motor, being careful about pairing. */
1070 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1071 if (ofd && ofd->sc_flags & FD_MOTOR) {
1072 untimeout(fd_motor_off, ofd);
1073 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1074 }
1075 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1076 fd_set_motor(fdc);
1077 fdc->sc_state = MOTORWAIT;
1078 if (fdc->sc_flags & FDC_82077) { /* XXX */
1079 /* Allow .25s for motor to stabilize. */
1080 timeout(fd_motor_on, fd, hz / 4);
1081 } else {
1082 fd->sc_flags &= ~FD_MOTOR_WAIT;
1083 goto loop;
1084 }
1085 return 1;
1086 }
1087 /* Make sure the right drive is selected. */
1088 fd_set_motor(fdc);
1089
1090 /* fall through */
1091 case DOSEEK:
1092 doseek:
1093 if (fdc->sc_flags & FDC_EIS) {
1094 fd->sc_cylin = bp->b_cylin;
1095 /* We use implied seek */
1096 goto doio;
1097 }
1098
1099 if (fd->sc_cylin == bp->b_cylin)
1100 goto doio;
1101
1102 /* specify command */
1103 OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
1104 OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
1105 OUT_FDC(fdc, 6, SEEKTIMEDOUT); /* XXX head load time == 6ms */
1106
1107 fdc->sc_istate = ISTATE_SENSEI;
1108 /* seek function */
1109 OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
1110 OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
1111 OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
1112
1113 fd->sc_cylin = -1;
1114 fdc->sc_state = SEEKWAIT;
1115 fdc->sc_nstat = 0;
1116
1117 fd->sc_dk.dk_seek++;
1118 disk_busy(&fd->sc_dk);
1119
1120 timeout(fdctimeout, fdc, 4 * hz);
1121 return 1;
1122
1123 case DOIO:
1124 doio:
1125 type = fd->sc_type;
1126 sec = fd->sc_blkno % type->seccyl;
1127 nblks = type->seccyl - sec;
1128 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1129 nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
1130 fd->sc_nblks = nblks;
1131 fd->sc_nbytes = nblks * FDC_BSIZE;
1132 head = sec / type->sectrac;
1133 sec -= head * type->sectrac;
1134 #ifdef DIAGNOSTIC
1135 {int block;
1136 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1137 if (block != fd->sc_blkno) {
1138 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1139 #ifdef DDB
1140 Debugger();
1141 #endif
1142 }}
1143 #endif
1144 read = bp->b_flags & B_READ;
1145
1146 /* Setup for pseudo DMA */
1147 fdc->sc_data = bp->b_data + fd->sc_skip;
1148 fdc->sc_tc = fd->sc_nbytes;
1149
1150 *fdc->sc_reg_drs = type->rate;
1151 #ifdef FD_DEBUG
1152 if (fdc_debug > 1)
1153 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1154 read ? "read" : "write", fd->sc_drive,
1155 fd->sc_cylin, head, sec, nblks);
1156 #endif
1157 fdc->sc_state = IOCOMPLETE;
1158 fdc->sc_istate = ISTATE_DMA;
1159 fdc->sc_nstat = 0;
1160 if (read)
1161 OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT); /* READ */
1162 else
1163 OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT); /* WRITE */
1164 OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
1165 OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT); /* track */
1166 OUT_FDC(fdc, head, IOTIMEDOUT);
1167 OUT_FDC(fdc, sec + 1, IOTIMEDOUT); /* sector +1 */
1168 OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/* sector size */
1169 OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/* sectors/track */
1170 OUT_FDC(fdc, type->gap1, IOTIMEDOUT); /* gap1 size */
1171 OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/* data length */
1172
1173 disk_busy(&fd->sc_dk);
1174
1175 /* allow 2 seconds for operation */
1176 timeout(fdctimeout, fdc, 2 * hz);
1177 return 1; /* will return later */
1178
1179 case SEEKWAIT:
1180 untimeout(fdctimeout, fdc);
1181 fdc->sc_state = SEEKCOMPLETE;
1182 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1183 /* allow 1/50 second for heads to settle */
1184 timeout(fdcpseudointr, fdc, hz / 50);
1185 return 1; /* will return later */
1186 }
1187
1188 case SEEKCOMPLETE:
1189 disk_unbusy(&fd->sc_dk, 0); /* no data on seek */
1190
1191 /* Make sure seek really happened. */
1192 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1193 cyl != bp->b_cylin * fd->sc_type->step) {
1194 #ifdef FD_DEBUG
1195 if (fdc_debug)
1196 fdcstatus(&fd->sc_dv, 2, "seek failed");
1197 #endif
1198 fdcretry(fdc);
1199 goto loop;
1200 }
1201 fd->sc_cylin = bp->b_cylin;
1202 goto doio;
1203
1204 case IOTIMEDOUT:
1205 auxregbisc(AUXIO_FTC, 0);
1206 delay(10);
1207 auxregbisc(0, AUXIO_FTC);
1208 (void)fdcresult(fdc);
1209 case SEEKTIMEDOUT:
1210 case RECALTIMEDOUT:
1211 case RESETTIMEDOUT:
1212 fdcretry(fdc);
1213 goto loop;
1214
1215 case IOCOMPLETE: /* IO DONE, post-analyze */
1216 untimeout(fdctimeout, fdc);
1217
1218 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1219
1220 if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
1221 #ifdef FD_DEBUG
1222 if (fdc_debug) {
1223 fdcstatus(&fd->sc_dv, 7,
1224 bp->b_flags & B_READ
1225 ? "read failed" : "write failed");
1226 printf("blkno %d nblks %d tc %d\n",
1227 fd->sc_blkno, fd->sc_nblks, fdc->sc_tc);
1228 }
1229 #endif
1230 if (fdc->sc_nstat == 7 &&
1231 (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1232
1233 /*
1234 * Silently retry overruns if no other
1235 * error bit is set. Adjust threshold.
1236 */
1237 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1238 if (thr < 15) {
1239 thr++;
1240 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1241 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1242 #ifdef FD_DEBUG
1243 if (fdc_debug)
1244 printf("fdc: %d -> threshold\n", thr);
1245 #endif
1246 fdconf(fdc);
1247 fdc->sc_state = DOIO;
1248 fdc->sc_overruns = 0;
1249 }
1250 if (++fdc->sc_overruns < 3)
1251 goto loop;
1252 }
1253 fdcretry(fdc);
1254 goto loop;
1255 }
1256 if (fdc->sc_errors) {
1257 diskerr(bp, "fd", "soft error", LOG_PRINTF,
1258 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1259 printf("\n");
1260 fdc->sc_errors = 0;
1261 } else {
1262 if (--fdc->sc_overruns < -20) {
1263 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1264 if (thr > 0) {
1265 thr--;
1266 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1267 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1268 #ifdef FD_DEBUG
1269 if (fdc_debug)
1270 printf("fdc: %d -> threshold\n", thr);
1271 #endif
1272 fdconf(fdc);
1273 }
1274 fdc->sc_overruns = 0;
1275 }
1276 }
1277 fd->sc_blkno += fd->sc_nblks;
1278 fd->sc_skip += fd->sc_nbytes;
1279 fd->sc_bcount -= fd->sc_nbytes;
1280 if (fd->sc_bcount > 0) {
1281 bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
1282 goto doseek;
1283 }
1284 fdfinish(fd, bp);
1285 goto loop;
1286
1287 case DORESET:
1288 doreset:
1289 /* try a reset, keep motor on */
1290 fd_set_motor(fdc);
1291 delay(100);
1292 fdc_reset(fdc);
1293 fdc->sc_nstat = 0;
1294 fdc->sc_istate = ISTATE_SENSEI;
1295 fdc->sc_state = RESETCOMPLETE;
1296 timeout(fdctimeout, fdc, hz / 2);
1297 return 1; /* will return later */
1298
1299 case RESETCOMPLETE:
1300 untimeout(fdctimeout, fdc);
1301 fdconf(fdc);
1302
1303 /* fall through */
1304 case DORECAL:
1305 fdc->sc_state = RECALWAIT;
1306 fdc->sc_istate = ISTATE_SENSEI;
1307 fdc->sc_nstat = 0;
1308 /* recalibrate function */
1309 OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
1310 OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
1311 timeout(fdctimeout, fdc, 5 * hz);
1312 return 1; /* will return later */
1313
1314 case RECALWAIT:
1315 untimeout(fdctimeout, fdc);
1316 fdc->sc_state = RECALCOMPLETE;
1317 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1318 /* allow 1/30 second for heads to settle */
1319 timeout(fdcpseudointr, fdc, hz / 30);
1320 return 1; /* will return later */
1321 }
1322
1323 case RECALCOMPLETE:
1324 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1325 #ifdef FD_DEBUG
1326 if (fdc_debug)
1327 fdcstatus(&fd->sc_dv, 2, "recalibrate failed");
1328 #endif
1329 fdcretry(fdc);
1330 goto loop;
1331 }
1332 fd->sc_cylin = 0;
1333 goto doseek;
1334
1335 case MOTORWAIT:
1336 if (fd->sc_flags & FD_MOTOR_WAIT)
1337 return 1; /* time's not up yet */
1338 goto doseek;
1339
1340 default:
1341 fdcstatus(&fd->sc_dv, 0, "stray interrupt");
1342 return 1;
1343 }
1344 #ifdef DIAGNOSTIC
1345 panic("fdcintr: impossible");
1346 #endif
1347 #undef st0
1348 #undef st1
1349 #undef cyl
1350 }
1351
1352 void
1353 fdcretry(fdc)
1354 struct fdc_softc *fdc;
1355 {
1356 struct fd_softc *fd;
1357 struct buf *bp;
1358
1359 fd = fdc->sc_drives.tqh_first;
1360 bp = fd->sc_q.b_actf;
1361
1362 fdc->sc_overruns = 0;
1363
1364 switch (fdc->sc_errors) {
1365 case 0:
1366 /* try again */
1367 fdc->sc_state =
1368 (fdc->sc_flags & FDC_EIS) ? DOIO : SEEKCOMPLETE;
1369 break;
1370
1371 case 1: case 2: case 3:
1372 /* didn't work; try recalibrating */
1373 fdc->sc_state = DORECAL;
1374 break;
1375
1376 case 4:
1377 /* still no go; reset the bastard */
1378 fdc->sc_state = DORESET;
1379 break;
1380
1381 default:
1382 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1383 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1384 printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
1385 fdc->sc_status[0], NE7_ST0BITS,
1386 fdc->sc_status[1], NE7_ST1BITS,
1387 fdc->sc_status[2], NE7_ST2BITS,
1388 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1389
1390 bp->b_flags |= B_ERROR;
1391 bp->b_error = EIO;
1392 fdfinish(fd, bp);
1393 }
1394 fdc->sc_errors++;
1395 }
1396
1397 int
1398 fdsize(dev)
1399 dev_t dev;
1400 {
1401
1402 /* Swapping to floppies would not make sense. */
1403 return -1;
1404 }
1405
1406 int
1407 fddump()
1408 {
1409
1410 /* Not implemented. */
1411 return EINVAL;
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 = fdcd.cd_devs[FDUNIT(dev)];
1422 struct disklabel buffer;
1423 int error;
1424
1425 switch (cmd) {
1426 case DIOCGDINFO:
1427 bzero(fd->sc_dk.dk_label, sizeof(struct disklabel));
1428
1429 fd->sc_dk.dk_label->d_secpercyl = fd->sc_type->seccyl;
1430 fd->sc_dk.dk_label->d_type = DTYPE_FLOPPY;
1431 fd->sc_dk.dk_label->d_secsize = FDC_BSIZE;
1432
1433 if (readdisklabel(dev, fdstrategy,
1434 fd->sc_dk.dk_label,
1435 fd->sc_dk.dk_cpulabel) != NULL)
1436 return EINVAL;
1437
1438 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1439 return 0;
1440
1441 case DIOCWLABEL:
1442 if ((flag & FWRITE) == 0)
1443 return EBADF;
1444 /* XXX do something */
1445 return 0;
1446
1447 case DIOCWDINFO:
1448 if ((flag & FWRITE) == 0)
1449 return EBADF;
1450
1451 error = setdisklabel(fd->sc_dk.dk_label,
1452 (struct disklabel *)addr, 0,
1453 fd->sc_dk.dk_cpulabel);
1454 if (error)
1455 return error;
1456
1457 error = writedisklabel(dev, fdstrategy,
1458 fd->sc_dk.dk_label,
1459 fd->sc_dk.dk_cpulabel);
1460 return error;
1461
1462 case DIOCEJECT:
1463 auxregbisc(AUXIO_FDS, AUXIO_FEJ);
1464 delay(10);
1465 auxregbisc(AUXIO_FEJ, AUXIO_FDS);
1466 return 0;
1467
1468 #ifdef DEBUG
1469 case _IO('f', 100):
1470 {
1471 int i;
1472 struct fdc_softc *fdc = (struct fdc_softc *)
1473 fd->sc_dv.dv_parent;
1474
1475 out_fdc(fdc, NE7CMD_DUMPREG);
1476 fdcresult(fdc);
1477 printf("dumpreg(%d regs): <", fdc->sc_nstat);
1478 for (i = 0; i < fdc->sc_nstat; i++)
1479 printf(" %x", fdc->sc_status[i]);
1480 printf(">\n");
1481 }
1482
1483 return 0;
1484 case _IOW('f', 101, int):
1485 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg &=
1486 ~CFG_THRHLD_MASK;
1487 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg |=
1488 (*(int *)addr & CFG_THRHLD_MASK);
1489 fdconf(fd->sc_dv.dv_parent);
1490 return 0;
1491 case _IO('f', 102):
1492 {
1493 int i;
1494 struct fdc_softc *fdc = (struct fdc_softc *)
1495 fd->sc_dv.dv_parent;
1496 out_fdc(fdc, NE7CMD_SENSEI);
1497 fdcresult(fdc);
1498 printf("sensei(%d regs): <", fdc->sc_nstat);
1499 for (i=0; i< fdc->sc_nstat; i++)
1500 printf(" 0x%x", fdc->sc_status[i]);
1501 }
1502 printf(">\n");
1503 return 0;
1504 #endif
1505 default:
1506 return ENOTTY;
1507 }
1508
1509 #ifdef DIAGNOSTIC
1510 panic("fdioctl: impossible");
1511 #endif
1512 }
1513