fd.c revision 1.33.4.1 1 /* $NetBSD: fd.c,v 1.33.4.1 1996/06/12 20:52:25 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/file.h>
47 #include <sys/ioctl.h>
48 #include <sys/device.h>
49 #include <sys/disklabel.h>
50 #include <sys/dkstat.h>
51 #include <sys/disk.h>
52 #include <sys/buf.h>
53 #include <sys/uio.h>
54 #include <sys/stat.h>
55 #include <sys/syslog.h>
56 #include <sys/queue.h>
57 #include <sys/conf.h>
58
59 #include <dev/cons.h>
60
61 #include <machine/cpu.h>
62 #include <machine/autoconf.h>
63 #include <machine/conf.h>
64
65 #include <sparc/sparc/auxreg.h>
66 #include <sparc/dev/fdreg.h>
67 #include <sparc/dev/fdvar.h>
68
69 #define FDUNIT(dev) (minor(dev) / 8)
70 #define FDTYPE(dev) (minor(dev) % 8)
71
72 #define b_cylin b_resid
73
74 #define FD_DEBUG
75 #ifdef FD_DEBUG
76 int fdc_debug = 0;
77 #endif
78
79 enum fdc_state {
80 DEVIDLE = 0,
81 MOTORWAIT,
82 DOSEEK,
83 SEEKWAIT,
84 SEEKTIMEDOUT,
85 SEEKCOMPLETE,
86 DOIO,
87 IOCOMPLETE,
88 IOTIMEDOUT,
89 DORESET,
90 RESETCOMPLETE,
91 RESETTIMEDOUT,
92 DORECAL,
93 RECALWAIT,
94 RECALTIMEDOUT,
95 RECALCOMPLETE,
96 };
97
98 /* software state, per controller */
99 struct fdc_softc {
100 struct device sc_dev; /* boilerplate */
101 struct intrhand sc_sih;
102 struct intrhand sc_hih;
103 caddr_t sc_reg;
104 struct fd_softc *sc_fd[4]; /* pointers to children */
105 TAILQ_HEAD(drivehead, fd_softc) sc_drives;
106 enum fdc_state sc_state;
107 int sc_flags;
108 #define FDC_82077 0x01
109 #define FDC_NEEDHEADSETTLE 0x02
110 #define FDC_EIS 0x04
111 int sc_errors; /* number of retries so far */
112 int sc_overruns; /* number of DMA overruns */
113 int sc_cfg; /* current configuration */
114 struct fdcio sc_io;
115 #define sc_reg_msr sc_io.fdcio_reg_msr
116 #define sc_reg_fifo sc_io.fdcio_reg_fifo
117 #define sc_reg_dor sc_io.fdcio_reg_dor
118 #define sc_reg_drs sc_io.fdcio_reg_msr
119 #define sc_istate sc_io.fdcio_istate
120 #define sc_data sc_io.fdcio_data
121 #define sc_tc sc_io.fdcio_tc
122 #define sc_nstat sc_io.fdcio_nstat
123 #define sc_status sc_io.fdcio_status
124 #define sc_intrcnt sc_io.fdcio_intrcnt
125 };
126
127 #ifndef FDC_C_HANDLER
128 extern struct fdcio *fdciop;
129 #endif
130
131 /* controller driver configuration */
132 int fdcmatch __P((struct device *, void *, void *));
133 void fdcattach __P((struct device *, struct device *, void *));
134
135 struct cfattach fdc_ca = {
136 sizeof(struct fdc_softc), fdcmatch, fdcattach
137 };
138
139 struct cfdriver fdc_cd = {
140 NULL, "fdc", DV_DULL
141 };
142
143 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
144
145 /*
146 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
147 * we tell them apart.
148 */
149 struct fd_type {
150 int sectrac; /* sectors per track */
151 int heads; /* number of heads */
152 int seccyl; /* sectors per cylinder */
153 int secsize; /* size code for sectors */
154 int datalen; /* data len when secsize = 0 */
155 int steprate; /* step rate and head unload time */
156 int gap1; /* gap len between sectors */
157 int gap2; /* formatting gap */
158 int tracks; /* total num of tracks */
159 int size; /* size of disk in sectors */
160 int step; /* steps per cylinder */
161 int rate; /* transfer speed code */
162 char *name;
163 };
164
165 /* The order of entries in the following table is important -- BEWARE! */
166 struct fd_type fd_types[] = {
167 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" }, /* 1.44MB diskette */
168 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,"1.2MB" }, /* 1.2 MB AT-diskettes */
169 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,"360KB/AT" }, /* 360kB in 1.2MB drive */
170 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,"360KB/PC" }, /* 360kB PC diskettes */
171 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,"720KB" }, /* 3.5" 720kB diskette */
172 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,"720KB/x" }, /* 720kB in 1.2MB drive */
173 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,"360KB/x" }, /* 360kB in 720kB drive */
174 };
175
176 /* software state, per disk (with up to 4 disks per ctlr) */
177 struct fd_softc {
178 struct device sc_dv; /* generic device info */
179 struct disk sc_dk; /* generic disk info */
180
181 struct fd_type *sc_deftype; /* default type descriptor */
182 struct fd_type *sc_type; /* current type descriptor */
183
184 daddr_t sc_blkno; /* starting block number */
185 int sc_bcount; /* byte count left */
186 int sc_skip; /* bytes already transferred */
187 int sc_nblks; /* number of blocks currently tranferring */
188 int sc_nbytes; /* number of bytes currently tranferring */
189
190 int sc_drive; /* physical unit number */
191 int sc_flags;
192 #define FD_OPEN 0x01 /* it's open */
193 #define FD_MOTOR 0x02 /* motor should be on */
194 #define FD_MOTOR_WAIT 0x04 /* motor coming up */
195 int sc_cylin; /* where we think the head is */
196
197 void *sc_sdhook; /* shutdownhook cookie */
198
199 TAILQ_ENTRY(fd_softc) sc_drivechain;
200 int sc_ops; /* I/O ops since last switch */
201 struct buf sc_q; /* head of buf chain */
202 };
203
204 /* floppy driver configuration */
205 int fdmatch __P((struct device *, void *, void *));
206 void fdattach __P((struct device *, struct device *, void *));
207
208 struct cfattach fd_ca = {
209 sizeof(struct fd_softc), fdmatch, fdattach
210 };
211
212 struct cfdriver fd_cd = {
213 NULL, "fd", DV_DISK
214 };
215
216 void fdgetdisklabel __P((dev_t));
217 int fd_get_parms __P((struct fd_softc *));
218 void fdstrategy __P((struct buf *));
219 void fdstart __P((struct fd_softc *));
220 int fdprint __P((void *, char *));
221
222 struct dkdriver fddkdriver = { fdstrategy };
223
224 struct fd_type *fd_nvtotype __P((char *, int, int));
225 void fd_set_motor __P((struct fdc_softc *fdc));
226 void fd_motor_off __P((void *arg));
227 void fd_motor_on __P((void *arg));
228 int fdcresult __P((struct fdc_softc *fdc));
229 int out_fdc __P((struct fdc_softc *fdc, u_char x));
230 void fdcstart __P((struct fdc_softc *fdc));
231 void fdcstatus __P((struct device *dv, int n, char *s));
232 void fdc_reset __P((struct fdc_softc *fdc));
233 void fdctimeout __P((void *arg));
234 void fdcpseudointr __P((void *arg));
235 #ifdef FDC_C_HANDLER
236 int fdchwintr __P((struct fdc_softc *));
237 #else
238 void fdchwintr __P((void));
239 #endif
240 int fdcswintr __P((struct fdc_softc *));
241 void fdcretry __P((struct fdc_softc *fdc));
242 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
243 void fd_do_eject __P((void));
244 void fd_mountroot_hook __P((struct device *));
245 static void fdconf __P((struct fdc_softc *));
246
247 #if PIL_FDSOFT == 4
248 #define IE_FDSOFT IE_L4
249 #else
250 #error 4
251 #endif
252
253 #define OBP_FDNAME (CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
254
255 int
256 fdcmatch(parent, match, aux)
257 struct device *parent;
258 void *match, *aux;
259 {
260 register struct confargs *ca = aux;
261 register struct romaux *ra = &ca->ca_ra;
262
263 /*
264 * Floppy doesn't exist on sun4.
265 */
266 if (CPU_ISSUN4)
267 return (0);
268
269 /*
270 * Floppy controller is on mainbus on sun4c.
271 */
272 if ((CPU_ISSUN4C) && (ca->ca_bustype != BUS_MAIN))
273 return (0);
274
275 /*
276 * Floppy controller is on obio on sun4m.
277 */
278 if ((CPU_ISSUN4M) && (ca->ca_bustype != BUS_OBIO))
279 return (0);
280
281 /* Sun PROMs call the controller an "fd" or "SUNW,fdtwo" */
282 if (strcmp(OBP_FDNAME, ra->ra_name))
283 return (0);
284
285 if (ca->ca_ra.ra_vaddr &&
286 probeget(ca->ca_ra.ra_vaddr, 1) == -1) {
287 return (0);
288 }
289
290 return (1);
291 }
292
293 /*
294 * Arguments passed between fdcattach and fdprobe.
295 */
296 struct fdc_attach_args {
297 int fa_drive;
298 struct bootpath *fa_bootpath;
299 struct fd_type *fa_deftype;
300 };
301
302 /*
303 * Print the location of a disk drive (called just before attaching the
304 * the drive). If `fdc' is not NULL, the drive was found but was not
305 * in the system config file; print the drive name as well.
306 * Return QUIET (config_find ignores this if the device was configured) to
307 * avoid printing `fdN not configured' messages.
308 */
309 int
310 fdprint(aux, fdc)
311 void *aux;
312 char *fdc;
313 {
314 register struct fdc_attach_args *fa = aux;
315
316 if (!fdc)
317 printf(" drive %d", fa->fa_drive);
318 return QUIET;
319 }
320
321 static void
322 fdconf(fdc)
323 struct fdc_softc *fdc;
324 {
325 int vroom;
326
327 if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
328 return;
329
330 /*
331 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
332 * the PROM thinks is appropriate.
333 */
334 if ((vroom = fdc->sc_status[7]) == 0)
335 vroom = 0x64;
336
337 /* Configure controller to use FIFO and Implied Seek */
338 out_fdc(fdc, NE7CMD_CFG);
339 out_fdc(fdc, vroom);
340 out_fdc(fdc, fdc->sc_cfg);
341 out_fdc(fdc, 0); /* PRETRK */
342 /* No result phase */
343 }
344
345 void
346 fdcattach(parent, self, aux)
347 struct device *parent, *self;
348 void *aux;
349 {
350 register struct confargs *ca = aux;
351 struct fdc_softc *fdc = (void *)self;
352 struct fdc_attach_args fa;
353 struct bootpath *bp;
354 int pri;
355 char code;
356
357 if (ca->ca_ra.ra_vaddr)
358 fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr;
359 else
360 fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_reg, 0,
361 ca->ca_ra.ra_len,
362 ca->ca_bustype);
363
364 fdc->sc_state = DEVIDLE;
365 fdc->sc_istate = ISTATE_IDLE;
366 fdc->sc_flags |= FDC_EIS;
367 TAILQ_INIT(&fdc->sc_drives);
368
369 pri = ca->ca_ra.ra_intr[0].int_pri;
370 #ifdef FDC_C_HANDLER
371 fdc->sc_hih.ih_fun = (void *)fdchwintr;
372 fdc->sc_hih.ih_arg = fdc;
373 intr_establish(pri, &fdc->sc_hih);
374 #else
375 fdciop = &fdc->sc_io;
376 intr_fasttrap(pri, fdchwintr);
377 #endif
378 fdc->sc_sih.ih_fun = (void *)fdcswintr;
379 fdc->sc_sih.ih_arg = fdc;
380 intr_establish(PIL_FDSOFT, &fdc->sc_sih);
381
382 /* Assume a 82077 */
383 fdc->sc_reg_msr = &((struct fdreg_77 *)fdc->sc_reg)->fd_msr;
384 fdc->sc_reg_fifo = &((struct fdreg_77 *)fdc->sc_reg)->fd_fifo;
385 fdc->sc_reg_dor = &((struct fdreg_77 *)fdc->sc_reg)->fd_dor;
386
387 code = '7';
388 if (*fdc->sc_reg_dor == NE7_RQM) {
389 /*
390 * This hack from Chris Torek: apparently DOR really
391 * addresses MSR/DRS on a 82072.
392 * We used to rely on the VERSION command to tell the
393 * difference (which did not work).
394 */
395 *fdc->sc_reg_dor = FDC_250KBPS;
396 if (*fdc->sc_reg_dor == NE7_RQM)
397 code = '2';
398 }
399 if (code == '7') {
400 fdc->sc_flags |= FDC_82077;
401 } else {
402 fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr;
403 fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo;
404 fdc->sc_reg_dor = 0;
405 }
406
407 #ifdef FD_DEBUG
408 if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
409 fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
410 if (fdc_debug)
411 printf("[version cmd]");
412 }
413 #endif
414
415 /*
416 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
417 * Note: CFG_EFIFO is active-low, initial threshold value: 8
418 */
419 fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
420 fdconf(fdc);
421
422 if (fdc->sc_flags & FDC_82077) {
423 /* Lock configuration across soft resets. */
424 out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
425 if (fdcresult(fdc) != 1)
426 printf(" CFGLOCK: unexpected response");
427 }
428
429 evcnt_attach(&fdc->sc_dev, "intr", &fdc->sc_intrcnt);
430
431 printf(" pri %d, softpri %d: chip 8207%c\n", pri, PIL_FDSOFT, code);
432
433 /*
434 * Controller and drives are represented by one and the same
435 * Openprom node, so we can as well check for the floppy boots here.
436 */
437 fa.fa_bootpath = 0;
438 if ((bp = ca->ca_ra.ra_bp) && strcmp(bp->name, OBP_FDNAME) == 0) {
439 /*
440 * WOAH THERE! It looks like we can get the bootpath
441 * in several different formats!! The faked
442 * bootpath (and some v2?) looks like /fd@0,0
443 * but the real bootpath on some v2 OpenPROM
444 * systems looks like /fd0. In the case of
445 * a floppy controller on obio (such as on the sun4m),
446 * we use "slot, offset" to determine if this is the
447 * right one. --thorpej
448 */
449 switch (ca->ca_bustype) {
450 case BUS_MAIN:
451 if (((bp->val[0] == 0) && /* /fd@0,0 */
452 (bp->val[1] == 0)) ||
453 ((bp->val[0] == -1) && /* /fd0 */
454 (bp->val[1] == 0)))
455 fa.fa_bootpath = bp;
456 break;
457
458 case BUS_OBIO:
459 /* /obio0/SUNW,fdtwo@0,700000 */
460 if ((bp->val[0] == ca->ca_slot) &&
461 (bp->val[1] == ca->ca_offset))
462 fa.fa_bootpath = bp;
463 break;
464 }
465
466 }
467
468 /* physical limit: four drives per controller. */
469 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
470 fa.fa_deftype = NULL; /* unknown */
471 fa.fa_deftype = &fd_types[0]; /* XXX */
472 (void)config_found(self, (void *)&fa, fdprint);
473 }
474
475 bootpath_store(1, NULL);
476 }
477
478 int
479 fdmatch(parent, match, aux)
480 struct device *parent;
481 void *match, *aux;
482 {
483 struct fdc_softc *fdc = (void *)parent;
484 struct fdc_attach_args *fa = aux;
485 int drive = fa->fa_drive;
486 int n, ok;
487
488 if (drive > 0)
489 /* XXX - for now, punt > 1 drives */
490 return 0;
491
492 if (fdc->sc_flags & FDC_82077) {
493 /* select drive and turn on motor */
494 *fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
495 /* wait for motor to spin up */
496 delay(250000);
497 } else {
498 auxregbisc(AUXIO_FDS, 0);
499 }
500 fdc->sc_nstat = 0;
501 out_fdc(fdc, NE7CMD_RECAL);
502 out_fdc(fdc, drive);
503 /* wait for recalibrate */
504 for (n = 0; n < 10000; n++) {
505 delay(1000);
506 if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
507 /* wait a bit longer till device *really* is ready */
508 delay(100000);
509 if (out_fdc(fdc, NE7CMD_SENSEI))
510 break;
511 if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
512 /*
513 * Got `invalid command'; we interpret it
514 * to mean that the re-calibrate hasn't in
515 * fact finished yet
516 */
517 continue;
518 break;
519 }
520 }
521 n = fdc->sc_nstat;
522 #ifdef FD_DEBUG
523 if (fdc_debug) {
524 int i;
525 printf("fdprobe: %d stati:", n);
526 for (i = 0; i < n; i++)
527 printf(" %x", fdc->sc_status[i]);
528 printf("\n");
529 }
530 #endif
531 ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
532
533 /* turn off motor */
534 if (fdc->sc_flags & FDC_82077) {
535 /* select drive and turn on motor */
536 *fdc->sc_reg_dor = FDO_FRST;
537 } else {
538 auxregbisc(0, AUXIO_FDS);
539 }
540
541 return ok;
542 }
543
544 /*
545 * Controller is working, and drive responded. Attach it.
546 */
547 void
548 fdattach(parent, self, aux)
549 struct device *parent, *self;
550 void *aux;
551 {
552 struct fdc_softc *fdc = (void *)parent;
553 struct fd_softc *fd = (void *)self;
554 struct fdc_attach_args *fa = aux;
555 struct fd_type *type = fa->fa_deftype;
556 int drive = fa->fa_drive;
557
558 /* XXX Allow `flags' to override device type? */
559
560 if (type)
561 printf(": %s %d cyl, %d head, %d sec\n", type->name,
562 type->tracks, type->heads, type->sectrac);
563 else
564 printf(": density unknown\n");
565
566 fd->sc_cylin = -1;
567 fd->sc_drive = drive;
568 fd->sc_deftype = type;
569 fdc->sc_fd[drive] = fd;
570
571 /*
572 * Initialize and attach the disk structure.
573 */
574 fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
575 fd->sc_dk.dk_driver = &fddkdriver;
576 disk_attach(&fd->sc_dk);
577
578 /*
579 * We're told if we're the boot device in fdcattach().
580 */
581 if (fa->fa_bootpath)
582 fa->fa_bootpath->dev = &fd->sc_dv;
583
584 /*
585 * Establish a mountroot_hook anyway in case we booted
586 * with RB_ASKNAME and get selected as the boot device.
587 */
588 mountroot_hook_establish(fd_mountroot_hook, &fd->sc_dv);
589
590 /* Make sure the drive motor gets turned off at shutdown time. */
591 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
592
593 /* XXX Need to do some more fiddling with sc_dk. */
594 dk_establish(&fd->sc_dk, &fd->sc_dv);
595 }
596
597 __inline struct fd_type *
598 fd_dev_to_type(fd, dev)
599 struct fd_softc *fd;
600 dev_t dev;
601 {
602 int type = FDTYPE(dev);
603
604 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
605 return NULL;
606 return type ? &fd_types[type - 1] : fd->sc_deftype;
607 }
608
609 void
610 fdstrategy(bp)
611 register struct buf *bp; /* IO operation to perform */
612 {
613 struct fd_softc *fd;
614 int unit = FDUNIT(bp->b_dev);
615 int sz;
616 int s;
617
618 /* Valid unit, controller, and request? */
619 if (unit >= fd_cd.cd_ndevs ||
620 (fd = fd_cd.cd_devs[unit]) == 0 ||
621 bp->b_blkno < 0 ||
622 (bp->b_bcount % FDC_BSIZE) != 0) {
623 bp->b_error = EINVAL;
624 goto bad;
625 }
626
627 /* If it's a null transfer, return immediately. */
628 if (bp->b_bcount == 0)
629 goto done;
630
631 sz = howmany(bp->b_bcount, FDC_BSIZE);
632
633 if (bp->b_blkno + sz > fd->sc_type->size) {
634 sz = fd->sc_type->size - bp->b_blkno;
635 if (sz == 0) {
636 /* If exactly at end of disk, return EOF. */
637 bp->b_resid = bp->b_bcount;
638 goto done;
639 }
640 if (sz < 0) {
641 /* If past end of disk, return EINVAL. */
642 bp->b_error = EINVAL;
643 goto bad;
644 }
645 /* Otherwise, truncate request. */
646 bp->b_bcount = sz << DEV_BSHIFT;
647 }
648
649 bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
650
651 #ifdef FD_DEBUG
652 if (fdc_debug > 1)
653 printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n",
654 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
655 #endif
656
657 /* Queue transfer on drive, activate drive and controller if idle. */
658 s = splbio();
659 disksort(&fd->sc_q, bp);
660 untimeout(fd_motor_off, fd); /* a good idea */
661 if (!fd->sc_q.b_active)
662 fdstart(fd);
663 #ifdef DIAGNOSTIC
664 else {
665 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
666 if (fdc->sc_state == DEVIDLE) {
667 printf("fdstrategy: controller inactive\n");
668 fdcstart(fdc);
669 }
670 }
671 #endif
672 splx(s);
673 return;
674
675 bad:
676 bp->b_flags |= B_ERROR;
677 done:
678 /* Toss transfer; we're done early. */
679 biodone(bp);
680 }
681
682 void
683 fdstart(fd)
684 struct fd_softc *fd;
685 {
686 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
687 int active = fdc->sc_drives.tqh_first != 0;
688
689 /* Link into controller queue. */
690 fd->sc_q.b_active = 1;
691 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
692
693 /* If controller not already active, start it. */
694 if (!active)
695 fdcstart(fdc);
696 }
697
698 void
699 fdfinish(fd, bp)
700 struct fd_softc *fd;
701 struct buf *bp;
702 {
703 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
704
705 /*
706 * Move this drive to the end of the queue to give others a `fair'
707 * chance. We only force a switch if N operations are completed while
708 * another drive is waiting to be serviced, since there is a long motor
709 * startup delay whenever we switch.
710 */
711 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
712 fd->sc_ops = 0;
713 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
714 if (bp->b_actf) {
715 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
716 } else
717 fd->sc_q.b_active = 0;
718 }
719 bp->b_resid = fd->sc_bcount;
720 fd->sc_skip = 0;
721 fd->sc_q.b_actf = bp->b_actf;
722
723 biodone(bp);
724 /* turn off motor 5s from now */
725 timeout(fd_motor_off, fd, 5 * hz);
726 fdc->sc_state = DEVIDLE;
727 }
728
729 void
730 fdc_reset(fdc)
731 struct fdc_softc *fdc;
732 {
733 if (fdc->sc_flags & FDC_82077) {
734 *fdc->sc_reg_dor = FDO_MOEN(0);
735 }
736
737 *fdc->sc_reg_drs = DRS_RESET;
738 delay(10);
739 *fdc->sc_reg_drs = 0;
740 #ifdef FD_DEBUG
741 if (fdc_debug)
742 printf("fdc reset\n");
743 #endif
744 }
745
746 void
747 fd_set_motor(fdc)
748 struct fdc_softc *fdc;
749 {
750 struct fd_softc *fd;
751 u_char status;
752 int n;
753
754 if (fdc->sc_flags & FDC_82077) {
755 status = FDO_FRST | FDO_FDMAEN;
756 if ((fd = fdc->sc_drives.tqh_first) != NULL)
757 status |= fd->sc_drive;
758
759 for (n = 0; n < 4; n++)
760 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
761 status |= FDO_MOEN(n);
762 *fdc->sc_reg_dor = status;
763 } else {
764 int on = 0;
765
766 for (n = 0; n < 4; n++)
767 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
768 on = 1;
769 if (on) {
770 auxregbisc(AUXIO_FDS, 0);
771 } else {
772 auxregbisc(0, AUXIO_FDS);
773 }
774 }
775 }
776
777 void
778 fd_motor_off(arg)
779 void *arg;
780 {
781 struct fd_softc *fd = arg;
782 int s;
783
784 s = splbio();
785 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
786 fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
787 splx(s);
788 }
789
790 void
791 fd_motor_on(arg)
792 void *arg;
793 {
794 struct fd_softc *fd = arg;
795 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
796 int s;
797
798 s = splbio();
799 fd->sc_flags &= ~FD_MOTOR_WAIT;
800 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
801 (void) fdcswintr(fdc);
802 splx(s);
803 }
804
805 int
806 fdcresult(fdc)
807 struct fdc_softc *fdc;
808 {
809 u_char i;
810 int j = 100000,
811 n = 0;
812
813 for (; j; j--) {
814 i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
815 if (i == NE7_RQM)
816 return (fdc->sc_nstat = n);
817 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
818 if (n >= sizeof(fdc->sc_status)) {
819 log(LOG_ERR, "fdcresult: overrun\n");
820 return -1;
821 }
822 fdc->sc_status[n++] = *fdc->sc_reg_fifo;
823 }
824 }
825 log(LOG_ERR, "fdcresult: timeout\n");
826 return (fdc->sc_nstat = -1);
827 }
828
829 int
830 out_fdc(fdc, x)
831 struct fdc_softc *fdc;
832 u_char x;
833 {
834 int i = 100000;
835
836 while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0);
837 if (i <= 0)
838 return -1;
839
840 *fdc->sc_reg_fifo = x;
841 return 0;
842 }
843
844 int
845 fdopen(dev, flags, fmt, p)
846 dev_t dev;
847 int flags, fmt;
848 struct proc *p;
849 {
850 int unit, pmask;
851 struct fd_softc *fd;
852 struct fd_type *type;
853
854 unit = FDUNIT(dev);
855 if (unit >= fd_cd.cd_ndevs)
856 return ENXIO;
857 fd = fd_cd.cd_devs[unit];
858 if (fd == 0)
859 return ENXIO;
860 type = fd_dev_to_type(fd, dev);
861 if (type == NULL)
862 return ENXIO;
863
864 if ((fd->sc_flags & FD_OPEN) != 0 &&
865 fd->sc_type != type)
866 return EBUSY;
867
868 fd->sc_type = type;
869 fd->sc_cylin = -1;
870 fd->sc_flags |= FD_OPEN;
871
872 /*
873 * Only update the disklabel if we're not open anywhere else.
874 */
875 if (fd->sc_dk.dk_openmask == 0)
876 fdgetdisklabel(dev);
877
878 pmask = (1 << DISKPART(dev));
879
880 switch (fmt) {
881 case S_IFCHR:
882 fd->sc_dk.dk_copenmask |= pmask;
883 break;
884
885 case S_IFBLK:
886 fd->sc_dk.dk_bopenmask |= pmask;
887 break;
888 }
889 fd->sc_dk.dk_openmask =
890 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
891
892 return 0;
893 }
894
895 int
896 fdclose(dev, flags, fmt, p)
897 dev_t dev;
898 int flags, fmt;
899 struct proc *p;
900 {
901 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
902 int pmask = (1 << DISKPART(dev));
903
904 fd->sc_flags &= ~FD_OPEN;
905
906 switch (fmt) {
907 case S_IFCHR:
908 fd->sc_dk.dk_copenmask &= ~pmask;
909 break;
910
911 case S_IFBLK:
912 fd->sc_dk.dk_bopenmask &= ~pmask;
913 break;
914 }
915 fd->sc_dk.dk_openmask =
916 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
917
918 return 0;
919 }
920
921 int
922 fdread(dev, uio, flag)
923 dev_t dev;
924 struct uio *uio;
925 int flag;
926 {
927
928 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
929 }
930
931 int
932 fdwrite(dev, uio, flag)
933 dev_t dev;
934 struct uio *uio;
935 int flag;
936 {
937
938 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
939 }
940
941 void
942 fdcstart(fdc)
943 struct fdc_softc *fdc;
944 {
945
946 #ifdef DIAGNOSTIC
947 /* only got here if controller's drive queue was inactive; should
948 be in idle state */
949 if (fdc->sc_state != DEVIDLE) {
950 printf("fdcstart: not idle\n");
951 return;
952 }
953 #endif
954 (void) fdcswintr(fdc);
955 }
956
957 void
958 fdcstatus(dv, n, s)
959 struct device *dv;
960 int n;
961 char *s;
962 {
963 struct fdc_softc *fdc = (void *)dv->dv_parent;
964 #if 0
965 /*
966 * A 82072 seems to return <invalid command> on
967 * gratuitous Sense Interrupt commands.
968 */
969 if (n == 0 && (fdc->sc_flags & FDC_82077)) {
970 out_fdc(fdc, NE7CMD_SENSEI);
971 (void) fdcresult(fdc);
972 n = 2;
973 }
974 #endif
975
976 /* Just print last status */
977 n = fdc->sc_nstat;
978
979 printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
980
981 switch (n) {
982 case 0:
983 printf("\n");
984 break;
985 case 2:
986 printf(" (st0 %b cyl %d)\n",
987 fdc->sc_status[0], NE7_ST0BITS,
988 fdc->sc_status[1]);
989 break;
990 case 7:
991 printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
992 fdc->sc_status[0], NE7_ST0BITS,
993 fdc->sc_status[1], NE7_ST1BITS,
994 fdc->sc_status[2], NE7_ST2BITS,
995 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
996 break;
997 #ifdef DIAGNOSTIC
998 default:
999 printf(" fdcstatus: weird size: %d\n", n);
1000 break;
1001 #endif
1002 }
1003 }
1004
1005 void
1006 fdctimeout(arg)
1007 void *arg;
1008 {
1009 struct fdc_softc *fdc = arg;
1010 struct fd_softc *fd = fdc->sc_drives.tqh_first;
1011 int s;
1012
1013 s = splbio();
1014 fdcstatus(&fd->sc_dv, 0, "timeout");
1015
1016 if (fd->sc_q.b_actf)
1017 fdc->sc_state++;
1018 else
1019 fdc->sc_state = DEVIDLE;
1020
1021 (void) fdcswintr(fdc);
1022 splx(s);
1023 }
1024
1025 void
1026 fdcpseudointr(arg)
1027 void *arg;
1028 {
1029 struct fdc_softc *fdc = arg;
1030 int s;
1031
1032 /* Just ensure it has the right spl. */
1033 s = splbio();
1034 (void) fdcswintr(fdc);
1035 splx(s);
1036 }
1037
1038
1039 #ifdef FDC_C_HANDLER
1040 /*
1041 * hardware interrupt entry point: must be converted to `fast'
1042 * (in-window) handler.
1043 */
1044 int
1045 fdchwintr(fdc)
1046 struct fdc_softc *fdc;
1047 {
1048 struct buf *bp;
1049 int read;
1050
1051 switch (fdc->sc_istate) {
1052 case ISTATE_SENSEI:
1053 out_fdc(fdc, NE7CMD_SENSEI);
1054 fdcresult(fdc);
1055 fdc->sc_istate = ISTATE_IDLE;
1056 ienab_bis(IE_FDSOFT);
1057 return 1;
1058 case ISTATE_IDLE:
1059 case ISTATE_SPURIOUS:
1060 auxregbisc(0, AUXIO_FDS); /* Does this help? */
1061 fdcresult(fdc);
1062 fdc->sc_istate = ISTATE_SPURIOUS;
1063 printf("fdc: stray hard interrupt... ");
1064 ienab_bis(IE_FDSOFT);
1065 return 1;
1066 case ISTATE_DMA:
1067 break;
1068 default:
1069 printf("fdc: goofed ...\n");
1070 return 1;
1071 }
1072
1073 read = bp->b_flags & B_READ;
1074 for (;;) {
1075 register int msr;
1076
1077 msr = *fdc->sc_reg_msr;
1078
1079 if ((msr & NE7_RQM) == 0)
1080 break;
1081
1082 if ((msr & NE7_NDM) == 0) {
1083 fdcresult(fdc);
1084 fdc->sc_istate = ISTATE_IDLE;
1085 ienab_bis(IE_FDSOFT);
1086 printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
1087 break;
1088 }
1089
1090 if (msr & NE7_DIO) {
1091 #ifdef DIAGNOSTIC
1092 if (!read)
1093 printf("fdxfer: false read\n");
1094 #endif
1095 *fdc->sc_data++ = *fdc->sc_reg_fifo;
1096 } else {
1097 #ifdef DIAGNOSTIC
1098 if (read)
1099 printf("fdxfer: false write\n");
1100 #endif
1101 *fdc->sc_reg_fifo = *fdc->sc_data++;
1102 }
1103 if (--fdc->sc_tc == 0) {
1104 auxregbisc(AUXIO_FTC, 0);
1105 fdc->sc_istate = ISTATE_IDLE;
1106 delay(10);
1107 auxregbisc(0, AUXIO_FTC);
1108 fdcresult(fdc);
1109 ienab_bis(IE_FDSOFT);
1110 break;
1111 }
1112 }
1113 return 1;
1114 }
1115 #endif
1116
1117 int
1118 fdcswintr(fdc)
1119 struct fdc_softc *fdc;
1120 {
1121 #define st0 fdc->sc_status[0]
1122 #define st1 fdc->sc_status[1]
1123 #define cyl fdc->sc_status[1]
1124 #define OUT_FDC(fdc, c, s) \
1125 do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
1126
1127 struct fd_softc *fd;
1128 struct buf *bp;
1129 int read, head, sec, nblks;
1130 struct fd_type *type;
1131
1132
1133 if (fdc->sc_istate != ISTATE_IDLE) {
1134 /* Trouble... */
1135 printf("fdc: spurious interrupt: state %d, istate=%d\n",
1136 fdc->sc_state, fdc->sc_istate);
1137 fdc->sc_istate = ISTATE_IDLE;
1138 if (fdc->sc_state == RESETCOMPLETE ||
1139 fdc->sc_state == RESETTIMEDOUT) {
1140 panic("fdcintr: spurious interrupt can't be cleared");
1141 }
1142 goto doreset;
1143 }
1144
1145 loop:
1146 /* Is there a drive for the controller to do a transfer with? */
1147 fd = fdc->sc_drives.tqh_first;
1148 if (fd == NULL) {
1149 fdc->sc_state = DEVIDLE;
1150 return 0;
1151 }
1152
1153 /* Is there a transfer to this drive? If not, deactivate drive. */
1154 bp = fd->sc_q.b_actf;
1155 if (bp == NULL) {
1156 fd->sc_ops = 0;
1157 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1158 fd->sc_q.b_active = 0;
1159 goto loop;
1160 }
1161
1162 switch (fdc->sc_state) {
1163 case DEVIDLE:
1164 fdc->sc_errors = 0;
1165 fd->sc_skip = 0;
1166 fd->sc_bcount = bp->b_bcount;
1167 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1168 untimeout(fd_motor_off, fd);
1169 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1170 fdc->sc_state = MOTORWAIT;
1171 return 1;
1172 }
1173 if ((fd->sc_flags & FD_MOTOR) == 0) {
1174 /* Turn on the motor, being careful about pairing. */
1175 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1176 if (ofd && ofd->sc_flags & FD_MOTOR) {
1177 untimeout(fd_motor_off, ofd);
1178 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1179 }
1180 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1181 fd_set_motor(fdc);
1182 fdc->sc_state = MOTORWAIT;
1183 if (fdc->sc_flags & FDC_82077) { /* XXX */
1184 /* Allow .25s for motor to stabilize. */
1185 timeout(fd_motor_on, fd, hz / 4);
1186 } else {
1187 fd->sc_flags &= ~FD_MOTOR_WAIT;
1188 goto loop;
1189 }
1190 return 1;
1191 }
1192 /* Make sure the right drive is selected. */
1193 fd_set_motor(fdc);
1194
1195 /* fall through */
1196 case DOSEEK:
1197 doseek:
1198 if (fdc->sc_flags & FDC_EIS) {
1199 fd->sc_cylin = bp->b_cylin;
1200 /* We use implied seek */
1201 goto doio;
1202 }
1203
1204 if (fd->sc_cylin == bp->b_cylin)
1205 goto doio;
1206
1207 /* specify command */
1208 OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
1209 OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
1210 OUT_FDC(fdc, 6, SEEKTIMEDOUT); /* XXX head load time == 6ms */
1211
1212 fdc->sc_istate = ISTATE_SENSEI;
1213 /* seek function */
1214 OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
1215 OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
1216 OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
1217
1218 fd->sc_cylin = -1;
1219 fdc->sc_state = SEEKWAIT;
1220 fdc->sc_nstat = 0;
1221
1222 fd->sc_dk.dk_seek++;
1223 disk_busy(&fd->sc_dk);
1224
1225 timeout(fdctimeout, fdc, 4 * hz);
1226 return 1;
1227
1228 case DOIO:
1229 doio:
1230 type = fd->sc_type;
1231 sec = fd->sc_blkno % type->seccyl;
1232 nblks = type->seccyl - sec;
1233 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1234 nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
1235 fd->sc_nblks = nblks;
1236 fd->sc_nbytes = nblks * FDC_BSIZE;
1237 head = sec / type->sectrac;
1238 sec -= head * type->sectrac;
1239 #ifdef DIAGNOSTIC
1240 {int block;
1241 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1242 if (block != fd->sc_blkno) {
1243 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1244 #ifdef DDB
1245 Debugger();
1246 #endif
1247 }}
1248 #endif
1249 read = bp->b_flags & B_READ;
1250
1251 /* Setup for pseudo DMA */
1252 fdc->sc_data = bp->b_data + fd->sc_skip;
1253 fdc->sc_tc = fd->sc_nbytes;
1254
1255 *fdc->sc_reg_drs = type->rate;
1256 #ifdef FD_DEBUG
1257 if (fdc_debug > 1)
1258 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1259 read ? "read" : "write", fd->sc_drive,
1260 fd->sc_cylin, head, sec, nblks);
1261 #endif
1262 fdc->sc_state = IOCOMPLETE;
1263 fdc->sc_istate = ISTATE_DMA;
1264 fdc->sc_nstat = 0;
1265 if (read)
1266 OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT); /* READ */
1267 else
1268 OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT); /* WRITE */
1269 OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
1270 OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT); /* track */
1271 OUT_FDC(fdc, head, IOTIMEDOUT);
1272 OUT_FDC(fdc, sec + 1, IOTIMEDOUT); /* sector +1 */
1273 OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/* sector size */
1274 OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/* sectors/track */
1275 OUT_FDC(fdc, type->gap1, IOTIMEDOUT); /* gap1 size */
1276 OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/* data length */
1277
1278 disk_busy(&fd->sc_dk);
1279
1280 /* allow 2 seconds for operation */
1281 timeout(fdctimeout, fdc, 2 * hz);
1282 return 1; /* will return later */
1283
1284 case SEEKWAIT:
1285 untimeout(fdctimeout, fdc);
1286 fdc->sc_state = SEEKCOMPLETE;
1287 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1288 /* allow 1/50 second for heads to settle */
1289 timeout(fdcpseudointr, fdc, hz / 50);
1290 return 1; /* will return later */
1291 }
1292
1293 case SEEKCOMPLETE:
1294 disk_unbusy(&fd->sc_dk, 0); /* no data on seek */
1295
1296 /* Make sure seek really happened. */
1297 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1298 cyl != bp->b_cylin * fd->sc_type->step) {
1299 #ifdef FD_DEBUG
1300 if (fdc_debug)
1301 fdcstatus(&fd->sc_dv, 2, "seek failed");
1302 #endif
1303 fdcretry(fdc);
1304 goto loop;
1305 }
1306 fd->sc_cylin = bp->b_cylin;
1307 goto doio;
1308
1309 case IOTIMEDOUT:
1310 auxregbisc(AUXIO_FTC, 0);
1311 delay(10);
1312 auxregbisc(0, AUXIO_FTC);
1313 (void)fdcresult(fdc);
1314 case SEEKTIMEDOUT:
1315 case RECALTIMEDOUT:
1316 case RESETTIMEDOUT:
1317 fdcretry(fdc);
1318 goto loop;
1319
1320 case IOCOMPLETE: /* IO DONE, post-analyze */
1321 untimeout(fdctimeout, fdc);
1322
1323 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1324
1325 if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
1326 #ifdef FD_DEBUG
1327 if (fdc_debug) {
1328 fdcstatus(&fd->sc_dv, 7,
1329 bp->b_flags & B_READ
1330 ? "read failed" : "write failed");
1331 printf("blkno %d nblks %d tc %d\n",
1332 fd->sc_blkno, fd->sc_nblks, fdc->sc_tc);
1333 }
1334 #endif
1335 if (fdc->sc_nstat == 7 &&
1336 (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1337
1338 /*
1339 * Silently retry overruns if no other
1340 * error bit is set. Adjust threshold.
1341 */
1342 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1343 if (thr < 15) {
1344 thr++;
1345 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1346 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1347 #ifdef FD_DEBUG
1348 if (fdc_debug)
1349 printf("fdc: %d -> threshold\n", thr);
1350 #endif
1351 fdconf(fdc);
1352 fdc->sc_overruns = 0;
1353 }
1354 if (++fdc->sc_overruns < 3) {
1355 fdc->sc_state = DOIO;
1356 goto loop;
1357 }
1358 }
1359 fdcretry(fdc);
1360 goto loop;
1361 }
1362 if (fdc->sc_errors) {
1363 diskerr(bp, "fd", "soft error", LOG_PRINTF,
1364 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1365 printf("\n");
1366 fdc->sc_errors = 0;
1367 } else {
1368 if (--fdc->sc_overruns < -20) {
1369 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1370 if (thr > 0) {
1371 thr--;
1372 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1373 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1374 #ifdef FD_DEBUG
1375 if (fdc_debug)
1376 printf("fdc: %d -> threshold\n", thr);
1377 #endif
1378 fdconf(fdc);
1379 }
1380 fdc->sc_overruns = 0;
1381 }
1382 }
1383 fd->sc_blkno += fd->sc_nblks;
1384 fd->sc_skip += fd->sc_nbytes;
1385 fd->sc_bcount -= fd->sc_nbytes;
1386 if (fd->sc_bcount > 0) {
1387 bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
1388 goto doseek;
1389 }
1390 fdfinish(fd, bp);
1391 goto loop;
1392
1393 case DORESET:
1394 doreset:
1395 /* try a reset, keep motor on */
1396 fd_set_motor(fdc);
1397 delay(100);
1398 fdc_reset(fdc);
1399 fdc->sc_nstat = 0;
1400 fdc->sc_istate = ISTATE_SENSEI;
1401 fdc->sc_state = RESETCOMPLETE;
1402 timeout(fdctimeout, fdc, hz / 2);
1403 return 1; /* will return later */
1404
1405 case RESETCOMPLETE:
1406 untimeout(fdctimeout, fdc);
1407 fdconf(fdc);
1408
1409 /* fall through */
1410 case DORECAL:
1411 fdc->sc_state = RECALWAIT;
1412 fdc->sc_istate = ISTATE_SENSEI;
1413 fdc->sc_nstat = 0;
1414 /* recalibrate function */
1415 OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
1416 OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
1417 timeout(fdctimeout, fdc, 5 * hz);
1418 return 1; /* will return later */
1419
1420 case RECALWAIT:
1421 untimeout(fdctimeout, fdc);
1422 fdc->sc_state = RECALCOMPLETE;
1423 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1424 /* allow 1/30 second for heads to settle */
1425 timeout(fdcpseudointr, fdc, hz / 30);
1426 return 1; /* will return later */
1427 }
1428
1429 case RECALCOMPLETE:
1430 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1431 #ifdef FD_DEBUG
1432 if (fdc_debug)
1433 fdcstatus(&fd->sc_dv, 2, "recalibrate failed");
1434 #endif
1435 fdcretry(fdc);
1436 goto loop;
1437 }
1438 fd->sc_cylin = 0;
1439 goto doseek;
1440
1441 case MOTORWAIT:
1442 if (fd->sc_flags & FD_MOTOR_WAIT)
1443 return 1; /* time's not up yet */
1444 goto doseek;
1445
1446 default:
1447 fdcstatus(&fd->sc_dv, 0, "stray interrupt");
1448 return 1;
1449 }
1450 #ifdef DIAGNOSTIC
1451 panic("fdcintr: impossible");
1452 #endif
1453 #undef st0
1454 #undef st1
1455 #undef cyl
1456 }
1457
1458 void
1459 fdcretry(fdc)
1460 struct fdc_softc *fdc;
1461 {
1462 struct fd_softc *fd;
1463 struct buf *bp;
1464
1465 fd = fdc->sc_drives.tqh_first;
1466 bp = fd->sc_q.b_actf;
1467
1468 fdc->sc_overruns = 0;
1469
1470 switch (fdc->sc_errors) {
1471 case 0:
1472 /* try again */
1473 fdc->sc_state =
1474 (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
1475 break;
1476
1477 case 1: case 2: case 3:
1478 /* didn't work; try recalibrating */
1479 fdc->sc_state = DORECAL;
1480 break;
1481
1482 case 4:
1483 /* still no go; reset the bastard */
1484 fdc->sc_state = DORESET;
1485 break;
1486
1487 default:
1488 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1489 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1490
1491 printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
1492 fdc->sc_status[0], NE7_ST0BITS,
1493 fdc->sc_status[1], NE7_ST1BITS,
1494 fdc->sc_status[2], NE7_ST2BITS,
1495 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1496
1497 bp->b_flags |= B_ERROR;
1498 bp->b_error = EIO;
1499 fdfinish(fd, bp);
1500 }
1501 fdc->sc_errors++;
1502 }
1503
1504 int
1505 fdsize(dev)
1506 dev_t dev;
1507 {
1508
1509 /* Swapping to floppies would not make sense. */
1510 return -1;
1511 }
1512
1513 int
1514 fddump(dev, blkno, va, size)
1515 dev_t dev;
1516 daddr_t blkno;
1517 caddr_t va;
1518 size_t size;
1519 {
1520
1521 /* Not implemented. */
1522 return EINVAL;
1523 }
1524
1525 int
1526 fdioctl(dev, cmd, addr, flag, p)
1527 dev_t dev;
1528 u_long cmd;
1529 caddr_t addr;
1530 int flag;
1531 struct proc *p;
1532 {
1533 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1534 int error;
1535
1536 switch (cmd) {
1537 case DIOCGDINFO:
1538 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1539 return 0;
1540
1541 case DIOCWLABEL:
1542 if ((flag & FWRITE) == 0)
1543 return EBADF;
1544 /* XXX do something */
1545 return 0;
1546
1547 case DIOCWDINFO:
1548 if ((flag & FWRITE) == 0)
1549 return EBADF;
1550
1551 error = setdisklabel(fd->sc_dk.dk_label,
1552 (struct disklabel *)addr, 0,
1553 fd->sc_dk.dk_cpulabel);
1554 if (error)
1555 return error;
1556
1557 error = writedisklabel(dev, fdstrategy,
1558 fd->sc_dk.dk_label,
1559 fd->sc_dk.dk_cpulabel);
1560 return error;
1561
1562 case DIOCLOCK:
1563 /*
1564 * Nothing to do here, really.
1565 */
1566 return 0;
1567
1568 case DIOCEJECT:
1569 fd_do_eject();
1570 return 0;
1571
1572 #ifdef DEBUG
1573 case _IO('f', 100):
1574 {
1575 int i;
1576 struct fdc_softc *fdc = (struct fdc_softc *)
1577 fd->sc_dv.dv_parent;
1578
1579 out_fdc(fdc, NE7CMD_DUMPREG);
1580 fdcresult(fdc);
1581 printf("dumpreg(%d regs): <", fdc->sc_nstat);
1582 for (i = 0; i < fdc->sc_nstat; i++)
1583 printf(" %x", fdc->sc_status[i]);
1584 printf(">\n");
1585 }
1586
1587 return 0;
1588 case _IOW('f', 101, int):
1589 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg &=
1590 ~CFG_THRHLD_MASK;
1591 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg |=
1592 (*(int *)addr & CFG_THRHLD_MASK);
1593 fdconf((struct fdc_softc *) fd->sc_dv.dv_parent);
1594 return 0;
1595 case _IO('f', 102):
1596 {
1597 int i;
1598 struct fdc_softc *fdc = (struct fdc_softc *)
1599 fd->sc_dv.dv_parent;
1600 out_fdc(fdc, NE7CMD_SENSEI);
1601 fdcresult(fdc);
1602 printf("sensei(%d regs): <", fdc->sc_nstat);
1603 for (i=0; i< fdc->sc_nstat; i++)
1604 printf(" 0x%x", fdc->sc_status[i]);
1605 }
1606 printf(">\n");
1607 return 0;
1608 #endif
1609 default:
1610 return ENOTTY;
1611 }
1612
1613 #ifdef DIAGNOSTIC
1614 panic("fdioctl: impossible");
1615 #endif
1616 }
1617
1618 void
1619 fdgetdisklabel(dev)
1620 dev_t dev;
1621 {
1622 int unit = FDUNIT(dev), i;
1623 struct fd_softc *fd = fd_cd.cd_devs[unit];
1624 struct disklabel *lp = fd->sc_dk.dk_label;
1625 struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
1626
1627 bzero(lp, sizeof(struct disklabel));
1628 bzero(lp, sizeof(struct cpu_disklabel));
1629
1630 lp->d_type = DTYPE_FLOPPY;
1631 lp->d_secsize = FDC_BSIZE;
1632 lp->d_secpercyl = fd->sc_type->seccyl;
1633 lp->d_nsectors = fd->sc_type->sectrac;
1634 lp->d_ncylinders = fd->sc_type->tracks;
1635 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
1636 lp->d_rpm = 3600; /* XXX like it matters... */
1637
1638 strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
1639 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1640 lp->d_interleave = 1;
1641
1642 lp->d_partitions[RAW_PART].p_offset = 0;
1643 lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
1644 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1645 lp->d_npartitions = RAW_PART + 1;
1646
1647 lp->d_magic = DISKMAGIC;
1648 lp->d_magic2 = DISKMAGIC;
1649 lp->d_checksum = dkcksum(lp);
1650
1651 /*
1652 * Call the generic disklabel extraction routine. If there's
1653 * not a label there, fake it.
1654 */
1655 if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
1656 strncpy(lp->d_packname, "default label",
1657 sizeof(lp->d_packname));
1658 /*
1659 * Reset the partition info; it might have gotten
1660 * trashed in readdisklabel().
1661 *
1662 * XXX Why do we have to do this? readdisklabel()
1663 * should be safe...
1664 */
1665 for (i = 0; i < MAXPARTITIONS; ++i) {
1666 lp->d_partitions[i].p_offset = 0;
1667 if (i == RAW_PART) {
1668 lp->d_partitions[i].p_size =
1669 lp->d_secpercyl * lp->d_ncylinders;
1670 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1671 } else {
1672 lp->d_partitions[i].p_size = 0;
1673 lp->d_partitions[i].p_fstype = FS_UNUSED;
1674 }
1675 }
1676 lp->d_npartitions = RAW_PART + 1;
1677 }
1678 }
1679
1680 void
1681 fd_do_eject()
1682 {
1683
1684 auxregbisc(AUXIO_FDS, AUXIO_FEJ);
1685 delay(10);
1686 auxregbisc(AUXIO_FEJ, AUXIO_FDS);
1687 }
1688
1689 /* ARGSUSED */
1690 void
1691 fd_mountroot_hook(dev)
1692 struct device *dev;
1693 {
1694 int c;
1695
1696 fd_do_eject();
1697 printf("Insert filesystem floppy and press return.");
1698 for (;;) {
1699 c = cngetc();
1700 if ((c == '\r') || (c == '\n')) {
1701 printf("\n");
1702 return;
1703 }
1704 }
1705 }
1706