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