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