fd.c revision 1.26 1 /* $NetBSD: fd.c,v 1.26 2006/04/14 13:09:05 blymn Exp $ */
2 /* $OpenBSD: fd.c,v 1.6 1998/10/03 21:18:57 millert Exp $ */
3 /* NetBSD: fd.c,v 1.78 1995/07/04 07:23:09 mycroft Exp */
4
5 /*-
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Charles M. Hannum.
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 NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*-
42 * Copyright (c) 1990 The Regents of the University of California.
43 * All rights reserved.
44 *
45 * This code is derived from software contributed to Berkeley by
46 * Don Ahn.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)fd.c 7.4 (Berkeley) 5/25/91
73 */
74
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.26 2006/04/14 13:09:05 blymn Exp $");
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/callout.h>
81 #include <sys/kernel.h>
82 #include <sys/conf.h>
83 #include <sys/file.h>
84 #include <sys/ioctl.h>
85 #include <sys/device.h>
86 #include <sys/disklabel.h>
87 #include <sys/disk.h>
88 #include <sys/buf.h>
89 #include <sys/bufq.h>
90 #include <sys/uio.h>
91 #include <sys/syslog.h>
92 #include <sys/queue.h>
93
94 #include <uvm/uvm_extern.h>
95
96 #include <dev/cons.h>
97
98 #include <machine/bus.h>
99 #include <machine/cpu.h>
100
101 #include <arc/jazz/fdreg.h>
102 #include <arc/jazz/fdcvar.h>
103
104 #include "locators.h"
105
106 #define FDUNIT(dev) DISKUNIT(dev)
107 #define FDTYPE(dev) DISKPART(dev)
108
109 /* controller driver configuration */
110 int fdprint(void *, const char *);
111
112 /*
113 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
114 * we tell them apart.
115 */
116 struct fd_type {
117 int sectrac; /* sectors per track */
118 int heads; /* number of heads */
119 int seccyl; /* sectors per cylinder */
120 int secsize; /* size code for sectors */
121 int datalen; /* data len when secsize = 0 */
122 int steprate; /* step rate and head unload time */
123 int gap1; /* gap len between sectors */
124 int gap2; /* formatting gap */
125 int cyls; /* total num of cylinders */
126 int size; /* size of disk in sectors */
127 int step; /* steps per cylinder */
128 int rate; /* transfer speed code */
129 const char *name;
130 };
131
132 /* The order of entries in the following table is important -- BEWARE! */
133 struct fd_type fd_types[] = {
134 /* 1.44MB diskette */
135 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" },
136 /* 1.2 MB AT-diskettes */
137 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB" },
138 /* 360kB in 1.2MB drive */
139 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" },
140 /* 360kB PC diskettes */
141 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" },
142 /* 3.5" 720kB diskette */
143 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB" },
144 /* 720kB in 1.2MB drive */
145 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x" },
146 /* 360kB in 720kB drive */
147 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x" },
148 };
149
150 /* software state, per disk (with up to 4 disks per ctlr) */
151 struct fd_softc {
152 struct device sc_dev;
153 struct disk sc_dk;
154
155 const struct fd_type *sc_deftype; /* default type descriptor */
156 struct fd_type *sc_type; /* current type descriptor */
157 struct fd_type sc_type_copy; /* copy for fiddling when formatting */
158
159 struct callout sc_motoron_ch;
160 struct callout sc_motoroff_ch;
161
162 daddr_t sc_blkno; /* starting block number */
163 int sc_bcount; /* byte count left */
164 int sc_opts; /* user-set options */
165 int sc_skip; /* bytes already transferred */
166 int sc_nblks; /* number of blocks currently transferring */
167 int sc_nbytes; /* number of bytes currently transferring */
168
169 int sc_drive; /* physical unit number */
170 int sc_flags;
171 #define FD_OPEN 0x01 /* it's open */
172 #define FD_MOTOR 0x02 /* motor should be on */
173 #define FD_MOTOR_WAIT 0x04 /* motor coming up */
174 int sc_cylin; /* where we think the head is */
175
176 void *sc_sdhook; /* saved shutdown hook for drive. */
177
178 TAILQ_ENTRY(fd_softc) sc_drivechain;
179 int sc_ops; /* I/O ops since last switch */
180 struct bufq_state *sc_q;/* pending I/O requests */
181 int sc_active; /* number of active I/O operations */
182 };
183
184 /* floppy driver configuration */
185 int fdprobe(struct device *, struct cfdata *, void *);
186 void fdattach(struct device *, struct device *, void *);
187
188 extern struct cfdriver fd_cd;
189
190 CFATTACH_DECL(fd, sizeof(struct fd_softc), fdprobe, fdattach, NULL, NULL);
191
192 dev_type_open(fdopen);
193 dev_type_close(fdclose);
194 dev_type_read(fdread);
195 dev_type_write(fdwrite);
196 dev_type_ioctl(fdioctl);
197 dev_type_strategy(fdstrategy);
198
199 const struct bdevsw fd_bdevsw = {
200 fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
201 };
202
203 const struct cdevsw fd_cdevsw = {
204 fdopen, fdclose, fdread, fdwrite, fdioctl,
205 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
206 };
207
208 void fdgetdisklabel(struct fd_softc *);
209 int fd_get_parms(struct fd_softc *);
210 void fdstrategy(struct buf *);
211 void fdstart(struct fd_softc *);
212
213 struct dkdriver fddkdriver = { fdstrategy };
214
215 #if 0
216 const struct fd_type *fd_nvtotype(char *, int, int);
217 #endif
218 void fd_set_motor(struct fdc_softc *, int);
219 void fd_motor_off(void *);
220 void fd_motor_on(void *);
221 int fdcresult(struct fdc_softc *);
222 void fdcstart(struct fdc_softc *);
223 void fdcstatus(struct device *, int, const char *);
224 void fdctimeout(void *);
225 void fdcpseudointr(void *);
226 void fdcretry(struct fdc_softc *);
227 void fdfinish(struct fd_softc *, struct buf *);
228 inline const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
229 void fd_mountroot_hook(struct device *);
230
231 /*
232 * Arguments passed between fdcattach and fdprobe.
233 */
234 struct fdc_attach_args {
235 int fa_drive;
236 const struct fd_type *fa_deftype;
237 };
238
239 /*
240 * Print the location of a disk drive (called just before attaching the
241 * the drive). If `fdc' is not NULL, the drive was found but was not
242 * in the system config file; print the drive name as well.
243 * Return QUIET (config_find ignores this if the device was configured) to
244 * avoid printing `fdN not configured' messages.
245 */
246 int
247 fdprint(void *aux, const char *fdc)
248 {
249 struct fdc_attach_args *fa = aux;
250
251 if (!fdc)
252 aprint_normal(" drive %d", fa->fa_drive);
253 return QUIET;
254 }
255
256 void
257 fdcattach(struct fdc_softc *fdc)
258 {
259 struct fdc_attach_args fa;
260 bus_space_tag_t iot;
261 bus_space_handle_t ioh;
262 int type;
263
264 iot = fdc->sc_iot;
265 ioh = fdc->sc_ioh;
266 callout_init(&fdc->sc_timo_ch);
267 callout_init(&fdc->sc_intr_ch);
268
269 fdc->sc_state = DEVIDLE;
270 TAILQ_INIT(&fdc->sc_drives);
271
272 /*
273 * No way yet to determine default disk types.
274 * we assume 1.44 3.5" type for the moment.
275 */
276 type = 0;
277
278 /* physical limit: two drives per controller. */
279 for (fa.fa_drive = 0; fa.fa_drive < 2; fa.fa_drive++) {
280 fa.fa_deftype = &fd_types[type];
281 (void)config_found(&fdc->sc_dev, (void *)&fa, fdprint);
282 }
283 }
284
285 int
286 fdprobe(struct device *parent, struct cfdata *match, void *aux)
287 {
288 struct fdc_softc *fdc = (void *)parent;
289 struct cfdata *cf = match;
290 struct fdc_attach_args *fa = aux;
291 int drive = fa->fa_drive;
292 bus_space_tag_t iot = fdc->sc_iot;
293 bus_space_handle_t ioh = fdc->sc_ioh;
294 int n;
295
296 if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
297 cf->cf_loc[FDCCF_DRIVE] != drive)
298 return 0;
299
300 /* select drive and turn on motor */
301 bus_space_write_1(iot, ioh, FDOUT, drive | FDO_FRST | FDO_MOEN(drive));
302 /* wait for motor to spin up */
303 delay(250000);
304 out_fdc(iot, ioh, NE7CMD_RECAL);
305 out_fdc(iot, ioh, drive);
306 /* wait for recalibrate */
307 delay(2000000);
308 out_fdc(iot, ioh, NE7CMD_SENSEI);
309 n = fdcresult(fdc);
310 #ifdef FD_DEBUG
311 {
312 int i;
313 printf("fdprobe: status");
314 for (i = 0; i < n; i++)
315 printf(" %x", fdc->sc_status[i]);
316 printf("\n");
317 }
318 #endif
319 if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
320 return 0;
321 /* turn off motor */
322 bus_space_write_1(iot, ioh, FDOUT, FDO_FRST);
323
324 return 1;
325 }
326
327 /*
328 * Controller is working, and drive responded. Attach it.
329 */
330 void
331 fdattach(struct device *parent, struct device *self, void *aux)
332 {
333 struct fdc_softc *fdc = (void *)parent;
334 struct fd_softc *fd = (void *)self;
335 struct fdc_attach_args *fa = aux;
336 const struct fd_type *type = fa->fa_deftype;
337 int drive = fa->fa_drive;
338
339 callout_init(&fd->sc_motoron_ch);
340 callout_init(&fd->sc_motoroff_ch);
341
342 /* XXX Allow `flags' to override device type? */
343
344 if (type)
345 printf(": %s, %d cyl, %d head, %d sec\n", type->name,
346 type->cyls, type->heads, type->sectrac);
347 else
348 printf(": density unknown\n");
349
350 bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
351 fd->sc_cylin = -1;
352 fd->sc_drive = drive;
353 fd->sc_deftype = type;
354 fdc->sc_fd[drive] = fd;
355
356 /*
357 * Initialize and attach the disk structure.
358 */
359 fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
360 fd->sc_dk.dk_driver = &fddkdriver;
361 disk_attach(&fd->sc_dk);
362
363 /* Establish a mountroot hook. */
364 mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
365
366 /* Needed to power off if the motor is on when we halt. */
367 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
368 }
369
370 #if 0
371 /*
372 * Translate nvram type into internal data structure. Return NULL for
373 * none/unknown/unusable.
374 */
375 const struct fd_type *
376 fd_nvtotype(char *fdc, int nvraminfo, int drive)
377 {
378 int type;
379
380 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
381 #if 0
382 switch (type) {
383 case NVRAM_DISKETTE_NONE:
384 return NULL;
385 case NVRAM_DISKETTE_12M:
386 return &fd_types[1];
387 case NVRAM_DISKETTE_TYPE5:
388 case NVRAM_DISKETTE_TYPE6:
389 /* XXX We really ought to handle 2.88MB format. */
390 case NVRAM_DISKETTE_144M:
391 return &fd_types[0];
392 case NVRAM_DISKETTE_360K:
393 return &fd_types[3];
394 case NVRAM_DISKETTE_720K:
395 return &fd_types[4];
396 default:
397 printf("%s: drive %d: unknown device type 0x%x\n",
398 fdc, drive, type);
399 return NULL;
400 }
401 #else
402 return &fd_types[0]; /* Use only 1.44 for now */
403 #endif
404 }
405 #endif
406
407 inline const struct fd_type *
408 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
409 {
410 int type = FDTYPE(dev);
411
412 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
413 return NULL;
414 return type ? &fd_types[type - 1] : fd->sc_deftype;
415 }
416
417 void
418 fdstrategy(struct buf *bp)
419 {
420 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(bp->b_dev));
421 int sz;
422 int s;
423
424 /* Valid unit, controller, and request? */
425 if (bp->b_blkno < 0 ||
426 (bp->b_bcount % FDC_BSIZE) != 0) {
427 bp->b_error = EINVAL;
428 goto bad;
429 }
430
431 /* If it's a null transfer, return immediately. */
432 if (bp->b_bcount == 0)
433 goto done;
434
435 sz = howmany(bp->b_bcount, FDC_BSIZE);
436
437 if (bp->b_blkno + sz > fd->sc_type->size) {
438 sz = fd->sc_type->size - bp->b_blkno;
439 if (sz == 0) {
440 /* If exactly at end of disk, return EOF. */
441 goto done;
442 }
443 if (sz < 0) {
444 /* If past end of disk, return EINVAL. */
445 bp->b_error = EINVAL;
446 goto bad;
447 }
448 /* Otherwise, truncate request. */
449 bp->b_bcount = sz << DEV_BSHIFT;
450 }
451
452 bp->b_rawblkno = bp->b_blkno;
453 bp->b_cylinder =
454 bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
455
456 #ifdef FD_DEBUG
457 printf("fdstrategy: b_blkno %" PRId64 " b_bcount %ld blkno %" PRId64
458 " cylin %ld sz %d\n",
459 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
460 #endif
461
462 /* Queue transfer on drive, activate drive and controller if idle. */
463 s = splbio();
464 BUFQ_PUT(fd->sc_q, bp);
465 callout_stop(&fd->sc_motoroff_ch); /* a good idea */
466 if (fd->sc_active == 0)
467 fdstart(fd);
468 #ifdef DIAGNOSTIC
469 else {
470 struct fdc_softc *fdc = (void *) device_parent(&fd->sc_dev);
471 if (fdc->sc_state == DEVIDLE) {
472 printf("fdstrategy: controller inactive\n");
473 fdcstart(fdc);
474 }
475 }
476 #endif
477 splx(s);
478 return;
479
480 bad:
481 bp->b_flags |= B_ERROR;
482 done:
483 /* Toss transfer; we're done early. */
484 bp->b_resid = bp->b_bcount;
485 biodone(bp);
486 }
487
488 void
489 fdstart(struct fd_softc *fd)
490 {
491 struct fdc_softc *fdc = (void *) device_parent(&fd->sc_dev);
492 int active = TAILQ_FIRST(&fdc->sc_drives) != 0;
493
494 /* Link into controller queue. */
495 fd->sc_active = 1;
496 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
497
498 /* If controller not already active, start it. */
499 if (!active)
500 fdcstart(fdc);
501 }
502
503 void
504 fdfinish(struct fd_softc *fd, struct buf *bp)
505 {
506 struct fdc_softc *fdc = (void *) device_parent(&fd->sc_dev);
507
508 /*
509 * Move this drive to the end of the queue to give others a `fair'
510 * chance. We only force a switch if N operations are completed while
511 * another drive is waiting to be serviced, since there is a long motor
512 * startup delay whenever we switch.
513 */
514 (void)BUFQ_GET(fd->sc_q);
515 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
516 fd->sc_ops = 0;
517 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
518 if (BUFQ_PEEK(fd->sc_q) != NULL)
519 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
520 else
521 fd->sc_active = 0;
522 }
523 bp->b_resid = fd->sc_bcount;
524 fd->sc_skip = 0;
525 biodone(bp);
526 /* turn off motor 5s from now */
527 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
528 fdc->sc_state = DEVIDLE;
529 }
530
531 int
532 fdread(dev_t dev, struct uio *uio, int flags)
533 {
534
535 return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
536 }
537
538 int
539 fdwrite(dev_t dev, struct uio *uio, int flags)
540 {
541
542 return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
543 }
544
545 void
546 fd_set_motor(struct fdc_softc *fdc, int reset)
547 {
548 struct fd_softc *fd;
549 u_char status;
550 int n;
551
552 if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
553 status = fd->sc_drive;
554 else
555 status = 0;
556 if (!reset)
557 status |= FDO_FRST | FDO_FDMAEN;
558 for (n = 0; n < 4; n++)
559 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
560 status |= FDO_MOEN(n);
561 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, FDOUT, status);
562 }
563
564 void
565 fd_motor_off(void *arg)
566 {
567 struct fd_softc *fd = arg;
568 int s;
569
570 s = splbio();
571 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
572 fd_set_motor((struct fdc_softc *) device_parent(&fd->sc_dev), 0);
573 splx(s);
574 }
575
576 void
577 fd_motor_on(void *arg)
578 {
579 struct fd_softc *fd = arg;
580 struct fdc_softc *fdc = (void *) device_parent(&fd->sc_dev);
581 int s;
582
583 s = splbio();
584 fd->sc_flags &= ~FD_MOTOR_WAIT;
585 if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&
586 (fdc->sc_state == MOTORWAIT))
587 (void) fdcintr(fdc);
588 splx(s);
589 }
590
591 int
592 fdcresult(struct fdc_softc *fdc)
593 {
594 bus_space_tag_t iot = fdc->sc_iot;
595 bus_space_handle_t ioh = fdc->sc_ioh;
596 u_char i;
597 int j = 100000,
598 n = 0;
599
600 for (; j; j--) {
601 i = bus_space_read_1(iot, ioh, FDSTS) &
602 (NE7_DIO | NE7_RQM | NE7_CB);
603 if (i == NE7_RQM)
604 return n;
605 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
606 if (n >= sizeof(fdc->sc_status)) {
607 log(LOG_ERR, "fdcresult: overrun\n");
608 return -1;
609 }
610 fdc->sc_status[n++] =
611 bus_space_read_1(iot, ioh, FDDATA);
612 }
613 delay(10);
614 }
615 log(LOG_ERR, "fdcresult: timeout\n");
616 return -1;
617 }
618
619 int
620 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x)
621 {
622 int i = 100000;
623
624 while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_DIO) && i-- > 0);
625 if (i <= 0)
626 return -1;
627 while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_RQM) == 0 && i-- > 0);
628 if (i <= 0)
629 return -1;
630 bus_space_write_1(iot, ioh, FDDATA, x);
631 return 0;
632 }
633
634 int
635 fdopen(dev_t dev, int flags, int mode, struct lwp *l)
636 {
637 struct fd_softc *fd;
638 const struct fd_type *type;
639
640 fd = device_lookup(&fd_cd, FDUNIT(dev));
641 if (fd == NULL)
642 return ENXIO;
643
644 type = fd_dev_to_type(fd, dev);
645 if (type == NULL)
646 return ENXIO;
647
648 if ((fd->sc_flags & FD_OPEN) != 0 &&
649 memcmp(fd->sc_type, type, sizeof(*type)))
650 return EBUSY;
651
652 fd->sc_type_copy = *type;
653 fd->sc_type = &fd->sc_type_copy;
654 fd->sc_cylin = -1;
655 fd->sc_flags |= FD_OPEN;
656
657 return 0;
658 }
659
660 int
661 fdclose(dev_t dev, int flags, int mode, struct lwp *l)
662 {
663 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
664
665 fd->sc_flags &= ~FD_OPEN;
666 return 0;
667 }
668
669 void
670 fdcstart(struct fdc_softc *fdc)
671 {
672
673 #ifdef DIAGNOSTIC
674 /* only got here if controller's drive queue was inactive; should
675 be in idle state */
676 if (fdc->sc_state != DEVIDLE) {
677 printf("fdcstart: not idle\n");
678 return;
679 }
680 #endif
681 (void) fdcintr(fdc);
682 }
683
684 void
685 fdcstatus(struct device *dv, int n, const char *s)
686 {
687 struct fdc_softc *fdc = (void *) device_parent(dv);
688 char bits[64];
689
690 if (n == 0) {
691 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
692 (void) fdcresult(fdc);
693 n = 2;
694 }
695
696 printf("%s: %s", dv->dv_xname, s);
697
698 switch (n) {
699 case 0:
700 printf("\n");
701 break;
702 case 2:
703 printf(" (st0 %s cyl %d)\n",
704 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
705 bits, sizeof(bits)), fdc->sc_status[1]);
706 break;
707 case 7:
708 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
709 NE7_ST0BITS, bits, sizeof(bits)));
710 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
711 NE7_ST1BITS, bits, sizeof(bits)));
712 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
713 NE7_ST2BITS, bits, sizeof(bits)));
714 printf(" cyl %d head %d sec %d)\n",
715 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
716 break;
717 #ifdef DIAGNOSTIC
718 default:
719 printf("\nfdcstatus: weird size");
720 break;
721 #endif
722 }
723 }
724
725 void
726 fdctimeout(void *arg)
727 {
728 struct fdc_softc *fdc = arg;
729 struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
730 int s;
731
732 s = splbio();
733 #ifdef DEBUG
734 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
735 #endif
736 fdcstatus(&fd->sc_dev, 0, "timeout");
737
738 if (BUFQ_PEEK(fd->sc_q) != NULL)
739 fdc->sc_state++;
740 else
741 fdc->sc_state = DEVIDLE;
742
743 (void) fdcintr(fdc);
744 splx(s);
745 }
746
747 void
748 fdcpseudointr(void *arg)
749 {
750 int s;
751
752 /* Just ensure it has the right spl. */
753 s = splbio();
754 (void) fdcintr(arg);
755 splx(s);
756 }
757
758 int
759 fdcintr(void *arg)
760 {
761 struct fdc_softc *fdc = arg;
762 #define st0 fdc->sc_status[0]
763 #define cyl fdc->sc_status[1]
764 struct fd_softc *fd;
765 struct buf *bp;
766 bus_space_tag_t iot = fdc->sc_iot;
767 bus_space_handle_t ioh = fdc->sc_ioh;
768 int read, head, sec, i, nblks;
769 struct fd_type *type;
770
771 loop:
772 /* Is there a drive for the controller to do a transfer with? */
773 fd = TAILQ_FIRST(&fdc->sc_drives);
774 if (fd == NULL) {
775 fdc->sc_state = DEVIDLE;
776 return 1;
777 }
778
779 /* Is there a transfer to this drive? If not, deactivate drive. */
780 bp = BUFQ_PEEK(fd->sc_q);
781 if (bp == NULL) {
782 fd->sc_ops = 0;
783 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
784 fd->sc_active = 0;
785 goto loop;
786 }
787
788 switch (fdc->sc_state) {
789 case DEVIDLE:
790 fdc->sc_errors = 0;
791 fd->sc_skip = 0;
792 fd->sc_bcount = bp->b_bcount;
793 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
794 callout_stop(&fd->sc_motoroff_ch);
795 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
796 fdc->sc_state = MOTORWAIT;
797 return 1;
798 }
799 if ((fd->sc_flags & FD_MOTOR) == 0) {
800 /* Turn on the motor, being careful about pairing. */
801 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
802 if (ofd && ofd->sc_flags & FD_MOTOR) {
803 callout_stop(&ofd->sc_motoroff_ch);
804 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
805 }
806 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
807 fd_set_motor(fdc, 0);
808 fdc->sc_state = MOTORWAIT;
809 /* Allow .25s for motor to stabilize. */
810 callout_reset(&fd->sc_motoron_ch, hz / 4,
811 fd_motor_on, fd);
812 return 1;
813 }
814 /* Make sure the right drive is selected. */
815 fd_set_motor(fdc, 0);
816
817 /* fall through */
818 case DOSEEK:
819 doseek:
820 if (fd->sc_cylin == bp->b_cylinder)
821 goto doio;
822
823 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
824 out_fdc(iot, ioh, fd->sc_type->steprate);
825 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
826
827 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
828 out_fdc(iot, ioh, fd->sc_drive); /* drive number */
829 out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
830
831 fd->sc_cylin = -1;
832 fdc->sc_state = SEEKWAIT;
833
834 iostat_seek(fd->sc_dk.dk_stats);
835 disk_busy(&fd->sc_dk);
836
837 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
838 return 1;
839
840 case DOIO:
841 doio:
842 type = fd->sc_type;
843 sec = fd->sc_blkno % type->seccyl;
844 nblks = type->seccyl - sec;
845 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
846 nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
847 fd->sc_nblks = nblks;
848 fd->sc_nbytes = nblks * FDC_BSIZE;
849 head = sec / type->sectrac;
850 sec -= head * type->sectrac;
851 #ifdef DIAGNOSTIC
852 {
853 int block;
854 block = (fd->sc_cylin * type->heads + head) *
855 type->sectrac + sec;
856 if (block != fd->sc_blkno) {
857 printf("fdcintr: block %d != blkno %" PRId64
858 "\n", block, fd->sc_blkno);
859 #ifdef DDB
860 Debugger();
861 #endif
862 }
863 }
864 #endif
865 read = (bp->b_flags & B_READ) != 0;
866 FDCDMA_START(fdc, bp->b_data + fd->sc_skip,
867 fd->sc_nbytes, read);
868 bus_space_write_1(iot, ioh, FDCTL, type->rate);
869 #ifdef FD_DEBUG
870 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
871 read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
872 sec, nblks);
873 #endif
874 if (read)
875 out_fdc(iot, ioh, NE7CMD_READ); /* READ */
876 else
877 out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
878 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
879 out_fdc(iot, ioh, fd->sc_cylin); /* track */
880 out_fdc(iot, ioh, head);
881 out_fdc(iot, ioh, sec + 1); /* sector + 1 */
882 out_fdc(iot, ioh, type->secsize); /* sector size */
883 out_fdc(iot, ioh, type->sectrac); /* sectors/track */
884 out_fdc(iot, ioh, type->gap1); /* gap1 size */
885 out_fdc(iot, ioh, type->datalen); /* data length */
886 fdc->sc_state = IOCOMPLETE;
887
888 disk_busy(&fd->sc_dk);
889
890 /* allow 2 seconds for operation */
891 callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
892 return 1; /* will return later */
893
894 case SEEKWAIT:
895 callout_stop(&fdc->sc_timo_ch);
896 fdc->sc_state = SEEKCOMPLETE;
897 /* allow 1/50 second for heads to settle */
898 callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
899 return 1;
900
901 case SEEKCOMPLETE:
902 disk_unbusy(&fd->sc_dk, 0, 0);
903
904 /* Make sure seek really happened. */
905 out_fdc(iot, ioh, NE7CMD_SENSEI);
906 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
907 cyl != bp->b_cylinder * fd->sc_type->step) {
908 #ifdef FD_DEBUG
909 fdcstatus(&fd->sc_dev, 2, "seek failed");
910 #endif
911 fdcretry(fdc);
912 goto loop;
913 }
914 fd->sc_cylin = bp->b_cylinder;
915 goto doio;
916
917 case IOTIMEDOUT:
918 FDCDMA_ABORT(fdc);
919
920 case SEEKTIMEDOUT:
921 case RECALTIMEDOUT:
922 case RESETTIMEDOUT:
923 fdcretry(fdc);
924 goto loop;
925
926 case IOCOMPLETE: /* IO DONE, post-analyze */
927 callout_stop(&fdc->sc_timo_ch);
928
929 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
930 (bp->b_flags & B_READ));
931
932 i = fdcresult(fdc);
933 if (i != 7 || (st0 & 0xf8) != 0) {
934 FDCDMA_ABORT(fdc);
935 #ifdef FD_DEBUG
936 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
937 "read failed" : "write failed");
938 printf("blkno %" PRId64 " nblks %d\n",
939 fd->sc_blkno, fd->sc_nblks);
940 #endif
941 fdcretry(fdc);
942 goto loop;
943 }
944 FDCDMA_DONE(fdc);
945 if (fdc->sc_errors) {
946 diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
947 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
948 printf("\n");
949 fdc->sc_errors = 0;
950 }
951 fd->sc_blkno += fd->sc_nblks;
952 fd->sc_skip += fd->sc_nbytes;
953 fd->sc_bcount -= fd->sc_nbytes;
954 if (fd->sc_bcount > 0) {
955 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
956 goto doseek;
957 }
958 fdfinish(fd, bp);
959 goto loop;
960
961 case DORESET:
962 /* try a reset, keep motor on */
963 fd_set_motor(fdc, 1);
964 delay(100);
965 fd_set_motor(fdc, 0);
966 fdc->sc_state = RESETCOMPLETE;
967 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
968 return 1; /* will return later */
969
970 case RESETCOMPLETE:
971 callout_stop(&fdc->sc_timo_ch);
972 /* clear the controller output buffer */
973 for (i = 0; i < 4; i++) {
974 out_fdc(iot, ioh, NE7CMD_SENSEI);
975 (void) fdcresult(fdc);
976 }
977
978 /* fall through */
979 case DORECAL:
980 out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
981 out_fdc(iot, ioh, fd->sc_drive);
982 fdc->sc_state = RECALWAIT;
983 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
984 return 1; /* will return later */
985
986 case RECALWAIT:
987 callout_stop(&fdc->sc_timo_ch);
988 fdc->sc_state = RECALCOMPLETE;
989 /* allow 1/30 second for heads to settle */
990 callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
991 return 1; /* will return later */
992
993 case RECALCOMPLETE:
994 out_fdc(iot, ioh, NE7CMD_SENSEI);
995 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
996 #ifdef FD_DEBUG
997 fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
998 #endif
999 fdcretry(fdc);
1000 goto loop;
1001 }
1002 fd->sc_cylin = 0;
1003 goto doseek;
1004
1005 case MOTORWAIT:
1006 if (fd->sc_flags & FD_MOTOR_WAIT)
1007 return 1; /* time's not up yet */
1008 goto doseek;
1009
1010 default:
1011 fdcstatus(&fd->sc_dev, 0, "stray interrupt");
1012 return 1;
1013 }
1014 #ifdef DIAGNOSTIC
1015 panic("fdcintr: impossible");
1016 #endif
1017 #undef st0
1018 #undef cyl
1019 }
1020
1021 void
1022 fdcretry(struct fdc_softc *fdc)
1023 {
1024 struct fd_softc *fd;
1025 struct buf *bp;
1026 char bits[64];
1027
1028 fd = TAILQ_FIRST(&fdc->sc_drives);
1029 bp = BUFQ_PEEK(fd->sc_q);
1030
1031 switch (fdc->sc_errors) {
1032 case 0:
1033 /* try again */
1034 fdc->sc_state = DOSEEK;
1035 break;
1036
1037 case 1: case 2: case 3:
1038 /* didn't work; try recalibrating */
1039 fdc->sc_state = DORECAL;
1040 break;
1041
1042 case 4:
1043 /* still no go; reset the bastard */
1044 fdc->sc_state = DORESET;
1045 break;
1046
1047 default:
1048 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1049 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1050
1051 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1052 NE7_ST0BITS, bits, sizeof(bits)));
1053 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1054 NE7_ST1BITS, bits, sizeof(bits)));
1055 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1056 NE7_ST2BITS, bits, sizeof(bits)));
1057 printf(" cyl %d head %d sec %d)\n",
1058 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1059
1060 bp->b_flags |= B_ERROR;
1061 bp->b_error = EIO;
1062 fdfinish(fd, bp);
1063 }
1064 fdc->sc_errors++;
1065 }
1066
1067 int
1068 fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct lwp *l)
1069 {
1070 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1071 struct disklabel buffer;
1072 int error;
1073
1074 switch (cmd) {
1075 case DIOCGDINFO:
1076 memset(&buffer, 0, sizeof(buffer));
1077
1078 buffer.d_secpercyl = fd->sc_type->seccyl;
1079 buffer.d_type = DTYPE_FLOPPY;
1080 buffer.d_secsize = FDC_BSIZE;
1081
1082 if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1083 return EINVAL;
1084
1085 *(struct disklabel *)addr = buffer;
1086 return 0;
1087
1088 case DIOCWLABEL:
1089 if ((flag & FWRITE) == 0)
1090 return EBADF;
1091 /* XXX do something */
1092 return 0;
1093
1094 case DIOCWDINFO:
1095 if ((flag & FWRITE) == 0)
1096 return EBADF;
1097
1098 error = setdisklabel(&buffer, (struct disklabel *)addr,
1099 0, NULL);
1100 if (error)
1101 return error;
1102
1103 error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1104 return error;
1105
1106 default:
1107 return ENOTTY;
1108 }
1109
1110 #ifdef DIAGNOSTIC
1111 panic("fdioctl: impossible");
1112 #endif
1113 }
1114
1115 /*
1116 * Mountroot hook: prompt the user to enter the root file system floppy.
1117 */
1118 void
1119 fd_mountroot_hook(struct device *dev)
1120 {
1121 int c;
1122
1123 printf("Insert filesystem floppy and press return.");
1124 cnpollc(1);
1125 for (;;) {
1126 c = cngetc();
1127 if ((c == '\r') || (c == '\n')) {
1128 printf("\n");
1129 break;
1130 }
1131 }
1132 cnpollc(0);
1133 }
1134