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