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