fd.c revision 1.41 1 /* $NetBSD: fd.c,v 1.41 1996/11/27 21:14:33 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 *, const 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 const 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 switch (ca->ca_bustype) {
441 case BUS_MAIN:
442 /*
443 * We can get the bootpath in several different
444 * formats! The faked v1 bootpath looks like /fd@0,0.
445 * The v2 bootpath is either just /fd0, in which case
446 * `bp->val[0]' will have been set to -1, or /fd@x,y
447 * where <x,y> is the prom address specifier.
448 */
449 if (((bp->val[0] == ca->ca_ra.ra_iospace) &&
450 (bp->val[1] == (int)ca->ca_ra.ra_paddr)) ||
451
452 ((bp->val[0] == -1) && /* v2: /fd0 */
453 (bp->val[1] == 0)) ||
454
455 ((bp->val[0] == 0) && /* v1: /fd@0,0 */
456 (bp->val[1] == 0))
457 )
458 fa.fa_bootpath = bp;
459 break;
460
461 case BUS_OBIO:
462 /*
463 * floppy controller on obio (such as on the sun4m),
464 * e.g.: `/obio0/SUNW,fdtwo@0,700000'.
465 * We use "slot, offset" to determine if this is the
466 * right one.
467 */
468 if ((bp->val[0] == ca->ca_slot) &&
469 (bp->val[1] == ca->ca_offset))
470 fa.fa_bootpath = bp;
471 break;
472 }
473
474 }
475
476 /* physical limit: four drives per controller. */
477 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
478 fa.fa_deftype = NULL; /* unknown */
479 fa.fa_deftype = &fd_types[0]; /* XXX */
480 (void)config_found(self, (void *)&fa, fdprint);
481 }
482
483 bootpath_store(1, NULL);
484 }
485
486 int
487 fdmatch(parent, match, aux)
488 struct device *parent;
489 void *match, *aux;
490 {
491 struct fdc_softc *fdc = (void *)parent;
492 struct fdc_attach_args *fa = aux;
493 int drive = fa->fa_drive;
494 int n, ok;
495
496 if (drive > 0)
497 /* XXX - for now, punt > 1 drives */
498 return 0;
499
500 if (fdc->sc_flags & FDC_82077) {
501 /* select drive and turn on motor */
502 *fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
503 /* wait for motor to spin up */
504 delay(250000);
505 } else {
506 auxregbisc(AUXIO_FDS, 0);
507 }
508 fdc->sc_nstat = 0;
509 out_fdc(fdc, NE7CMD_RECAL);
510 out_fdc(fdc, drive);
511 /* wait for recalibrate */
512 for (n = 0; n < 10000; n++) {
513 delay(1000);
514 if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
515 /* wait a bit longer till device *really* is ready */
516 delay(100000);
517 if (out_fdc(fdc, NE7CMD_SENSEI))
518 break;
519 if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
520 /*
521 * Got `invalid command'; we interpret it
522 * to mean that the re-calibrate hasn't in
523 * fact finished yet
524 */
525 continue;
526 break;
527 }
528 }
529 n = fdc->sc_nstat;
530 #ifdef FD_DEBUG
531 if (fdc_debug) {
532 int i;
533 printf("fdprobe: %d stati:", n);
534 for (i = 0; i < n; i++)
535 printf(" %x", fdc->sc_status[i]);
536 printf("\n");
537 }
538 #endif
539 ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
540
541 /* turn off motor */
542 if (fdc->sc_flags & FDC_82077) {
543 /* select drive and turn on motor */
544 *fdc->sc_reg_dor = FDO_FRST;
545 } else {
546 auxregbisc(0, AUXIO_FDS);
547 }
548
549 return ok;
550 }
551
552 /*
553 * Controller is working, and drive responded. Attach it.
554 */
555 void
556 fdattach(parent, self, aux)
557 struct device *parent, *self;
558 void *aux;
559 {
560 struct fdc_softc *fdc = (void *)parent;
561 struct fd_softc *fd = (void *)self;
562 struct fdc_attach_args *fa = aux;
563 struct fd_type *type = fa->fa_deftype;
564 int drive = fa->fa_drive;
565
566 /* XXX Allow `flags' to override device type? */
567
568 if (type)
569 printf(": %s %d cyl, %d head, %d sec\n", type->name,
570 type->tracks, type->heads, type->sectrac);
571 else
572 printf(": density unknown\n");
573
574 fd->sc_cylin = -1;
575 fd->sc_drive = drive;
576 fd->sc_deftype = type;
577 fdc->sc_fd[drive] = fd;
578
579 /*
580 * Initialize and attach the disk structure.
581 */
582 fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
583 fd->sc_dk.dk_driver = &fddkdriver;
584 disk_attach(&fd->sc_dk);
585
586 /*
587 * We're told if we're the boot device in fdcattach().
588 */
589 if (fa->fa_bootpath)
590 fa->fa_bootpath->dev = &fd->sc_dv;
591
592 /*
593 * Establish a mountroot_hook anyway in case we booted
594 * with RB_ASKNAME and get selected as the boot device.
595 */
596 mountroot_hook_establish(fd_mountroot_hook, &fd->sc_dv);
597
598 /* Make sure the drive motor gets turned off at shutdown time. */
599 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
600
601 /* XXX Need to do some more fiddling with sc_dk. */
602 dk_establish(&fd->sc_dk, &fd->sc_dv);
603 }
604
605 __inline struct fd_type *
606 fd_dev_to_type(fd, dev)
607 struct fd_softc *fd;
608 dev_t dev;
609 {
610 int type = FDTYPE(dev);
611
612 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
613 return NULL;
614 return type ? &fd_types[type - 1] : fd->sc_deftype;
615 }
616
617 void
618 fdstrategy(bp)
619 register struct buf *bp; /* IO operation to perform */
620 {
621 struct fd_softc *fd;
622 int unit = FDUNIT(bp->b_dev);
623 int sz;
624 int s;
625
626 /* Valid unit, controller, and request? */
627 if (unit >= fd_cd.cd_ndevs ||
628 (fd = fd_cd.cd_devs[unit]) == 0 ||
629 bp->b_blkno < 0 ||
630 (bp->b_bcount % FDC_BSIZE) != 0) {
631 bp->b_error = EINVAL;
632 goto bad;
633 }
634
635 /* If it's a null transfer, return immediately. */
636 if (bp->b_bcount == 0)
637 goto done;
638
639 sz = howmany(bp->b_bcount, FDC_BSIZE);
640
641 if (bp->b_blkno + sz > fd->sc_type->size) {
642 sz = fd->sc_type->size - bp->b_blkno;
643 if (sz == 0) {
644 /* If exactly at end of disk, return EOF. */
645 bp->b_resid = bp->b_bcount;
646 goto done;
647 }
648 if (sz < 0) {
649 /* If past end of disk, return EINVAL. */
650 bp->b_error = EINVAL;
651 goto bad;
652 }
653 /* Otherwise, truncate request. */
654 bp->b_bcount = sz << DEV_BSHIFT;
655 }
656
657 bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
658
659 #ifdef FD_DEBUG
660 if (fdc_debug > 1)
661 printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n",
662 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
663 #endif
664
665 /* Queue transfer on drive, activate drive and controller if idle. */
666 s = splbio();
667 disksort(&fd->sc_q, bp);
668 untimeout(fd_motor_off, fd); /* a good idea */
669 if (!fd->sc_q.b_active)
670 fdstart(fd);
671 #ifdef DIAGNOSTIC
672 else {
673 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
674 if (fdc->sc_state == DEVIDLE) {
675 printf("fdstrategy: controller inactive\n");
676 fdcstart(fdc);
677 }
678 }
679 #endif
680 splx(s);
681 return;
682
683 bad:
684 bp->b_flags |= B_ERROR;
685 done:
686 /* Toss transfer; we're done early. */
687 biodone(bp);
688 }
689
690 void
691 fdstart(fd)
692 struct fd_softc *fd;
693 {
694 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
695 int active = fdc->sc_drives.tqh_first != 0;
696
697 /* Link into controller queue. */
698 fd->sc_q.b_active = 1;
699 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
700
701 /* If controller not already active, start it. */
702 if (!active)
703 fdcstart(fdc);
704 }
705
706 void
707 fdfinish(fd, bp)
708 struct fd_softc *fd;
709 struct buf *bp;
710 {
711 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
712
713 /*
714 * Move this drive to the end of the queue to give others a `fair'
715 * chance. We only force a switch if N operations are completed while
716 * another drive is waiting to be serviced, since there is a long motor
717 * startup delay whenever we switch.
718 */
719 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
720 fd->sc_ops = 0;
721 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
722 if (bp->b_actf) {
723 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
724 } else
725 fd->sc_q.b_active = 0;
726 }
727 bp->b_resid = fd->sc_bcount;
728 fd->sc_skip = 0;
729 fd->sc_q.b_actf = bp->b_actf;
730
731 biodone(bp);
732 /* turn off motor 5s from now */
733 timeout(fd_motor_off, fd, 5 * hz);
734 fdc->sc_state = DEVIDLE;
735 }
736
737 void
738 fdc_reset(fdc)
739 struct fdc_softc *fdc;
740 {
741 if (fdc->sc_flags & FDC_82077) {
742 *fdc->sc_reg_dor = FDO_MOEN(0);
743 }
744
745 *fdc->sc_reg_drs = DRS_RESET;
746 delay(10);
747 *fdc->sc_reg_drs = 0;
748 #ifdef FD_DEBUG
749 if (fdc_debug)
750 printf("fdc reset\n");
751 #endif
752 }
753
754 void
755 fd_set_motor(fdc)
756 struct fdc_softc *fdc;
757 {
758 struct fd_softc *fd;
759 u_char status;
760 int n;
761
762 if (fdc->sc_flags & FDC_82077) {
763 status = FDO_FRST | FDO_FDMAEN;
764 if ((fd = fdc->sc_drives.tqh_first) != NULL)
765 status |= fd->sc_drive;
766
767 for (n = 0; n < 4; n++)
768 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
769 status |= FDO_MOEN(n);
770 *fdc->sc_reg_dor = status;
771 } else {
772 int on = 0;
773
774 for (n = 0; n < 4; n++)
775 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
776 on = 1;
777 if (on) {
778 auxregbisc(AUXIO_FDS, 0);
779 } else {
780 auxregbisc(0, AUXIO_FDS);
781 }
782 }
783 }
784
785 void
786 fd_motor_off(arg)
787 void *arg;
788 {
789 struct fd_softc *fd = arg;
790 int s;
791
792 s = splbio();
793 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
794 fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
795 splx(s);
796 }
797
798 void
799 fd_motor_on(arg)
800 void *arg;
801 {
802 struct fd_softc *fd = arg;
803 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
804 int s;
805
806 s = splbio();
807 fd->sc_flags &= ~FD_MOTOR_WAIT;
808 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
809 (void) fdcswintr(fdc);
810 splx(s);
811 }
812
813 int
814 fdcresult(fdc)
815 struct fdc_softc *fdc;
816 {
817 u_char i;
818 int j = 100000,
819 n = 0;
820
821 for (; j; j--) {
822 i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
823 if (i == NE7_RQM)
824 return (fdc->sc_nstat = n);
825 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
826 if (n >= sizeof(fdc->sc_status)) {
827 log(LOG_ERR, "fdcresult: overrun\n");
828 return -1;
829 }
830 fdc->sc_status[n++] = *fdc->sc_reg_fifo;
831 } else
832 delay(10);
833 }
834 log(LOG_ERR, "fdcresult: timeout\n");
835 return (fdc->sc_nstat = -1);
836 }
837
838 int
839 out_fdc(fdc, x)
840 struct fdc_softc *fdc;
841 u_char x;
842 {
843 int i = 100000;
844
845 while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0)
846 delay(1);
847 if (i <= 0)
848 return -1;
849
850 *fdc->sc_reg_fifo = x;
851 return 0;
852 }
853
854 int
855 fdopen(dev, flags, fmt, p)
856 dev_t dev;
857 int flags, fmt;
858 struct proc *p;
859 {
860 int unit, pmask;
861 struct fd_softc *fd;
862 struct fd_type *type;
863
864 unit = FDUNIT(dev);
865 if (unit >= fd_cd.cd_ndevs)
866 return ENXIO;
867 fd = fd_cd.cd_devs[unit];
868 if (fd == 0)
869 return ENXIO;
870 type = fd_dev_to_type(fd, dev);
871 if (type == NULL)
872 return ENXIO;
873
874 if ((fd->sc_flags & FD_OPEN) != 0 &&
875 fd->sc_type != type)
876 return EBUSY;
877
878 fd->sc_type = type;
879 fd->sc_cylin = -1;
880 fd->sc_flags |= FD_OPEN;
881
882 /*
883 * Only update the disklabel if we're not open anywhere else.
884 */
885 if (fd->sc_dk.dk_openmask == 0)
886 fdgetdisklabel(dev);
887
888 pmask = (1 << DISKPART(dev));
889
890 switch (fmt) {
891 case S_IFCHR:
892 fd->sc_dk.dk_copenmask |= pmask;
893 break;
894
895 case S_IFBLK:
896 fd->sc_dk.dk_bopenmask |= pmask;
897 break;
898 }
899 fd->sc_dk.dk_openmask =
900 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
901
902 return 0;
903 }
904
905 int
906 fdclose(dev, flags, fmt, p)
907 dev_t dev;
908 int flags, fmt;
909 struct proc *p;
910 {
911 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
912 int pmask = (1 << DISKPART(dev));
913
914 fd->sc_flags &= ~FD_OPEN;
915
916 switch (fmt) {
917 case S_IFCHR:
918 fd->sc_dk.dk_copenmask &= ~pmask;
919 break;
920
921 case S_IFBLK:
922 fd->sc_dk.dk_bopenmask &= ~pmask;
923 break;
924 }
925 fd->sc_dk.dk_openmask =
926 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
927
928 return 0;
929 }
930
931 int
932 fdread(dev, uio, flag)
933 dev_t dev;
934 struct uio *uio;
935 int flag;
936 {
937
938 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
939 }
940
941 int
942 fdwrite(dev, uio, flag)
943 dev_t dev;
944 struct uio *uio;
945 int flag;
946 {
947
948 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
949 }
950
951 void
952 fdcstart(fdc)
953 struct fdc_softc *fdc;
954 {
955
956 #ifdef DIAGNOSTIC
957 /* only got here if controller's drive queue was inactive; should
958 be in idle state */
959 if (fdc->sc_state != DEVIDLE) {
960 printf("fdcstart: not idle\n");
961 return;
962 }
963 #endif
964 (void) fdcswintr(fdc);
965 }
966
967 void
968 fdcstatus(dv, n, s)
969 struct device *dv;
970 int n;
971 char *s;
972 {
973 struct fdc_softc *fdc = (void *)dv->dv_parent;
974 char bits[64];
975 #if 0
976 /*
977 * A 82072 seems to return <invalid command> on
978 * gratuitous Sense Interrupt commands.
979 */
980 if (n == 0 && (fdc->sc_flags & FDC_82077)) {
981 out_fdc(fdc, NE7CMD_SENSEI);
982 (void) fdcresult(fdc);
983 n = 2;
984 }
985 #endif
986
987 /* Just print last status */
988 n = fdc->sc_nstat;
989
990 printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
991
992 switch (n) {
993 case 0:
994 printf("\n");
995 break;
996 case 2:
997 printf(" (st0 %s cyl %d)\n",
998 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
999 bits, sizeof(bits)), fdc->sc_status[1]);
1000 break;
1001 case 7:
1002 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1003 NE7_ST0BITS, bits, sizeof(bits)));
1004 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1005 NE7_ST1BITS, bits, sizeof(bits)));
1006 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1007 NE7_ST2BITS, bits, sizeof(bits)));
1008 printf(" cyl %d head %d sec %d)\n",
1009 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1010 break;
1011 #ifdef DIAGNOSTIC
1012 default:
1013 printf(" fdcstatus: weird size: %d\n", n);
1014 break;
1015 #endif
1016 }
1017 }
1018
1019 void
1020 fdctimeout(arg)
1021 void *arg;
1022 {
1023 struct fdc_softc *fdc = arg;
1024 struct fd_softc *fd = fdc->sc_drives.tqh_first;
1025 int s;
1026
1027 s = splbio();
1028 fdcstatus(&fd->sc_dv, 0, "timeout");
1029
1030 if (fd->sc_q.b_actf)
1031 fdc->sc_state++;
1032 else
1033 fdc->sc_state = DEVIDLE;
1034
1035 (void) fdcswintr(fdc);
1036 splx(s);
1037 }
1038
1039 void
1040 fdcpseudointr(arg)
1041 void *arg;
1042 {
1043 struct fdc_softc *fdc = arg;
1044 int s;
1045
1046 /* Just ensure it has the right spl. */
1047 s = splbio();
1048 (void) fdcswintr(fdc);
1049 splx(s);
1050 }
1051
1052
1053 #ifdef FDC_C_HANDLER
1054 /*
1055 * hardware interrupt entry point: must be converted to `fast'
1056 * (in-window) handler.
1057 */
1058 int
1059 fdchwintr(fdc)
1060 struct fdc_softc *fdc;
1061 {
1062 struct buf *bp;
1063 int read;
1064
1065 switch (fdc->sc_istate) {
1066 case ISTATE_SENSEI:
1067 out_fdc(fdc, NE7CMD_SENSEI);
1068 fdcresult(fdc);
1069 fdc->sc_istate = ISTATE_IDLE;
1070 ienab_bis(IE_FDSOFT);
1071 return 1;
1072 case ISTATE_IDLE:
1073 case ISTATE_SPURIOUS:
1074 auxregbisc(0, AUXIO_FDS); /* Does this help? */
1075 fdcresult(fdc);
1076 fdc->sc_istate = ISTATE_SPURIOUS;
1077 printf("fdc: stray hard interrupt... ");
1078 ienab_bis(IE_FDSOFT);
1079 return 1;
1080 case ISTATE_DMA:
1081 break;
1082 default:
1083 printf("fdc: goofed ...\n");
1084 return 1;
1085 }
1086
1087 read = bp->b_flags & B_READ;
1088 for (;;) {
1089 register int msr;
1090
1091 msr = *fdc->sc_reg_msr;
1092
1093 if ((msr & NE7_RQM) == 0)
1094 break;
1095
1096 if ((msr & NE7_NDM) == 0) {
1097 fdcresult(fdc);
1098 fdc->sc_istate = ISTATE_IDLE;
1099 ienab_bis(IE_FDSOFT);
1100 printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
1101 break;
1102 }
1103
1104 if (msr & NE7_DIO) {
1105 #ifdef DIAGNOSTIC
1106 if (!read)
1107 printf("fdxfer: false read\n");
1108 #endif
1109 *fdc->sc_data++ = *fdc->sc_reg_fifo;
1110 } else {
1111 #ifdef DIAGNOSTIC
1112 if (read)
1113 printf("fdxfer: false write\n");
1114 #endif
1115 *fdc->sc_reg_fifo = *fdc->sc_data++;
1116 }
1117 if (--fdc->sc_tc == 0) {
1118 auxregbisc(AUXIO_FTC, 0);
1119 fdc->sc_istate = ISTATE_IDLE;
1120 delay(10);
1121 auxregbisc(0, AUXIO_FTC);
1122 fdcresult(fdc);
1123 ienab_bis(IE_FDSOFT);
1124 break;
1125 }
1126 }
1127 return 1;
1128 }
1129 #endif
1130
1131 int
1132 fdcswintr(fdc)
1133 struct fdc_softc *fdc;
1134 {
1135 #define st0 fdc->sc_status[0]
1136 #define st1 fdc->sc_status[1]
1137 #define cyl fdc->sc_status[1]
1138 #define OUT_FDC(fdc, c, s) \
1139 do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
1140
1141 struct fd_softc *fd;
1142 struct buf *bp;
1143 int read, head, sec, nblks;
1144 struct fd_type *type;
1145
1146
1147 if (fdc->sc_istate != ISTATE_IDLE) {
1148 /* Trouble... */
1149 printf("fdc: spurious interrupt: state %d, istate=%d\n",
1150 fdc->sc_state, fdc->sc_istate);
1151 fdc->sc_istate = ISTATE_IDLE;
1152 if (fdc->sc_state == RESETCOMPLETE ||
1153 fdc->sc_state == RESETTIMEDOUT) {
1154 panic("fdcintr: spurious interrupt can't be cleared");
1155 }
1156 goto doreset;
1157 }
1158
1159 loop:
1160 /* Is there a drive for the controller to do a transfer with? */
1161 fd = fdc->sc_drives.tqh_first;
1162 if (fd == NULL) {
1163 fdc->sc_state = DEVIDLE;
1164 return 0;
1165 }
1166
1167 /* Is there a transfer to this drive? If not, deactivate drive. */
1168 bp = fd->sc_q.b_actf;
1169 if (bp == NULL) {
1170 fd->sc_ops = 0;
1171 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1172 fd->sc_q.b_active = 0;
1173 goto loop;
1174 }
1175
1176 switch (fdc->sc_state) {
1177 case DEVIDLE:
1178 fdc->sc_errors = 0;
1179 fd->sc_skip = 0;
1180 fd->sc_bcount = bp->b_bcount;
1181 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1182 untimeout(fd_motor_off, fd);
1183 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1184 fdc->sc_state = MOTORWAIT;
1185 return 1;
1186 }
1187 if ((fd->sc_flags & FD_MOTOR) == 0) {
1188 /* Turn on the motor, being careful about pairing. */
1189 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1190 if (ofd && ofd->sc_flags & FD_MOTOR) {
1191 untimeout(fd_motor_off, ofd);
1192 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1193 }
1194 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1195 fd_set_motor(fdc);
1196 fdc->sc_state = MOTORWAIT;
1197 if (fdc->sc_flags & FDC_82077) { /* XXX */
1198 /* Allow .25s for motor to stabilize. */
1199 timeout(fd_motor_on, fd, hz / 4);
1200 } else {
1201 fd->sc_flags &= ~FD_MOTOR_WAIT;
1202 goto loop;
1203 }
1204 return 1;
1205 }
1206 /* Make sure the right drive is selected. */
1207 fd_set_motor(fdc);
1208
1209 /* fall through */
1210 case DOSEEK:
1211 doseek:
1212 if (fdc->sc_flags & FDC_EIS) {
1213 fd->sc_cylin = bp->b_cylin;
1214 /* We use implied seek */
1215 goto doio;
1216 }
1217
1218 if (fd->sc_cylin == bp->b_cylin)
1219 goto doio;
1220
1221 /* specify command */
1222 OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
1223 OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
1224 OUT_FDC(fdc, 6, SEEKTIMEDOUT); /* XXX head load time == 6ms */
1225
1226 fdc->sc_istate = ISTATE_SENSEI;
1227 /* seek function */
1228 OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
1229 OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
1230 OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
1231
1232 fd->sc_cylin = -1;
1233 fdc->sc_state = SEEKWAIT;
1234 fdc->sc_nstat = 0;
1235
1236 fd->sc_dk.dk_seek++;
1237 disk_busy(&fd->sc_dk);
1238
1239 timeout(fdctimeout, fdc, 4 * hz);
1240 return 1;
1241
1242 case DOIO:
1243 doio:
1244 type = fd->sc_type;
1245 sec = fd->sc_blkno % type->seccyl;
1246 nblks = type->seccyl - sec;
1247 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1248 nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
1249 fd->sc_nblks = nblks;
1250 fd->sc_nbytes = nblks * FDC_BSIZE;
1251 head = sec / type->sectrac;
1252 sec -= head * type->sectrac;
1253 #ifdef DIAGNOSTIC
1254 {int block;
1255 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1256 if (block != fd->sc_blkno) {
1257 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1258 #ifdef DDB
1259 Debugger();
1260 #endif
1261 }}
1262 #endif
1263 read = bp->b_flags & B_READ;
1264
1265 /* Setup for pseudo DMA */
1266 fdc->sc_data = bp->b_data + fd->sc_skip;
1267 fdc->sc_tc = fd->sc_nbytes;
1268
1269 *fdc->sc_reg_drs = type->rate;
1270 #ifdef FD_DEBUG
1271 if (fdc_debug > 1)
1272 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1273 read ? "read" : "write", fd->sc_drive,
1274 fd->sc_cylin, head, sec, nblks);
1275 #endif
1276 fdc->sc_state = IOCOMPLETE;
1277 fdc->sc_istate = ISTATE_DMA;
1278 fdc->sc_nstat = 0;
1279 if (read)
1280 OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT); /* READ */
1281 else
1282 OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT); /* WRITE */
1283 OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
1284 OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT); /* track */
1285 OUT_FDC(fdc, head, IOTIMEDOUT);
1286 OUT_FDC(fdc, sec + 1, IOTIMEDOUT); /* sector +1 */
1287 OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/* sector size */
1288 OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/* sectors/track */
1289 OUT_FDC(fdc, type->gap1, IOTIMEDOUT); /* gap1 size */
1290 OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/* data length */
1291
1292 disk_busy(&fd->sc_dk);
1293
1294 /* allow 2 seconds for operation */
1295 timeout(fdctimeout, fdc, 2 * hz);
1296 return 1; /* will return later */
1297
1298 case SEEKWAIT:
1299 untimeout(fdctimeout, fdc);
1300 fdc->sc_state = SEEKCOMPLETE;
1301 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1302 /* allow 1/50 second for heads to settle */
1303 timeout(fdcpseudointr, fdc, hz / 50);
1304 return 1; /* will return later */
1305 }
1306
1307 case SEEKCOMPLETE:
1308 disk_unbusy(&fd->sc_dk, 0); /* no data on seek */
1309
1310 /* Make sure seek really happened. */
1311 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1312 cyl != bp->b_cylin * fd->sc_type->step) {
1313 #ifdef FD_DEBUG
1314 if (fdc_debug)
1315 fdcstatus(&fd->sc_dv, 2, "seek failed");
1316 #endif
1317 fdcretry(fdc);
1318 goto loop;
1319 }
1320 fd->sc_cylin = bp->b_cylin;
1321 goto doio;
1322
1323 case IOTIMEDOUT:
1324 auxregbisc(AUXIO_FTC, 0);
1325 delay(10);
1326 auxregbisc(0, AUXIO_FTC);
1327 (void)fdcresult(fdc);
1328 case SEEKTIMEDOUT:
1329 case RECALTIMEDOUT:
1330 case RESETTIMEDOUT:
1331 fdcretry(fdc);
1332 goto loop;
1333
1334 case IOCOMPLETE: /* IO DONE, post-analyze */
1335 untimeout(fdctimeout, fdc);
1336
1337 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1338
1339 if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
1340 #ifdef FD_DEBUG
1341 if (fdc_debug) {
1342 fdcstatus(&fd->sc_dv, 7,
1343 bp->b_flags & B_READ
1344 ? "read failed" : "write failed");
1345 printf("blkno %d nblks %d tc %d\n",
1346 fd->sc_blkno, fd->sc_nblks, fdc->sc_tc);
1347 }
1348 #endif
1349 if (fdc->sc_nstat == 7 &&
1350 (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1351
1352 /*
1353 * Silently retry overruns if no other
1354 * error bit is set. Adjust threshold.
1355 */
1356 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1357 if (thr < 15) {
1358 thr++;
1359 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1360 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1361 #ifdef FD_DEBUG
1362 if (fdc_debug)
1363 printf("fdc: %d -> threshold\n", thr);
1364 #endif
1365 fdconf(fdc);
1366 fdc->sc_overruns = 0;
1367 }
1368 if (++fdc->sc_overruns < 3) {
1369 fdc->sc_state = DOIO;
1370 goto loop;
1371 }
1372 }
1373 fdcretry(fdc);
1374 goto loop;
1375 }
1376 if (fdc->sc_errors) {
1377 diskerr(bp, "fd", "soft error", LOG_PRINTF,
1378 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1379 printf("\n");
1380 fdc->sc_errors = 0;
1381 } else {
1382 if (--fdc->sc_overruns < -20) {
1383 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1384 if (thr > 0) {
1385 thr--;
1386 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1387 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1388 #ifdef FD_DEBUG
1389 if (fdc_debug)
1390 printf("fdc: %d -> threshold\n", thr);
1391 #endif
1392 fdconf(fdc);
1393 }
1394 fdc->sc_overruns = 0;
1395 }
1396 }
1397 fd->sc_blkno += fd->sc_nblks;
1398 fd->sc_skip += fd->sc_nbytes;
1399 fd->sc_bcount -= fd->sc_nbytes;
1400 if (fd->sc_bcount > 0) {
1401 bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
1402 goto doseek;
1403 }
1404 fdfinish(fd, bp);
1405 goto loop;
1406
1407 case DORESET:
1408 doreset:
1409 /* try a reset, keep motor on */
1410 fd_set_motor(fdc);
1411 delay(100);
1412 fdc_reset(fdc);
1413 fdc->sc_nstat = 0;
1414 fdc->sc_istate = ISTATE_SENSEI;
1415 fdc->sc_state = RESETCOMPLETE;
1416 timeout(fdctimeout, fdc, hz / 2);
1417 return 1; /* will return later */
1418
1419 case RESETCOMPLETE:
1420 untimeout(fdctimeout, fdc);
1421 fdconf(fdc);
1422
1423 /* fall through */
1424 case DORECAL:
1425 fdc->sc_state = RECALWAIT;
1426 fdc->sc_istate = ISTATE_SENSEI;
1427 fdc->sc_nstat = 0;
1428 /* recalibrate function */
1429 OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
1430 OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
1431 timeout(fdctimeout, fdc, 5 * hz);
1432 return 1; /* will return later */
1433
1434 case RECALWAIT:
1435 untimeout(fdctimeout, fdc);
1436 fdc->sc_state = RECALCOMPLETE;
1437 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1438 /* allow 1/30 second for heads to settle */
1439 timeout(fdcpseudointr, fdc, hz / 30);
1440 return 1; /* will return later */
1441 }
1442
1443 case RECALCOMPLETE:
1444 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1445 #ifdef FD_DEBUG
1446 if (fdc_debug)
1447 fdcstatus(&fd->sc_dv, 2, "recalibrate failed");
1448 #endif
1449 fdcretry(fdc);
1450 goto loop;
1451 }
1452 fd->sc_cylin = 0;
1453 goto doseek;
1454
1455 case MOTORWAIT:
1456 if (fd->sc_flags & FD_MOTOR_WAIT)
1457 return 1; /* time's not up yet */
1458 goto doseek;
1459
1460 default:
1461 fdcstatus(&fd->sc_dv, 0, "stray interrupt");
1462 return 1;
1463 }
1464 #ifdef DIAGNOSTIC
1465 panic("fdcintr: impossible");
1466 #endif
1467 #undef st0
1468 #undef st1
1469 #undef cyl
1470 }
1471
1472 void
1473 fdcretry(fdc)
1474 struct fdc_softc *fdc;
1475 {
1476 char bits[64];
1477 struct fd_softc *fd;
1478 struct buf *bp;
1479
1480 fd = fdc->sc_drives.tqh_first;
1481 bp = fd->sc_q.b_actf;
1482
1483 fdc->sc_overruns = 0;
1484
1485 switch (fdc->sc_errors) {
1486 case 0:
1487 /* try again */
1488 fdc->sc_state =
1489 (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
1490 break;
1491
1492 case 1: case 2: case 3:
1493 /* didn't work; try recalibrating */
1494 fdc->sc_state = DORECAL;
1495 break;
1496
1497 case 4:
1498 /* still no go; reset the bastard */
1499 fdc->sc_state = DORESET;
1500 break;
1501
1502 default:
1503 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1504 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1505
1506 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1507 NE7_ST0BITS, bits, sizeof(bits)));
1508 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1509 NE7_ST1BITS, bits, sizeof(bits)));
1510 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1511 NE7_ST2BITS, bits, sizeof(bits)));
1512 printf(" cyl %d head %d sec %d)\n",
1513 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1514
1515 bp->b_flags |= B_ERROR;
1516 bp->b_error = EIO;
1517 fdfinish(fd, bp);
1518 }
1519 fdc->sc_errors++;
1520 }
1521
1522 int
1523 fdsize(dev)
1524 dev_t dev;
1525 {
1526
1527 /* Swapping to floppies would not make sense. */
1528 return -1;
1529 }
1530
1531 int
1532 fddump(dev, blkno, va, size)
1533 dev_t dev;
1534 daddr_t blkno;
1535 caddr_t va;
1536 size_t size;
1537 {
1538
1539 /* Not implemented. */
1540 return EINVAL;
1541 }
1542
1543 int
1544 fdioctl(dev, cmd, addr, flag, p)
1545 dev_t dev;
1546 u_long cmd;
1547 caddr_t addr;
1548 int flag;
1549 struct proc *p;
1550 {
1551 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1552 int error;
1553
1554 switch (cmd) {
1555 case DIOCGDINFO:
1556 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1557 return 0;
1558
1559 case DIOCWLABEL:
1560 if ((flag & FWRITE) == 0)
1561 return EBADF;
1562 /* XXX do something */
1563 return 0;
1564
1565 case DIOCWDINFO:
1566 if ((flag & FWRITE) == 0)
1567 return EBADF;
1568
1569 error = setdisklabel(fd->sc_dk.dk_label,
1570 (struct disklabel *)addr, 0,
1571 fd->sc_dk.dk_cpulabel);
1572 if (error)
1573 return error;
1574
1575 error = writedisklabel(dev, fdstrategy,
1576 fd->sc_dk.dk_label,
1577 fd->sc_dk.dk_cpulabel);
1578 return error;
1579
1580 case DIOCLOCK:
1581 /*
1582 * Nothing to do here, really.
1583 */
1584 return 0;
1585
1586 case DIOCEJECT:
1587 fd_do_eject();
1588 return 0;
1589
1590 #ifdef DEBUG
1591 case _IO('f', 100):
1592 {
1593 int i;
1594 struct fdc_softc *fdc = (struct fdc_softc *)
1595 fd->sc_dv.dv_parent;
1596
1597 out_fdc(fdc, NE7CMD_DUMPREG);
1598 fdcresult(fdc);
1599 printf("dumpreg(%d regs): <", fdc->sc_nstat);
1600 for (i = 0; i < fdc->sc_nstat; i++)
1601 printf(" %x", fdc->sc_status[i]);
1602 printf(">\n");
1603 }
1604
1605 return 0;
1606 case _IOW('f', 101, int):
1607 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg &=
1608 ~CFG_THRHLD_MASK;
1609 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg |=
1610 (*(int *)addr & CFG_THRHLD_MASK);
1611 fdconf((struct fdc_softc *) fd->sc_dv.dv_parent);
1612 return 0;
1613 case _IO('f', 102):
1614 {
1615 int i;
1616 struct fdc_softc *fdc = (struct fdc_softc *)
1617 fd->sc_dv.dv_parent;
1618 out_fdc(fdc, NE7CMD_SENSEI);
1619 fdcresult(fdc);
1620 printf("sensei(%d regs): <", fdc->sc_nstat);
1621 for (i=0; i< fdc->sc_nstat; i++)
1622 printf(" 0x%x", fdc->sc_status[i]);
1623 }
1624 printf(">\n");
1625 return 0;
1626 #endif
1627 default:
1628 return ENOTTY;
1629 }
1630
1631 #ifdef DIAGNOSTIC
1632 panic("fdioctl: impossible");
1633 #endif
1634 }
1635
1636 void
1637 fdgetdisklabel(dev)
1638 dev_t dev;
1639 {
1640 int unit = FDUNIT(dev), i;
1641 struct fd_softc *fd = fd_cd.cd_devs[unit];
1642 struct disklabel *lp = fd->sc_dk.dk_label;
1643 struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
1644
1645 bzero(lp, sizeof(struct disklabel));
1646 bzero(lp, sizeof(struct cpu_disklabel));
1647
1648 lp->d_type = DTYPE_FLOPPY;
1649 lp->d_secsize = FDC_BSIZE;
1650 lp->d_secpercyl = fd->sc_type->seccyl;
1651 lp->d_nsectors = fd->sc_type->sectrac;
1652 lp->d_ncylinders = fd->sc_type->tracks;
1653 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
1654 lp->d_rpm = 3600; /* XXX like it matters... */
1655
1656 strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
1657 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1658 lp->d_interleave = 1;
1659
1660 lp->d_partitions[RAW_PART].p_offset = 0;
1661 lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
1662 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1663 lp->d_npartitions = RAW_PART + 1;
1664
1665 lp->d_magic = DISKMAGIC;
1666 lp->d_magic2 = DISKMAGIC;
1667 lp->d_checksum = dkcksum(lp);
1668
1669 /*
1670 * Call the generic disklabel extraction routine. If there's
1671 * not a label there, fake it.
1672 */
1673 if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
1674 strncpy(lp->d_packname, "default label",
1675 sizeof(lp->d_packname));
1676 /*
1677 * Reset the partition info; it might have gotten
1678 * trashed in readdisklabel().
1679 *
1680 * XXX Why do we have to do this? readdisklabel()
1681 * should be safe...
1682 */
1683 for (i = 0; i < MAXPARTITIONS; ++i) {
1684 lp->d_partitions[i].p_offset = 0;
1685 if (i == RAW_PART) {
1686 lp->d_partitions[i].p_size =
1687 lp->d_secpercyl * lp->d_ncylinders;
1688 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1689 } else {
1690 lp->d_partitions[i].p_size = 0;
1691 lp->d_partitions[i].p_fstype = FS_UNUSED;
1692 }
1693 }
1694 lp->d_npartitions = RAW_PART + 1;
1695 }
1696 }
1697
1698 void
1699 fd_do_eject()
1700 {
1701
1702 auxregbisc(AUXIO_FDS, AUXIO_FEJ);
1703 delay(10);
1704 auxregbisc(AUXIO_FEJ, AUXIO_FDS);
1705 }
1706
1707 #ifdef RAMDISK_HOOKS
1708 int fd_read_rd_image __P((size_t *, caddr_t *));
1709 #endif
1710
1711 /* ARGSUSED */
1712 void
1713 fd_mountroot_hook(dev)
1714 struct device *dev;
1715 {
1716 int c;
1717
1718 fd_do_eject();
1719 printf("Insert filesystem floppy and press return.");
1720 for (;;) {
1721 c = cngetc();
1722 if ((c == '\r') || (c == '\n')) {
1723 printf("\n");
1724 break;
1725 }
1726 }
1727 #ifdef RAMDISK_HOOKS
1728 {
1729 extern int (*rd_read_image) __P((size_t *, caddr_t *));
1730 rd_read_image = fd_read_rd_image;
1731 }
1732 #endif
1733 }
1734
1735 #ifdef RAMDISK_HOOKS
1736 #include <sys/malloc.h>
1737 #include <sys/proc.h>
1738
1739 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
1740
1741 int
1742 fd_read_rd_image(sizep, addrp)
1743 size_t *sizep;
1744 caddr_t *addrp;
1745 {
1746 struct buf buf, *bp = &buf;
1747 dev_t dev;
1748 off_t offset;
1749 caddr_t addr;
1750
1751 dev = makedev(54,0); /* XXX */
1752
1753 MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
1754 *addrp = addr;
1755
1756 if (fdopen(dev, 0, S_IFCHR, NULL))
1757 panic("fd: mountroot: fdopen");
1758
1759 offset = 0;
1760
1761 for (;;) {
1762 bp->b_dev = dev;
1763 bp->b_error = 0;
1764 bp->b_resid = 0;
1765 bp->b_proc = NULL;
1766 bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
1767 bp->b_blkno = btodb(offset);
1768 bp->b_bcount = DEV_BSIZE;
1769 bp->b_data = addr;
1770 fdstrategy(bp);
1771 while ((bp->b_flags & B_DONE) == 0) {
1772 tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0);
1773 }
1774 if (bp->b_error)
1775 panic("fd: mountroot: fdread error %d", bp->b_error);
1776
1777 if (bp->b_resid != 0)
1778 break;
1779
1780 addr += DEV_BSIZE;
1781 offset += DEV_BSIZE;
1782 if (offset + DEV_BSIZE > FDMICROROOTSIZE)
1783 break;
1784 }
1785 (void)fdclose(dev, 0, S_IFCHR, NULL);
1786 *sizep = offset;
1787 fd_do_eject();
1788 return 0;
1789 }
1790 #endif
1791