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