FreeBSD/Linux Kernel Cross Reference
sys/dev/fdc/fdc.c
1 /*-
2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Don Ahn.
8 *
9 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
10 * aided by the Linux floppy driver modifications from David Bateman
11 * (dbateman@eng.uts.edu.au).
12 *
13 * Copyright (c) 1993, 1994 by
14 * jc@irbs.UUCP (John Capo)
15 * vak@zebub.msk.su (Serge Vakulenko)
16 * ache@astral.msk.su (Andrew A. Chernov)
17 *
18 * Copyright (c) 1993, 1994, 1995 by
19 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
20 * dufault@hda.com (Peter Dufault)
21 *
22 * Copyright (c) 2001 Joerg Wunsch,
23 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
24 *
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
27 * are met:
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 4. Neither the name of the University nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 *
49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
50 *
51 */
52
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD: releng/6.0/sys/dev/fdc/fdc.c 149699 2005-09-02 00:33:30Z rodrigc $");
55
56 #include "opt_fdc.h"
57
58 #include <sys/param.h>
59 #include <sys/bio.h>
60 #include <sys/bus.h>
61 #include <sys/devicestat.h>
62 #include <sys/disk.h>
63 #include <sys/fcntl.h>
64 #include <sys/fdcio.h>
65 #include <sys/filio.h>
66 #include <sys/kernel.h>
67 #include <sys/kthread.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/module.h>
71 #include <sys/mutex.h>
72 #include <sys/proc.h>
73 #include <sys/rman.h>
74 #include <sys/sysctl.h>
75 #include <sys/systm.h>
76
77 #include <geom/geom.h>
78
79 #include <machine/bus.h>
80 #include <machine/clock.h>
81 #include <machine/stdarg.h>
82
83 #include <isa/isavar.h>
84 #include <isa/isareg.h>
85 #include <dev/fdc/fdcvar.h>
86 #include <isa/rtc.h>
87
88 #include <dev/ic/nec765.h>
89
90 /*
91 * Runtime configuration hints/flags
92 */
93
94 /* configuration flags for fd */
95 #define FD_TYPEMASK 0x0f /* drive type, matches enum
96 * fd_drivetype; on i386 machines, if
97 * given as 0, use RTC type for fd0
98 * and fd1 */
99 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
100 * assume it is there */
101
102 /*
103 * Things that could conceiveably considered parameters or tweakables
104 */
105
106 /*
107 * Maximal number of bytes in a cylinder.
108 * This is used for ISADMA bouncebuffer allocation and sets the max
109 * xfersize we support.
110 *
111 * 2.88M format has 2 x 36 x 512, allow for hacked up density.
112 */
113 #define MAX_BYTES_PER_CYL (2 * 40 * 512)
114
115 /*
116 * Timeout value for the PIO loops to wait until the FDC main status
117 * register matches our expectations (request for master, direction
118 * bit). This is supposed to be a number of microseconds, although
119 * timing might actually not be very accurate.
120 *
121 * Timeouts of 100 msec are believed to be required for some broken
122 * (old) hardware.
123 */
124 #define FDSTS_TIMEOUT 100000
125
126 /*
127 * After this many errors, stop whining. Close will reset this count.
128 */
129 #define FDC_ERRMAX 100
130
131 /*
132 * AutoDensity search lists for each drive type.
133 */
134
135 static struct fd_type fd_searchlist_360k[] = {
136 { FDF_5_360 },
137 { 0 }
138 };
139
140 static struct fd_type fd_searchlist_12m[] = {
141 { FDF_5_1200 | FL_AUTO },
142 { FDF_5_360 | FL_2STEP | FL_AUTO},
143 { 0 }
144 };
145
146 static struct fd_type fd_searchlist_720k[] = {
147 { FDF_3_720 },
148 { 0 }
149 };
150
151 static struct fd_type fd_searchlist_144m[] = {
152 { FDF_3_1440 | FL_AUTO},
153 { FDF_3_720 | FL_AUTO},
154 { 0 }
155 };
156
157 static struct fd_type fd_searchlist_288m[] = {
158 { FDF_3_1440 | FL_AUTO },
159 #if 0
160 { FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
161 #endif
162 { FDF_3_720 | FL_AUTO},
163 { 0 }
164 };
165
166 /*
167 * Order must match enum fd_drivetype in <sys/fdcio.h>.
168 */
169 static struct fd_type *fd_native_types[] = {
170 NULL, /* FDT_NONE */
171 fd_searchlist_360k, /* FDT_360K */
172 fd_searchlist_12m, /* FDT_12M */
173 fd_searchlist_720k, /* FDT_720K */
174 fd_searchlist_144m, /* FDT_144M */
175 fd_searchlist_288m, /* FDT_288M_1 (mapped to FDT_288M) */
176 fd_searchlist_288m, /* FDT_288M */
177 };
178
179 /*
180 * Internals start here
181 */
182
183 /* registers */
184 #define FDOUT 2 /* Digital Output Register (W) */
185 #define FDO_FDSEL 0x03 /* floppy device select */
186 #define FDO_FRST 0x04 /* floppy controller reset */
187 #define FDO_FDMAEN 0x08 /* enable floppy DMA and Interrupt */
188 #define FDO_MOEN0 0x10 /* motor enable drive 0 */
189 #define FDO_MOEN1 0x20 /* motor enable drive 1 */
190 #define FDO_MOEN2 0x40 /* motor enable drive 2 */
191 #define FDO_MOEN3 0x80 /* motor enable drive 3 */
192
193 #define FDSTS 4 /* NEC 765 Main Status Register (R) */
194 #define FDDATA 5 /* NEC 765 Data Register (R/W) */
195 #define FDCTL 7 /* Control Register (W) */
196
197 /*
198 * The YE-DATA PC Card floppies use PIO to read in the data rather
199 * than DMA due to the wild variability of DMA for the PC Card
200 * devices. DMA was deleted from the PC Card specification in version
201 * 7.2 of the standard, but that post-dates the YE-DATA devices by many
202 * years.
203 *
204 * In addition, if we cannot setup the DMA resources for the ISA
205 * attachment, we'll use this same offset for data transfer. However,
206 * that almost certainly won't work.
207 *
208 * For this mode, offset 0 and 1 must be used to setup the transfer
209 * for this floppy. This is OK for PC Card YE Data devices, but for
210 * ISA this is likely wrong. These registers are only available on
211 * those systems that map them to the floppy drive. Newer systems do
212 * not do this, and we should likely prohibit access to them (or
213 * disallow NODMA to be set).
214 */
215 #define FDBCDR 0 /* And 1 */
216 #define FD_YE_DATAPORT 6 /* Drive Data port */
217
218 #define FDI_DCHG 0x80 /* diskette has been changed */
219 /* requires drive and motor being selected */
220 /* is cleared by any step pulse to drive */
221
222 /*
223 * We have three private BIO commands.
224 */
225 #define BIO_PROBE BIO_CMD0
226 #define BIO_RDID BIO_CMD1
227 #define BIO_FMT BIO_CMD2
228
229 /*
230 * Per drive structure (softc).
231 */
232 struct fd_data {
233 u_char *fd_ioptr; /* IO pointer */
234 u_int fd_iosize; /* Size of IO chunks */
235 u_int fd_iocount; /* Outstanding requests */
236 struct fdc_data *fdc; /* pointer to controller structure */
237 int fdsu; /* this units number on this controller */
238 enum fd_drivetype type; /* drive type */
239 struct fd_type *ft; /* pointer to current type descriptor */
240 struct fd_type fts; /* type descriptors */
241 int sectorsize;
242 int flags;
243 #define FD_WP (1<<0) /* Write protected */
244 #define FD_MOTOR (1<<1) /* motor should be on */
245 #define FD_MOTORWAIT (1<<2) /* motor should be on */
246 #define FD_EMPTY (1<<3) /* no media */
247 #define FD_NEWDISK (1<<4) /* media changed */
248 #define FD_ISADMA (1<<5) /* isa dma started */
249 int track; /* where we think the head is */
250 #define FD_NO_TRACK -2
251 int options; /* FDOPT_* */
252 struct callout toffhandle;
253 struct callout tohandle;
254 struct g_geom *fd_geom;
255 struct g_provider *fd_provider;
256 device_t dev;
257 struct bio_queue_head fd_bq;
258 };
259
260 #define FD_NOT_VALID -2
261
262 static driver_intr_t fdc_intr;
263 static void fdc_reset(struct fdc_data *);
264
265 SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
266
267 static int fifo_threshold = 8;
268 SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
269 "FIFO threshold setting");
270
271 static int debugflags = 0;
272 SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
273 "Debug flags");
274
275 static int retries = 10;
276 SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
277 "Number of retries to attempt");
278
279 static int spec1 = 0xaf;
280 SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
281 "Specification byte one (step-rate + head unload)");
282
283 static int spec2 = 0x10;
284 SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
285 "Specification byte two (head load time + no-dma)");
286
287 static int settle;
288 SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
289 "Head settling time in sec/hz");
290
291 static void
292 fdprinttype(struct fd_type *ft)
293 {
294
295 printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
296 ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
297 ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
298 ft->offset_side2, ft->flags);
299 }
300
301 static void
302 fdsettype(struct fd_data *fd, struct fd_type *ft)
303 {
304 fd->ft = ft;
305 ft->size = ft->sectrac * ft->heads * ft->tracks;
306 fd->sectorsize = 128 << fd->ft->secsize;
307 }
308
309 /*
310 * Bus space handling (access to low-level IO).
311 */
312 __inline static void
313 fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
314 {
315
316 bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
317 }
318
319 __inline static uint8_t
320 fdregrd(struct fdc_data *fdc, int reg)
321 {
322
323 return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
324 }
325
326 static void
327 fdctl_wr(struct fdc_data *fdc, u_int8_t v)
328 {
329
330 fdregwr(fdc, FDCTL, v);
331 }
332
333 static void
334 fdout_wr(struct fdc_data *fdc, u_int8_t v)
335 {
336
337 fdregwr(fdc, FDOUT, v);
338 }
339
340 static u_int8_t
341 fdsts_rd(struct fdc_data *fdc)
342 {
343
344 return fdregrd(fdc, FDSTS);
345 }
346
347 static void
348 fddata_wr(struct fdc_data *fdc, u_int8_t v)
349 {
350
351 fdregwr(fdc, FDDATA, v);
352 }
353
354 static u_int8_t
355 fddata_rd(struct fdc_data *fdc)
356 {
357
358 return fdregrd(fdc, FDDATA);
359 }
360
361 static u_int8_t
362 fdin_rd(struct fdc_data *fdc)
363 {
364
365 return fdregrd(fdc, FDCTL);
366 }
367
368 /*
369 * Magic pseudo-DMA initialization for YE FDC. Sets count and
370 * direction.
371 */
372 static void
373 fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
374 {
375 fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
376 fdregwr(fdc, FDBCDR + 1,
377 (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
378 }
379
380 static int
381 fdc_err(struct fdc_data *fdc, const char *s)
382 {
383 fdc->fdc_errs++;
384 if (s) {
385 if (fdc->fdc_errs < FDC_ERRMAX)
386 device_printf(fdc->fdc_dev, "%s", s);
387 else if (fdc->fdc_errs == FDC_ERRMAX)
388 device_printf(fdc->fdc_dev, "too many errors, not "
389 "logging any more\n");
390 }
391
392 return (1);
393 }
394
395 /*
396 * FDC IO functions, take care of the main status register, timeout
397 * in case the desired status bits are never set.
398 *
399 * These PIO loops initially start out with short delays between
400 * each iteration in the expectation that the required condition
401 * is usually met quickly, so it can be handled immediately.
402 */
403 static int
404 fdc_in(struct fdc_data *fdc, int *ptr)
405 {
406 int i, j, step;
407
408 step = 1;
409 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
410 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
411 if (i == (NE7_DIO|NE7_RQM)) {
412 i = fddata_rd(fdc);
413 if (ptr)
414 *ptr = i;
415 return (0);
416 }
417 if (i == NE7_RQM)
418 return (fdc_err(fdc, "ready for output in input\n"));
419 step += step;
420 DELAY(step);
421 }
422 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
423 }
424
425 static int
426 fdc_out(struct fdc_data *fdc, int x)
427 {
428 int i, j, step;
429
430 step = 1;
431 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
432 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
433 if (i == NE7_RQM) {
434 fddata_wr(fdc, x);
435 return (0);
436 }
437 if (i == (NE7_DIO|NE7_RQM))
438 return (fdc_err(fdc, "ready for input in output\n"));
439 step += step;
440 DELAY(step);
441 }
442 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
443 }
444
445 /*
446 * fdc_cmd: Send a command to the chip.
447 * Takes a varargs with this structure:
448 * # of output bytes
449 * output bytes as int [...]
450 * # of input bytes
451 * input bytes as int* [...]
452 */
453 static int
454 fdc_cmd(struct fdc_data *fdc, int n_out, ...)
455 {
456 u_char cmd = 0;
457 int n_in;
458 int n, i;
459 va_list ap;
460
461 va_start(ap, n_out);
462 for (n = 0; n < n_out; n++) {
463 i = va_arg(ap, int);
464 if (n == 0)
465 cmd = i;
466 if (fdc_out(fdc, i) < 0) {
467 char msg[50];
468 snprintf(msg, sizeof(msg),
469 "cmd %x failed at out byte %d of %d\n",
470 cmd, n + 1, n_out);
471 fdc->flags |= FDC_NEEDS_RESET;
472 va_end(ap);
473 return fdc_err(fdc, msg);
474 }
475 }
476 n_in = va_arg(ap, int);
477 for (n = 0; n < n_in; n++) {
478 int *ptr = va_arg(ap, int *);
479 if (fdc_in(fdc, ptr) < 0) {
480 char msg[50];
481 snprintf(msg, sizeof(msg),
482 "cmd %02x failed at in byte %d of %d\n",
483 cmd, n + 1, n_in);
484 fdc->flags |= FDC_NEEDS_RESET;
485 va_end(ap);
486 return fdc_err(fdc, msg);
487 }
488 }
489 va_end(ap);
490 return (0);
491 }
492
493 static void
494 fdc_reset(struct fdc_data *fdc)
495 {
496 int i, r[10];
497
498 /* Try a reset, keep motor on */
499 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
500 DELAY(100);
501 /* enable FDC, but defer interrupts a moment */
502 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
503 DELAY(100);
504 fdout_wr(fdc, fdc->fdout);
505
506 /* XXX after a reset, silently believe the FDC will accept commands */
507 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
508 device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
509
510 if (fdc->fdct == FDC_ENHANCED) {
511 if (fdc_cmd(fdc, 4,
512 I8207X_CONFIGURE,
513 0,
514 0x40 | /* Enable Implied Seek */
515 0x10 | /* Polling disabled */
516 (fifo_threshold - 1), /* Fifo threshold */
517 0x00, /* Precomp track */
518 0))
519 device_printf(fdc->fdc_dev,
520 " CONFIGURE failed in reset\n");
521 if (debugflags & 1) {
522 if (fdc_cmd(fdc, 1,
523 0x0e, /* DUMPREG */
524 10, &r[0], &r[1], &r[2], &r[3], &r[4],
525 &r[5], &r[6], &r[7], &r[8], &r[9]))
526 device_printf(fdc->fdc_dev,
527 " DUMPREG failed in reset\n");
528 for (i = 0; i < 10; i++)
529 printf(" %02x", r[i]);
530 printf("\n");
531 }
532 }
533 }
534
535 static int
536 fdc_sense_drive(struct fdc_data *fdc, int *st3p)
537 {
538 int st3;
539
540 if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
541 return (fdc_err(fdc, "Sense Drive Status failed\n"));
542 if (st3p)
543 *st3p = st3;
544 return (0);
545 }
546
547 static int
548 fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
549 {
550 int cyl, st0, ret;
551
552 ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
553 if (ret) {
554 (void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
555 return (ret);
556 }
557
558 if (st0p)
559 *st0p = st0;
560
561 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
562 /*
563 * There doesn't seem to have been an interrupt.
564 */
565 return (FD_NOT_VALID);
566 }
567
568 if (fdc_in(fdc, &cyl) < 0)
569 return fdc_err(fdc, "can't get cyl num\n");
570
571 if (cylp)
572 *cylp = cyl;
573
574 return (0);
575 }
576
577 static int
578 fdc_read_status(struct fdc_data *fdc)
579 {
580 int i, ret, status;
581
582 for (i = ret = 0; i < 7; i++) {
583 ret = fdc_in(fdc, &status);
584 fdc->status[i] = status;
585 if (ret != 0)
586 break;
587 }
588
589 if (ret == 0)
590 fdc->flags |= FDC_STAT_VALID;
591 else
592 fdc->flags &= ~FDC_STAT_VALID;
593
594 return ret;
595 }
596
597 /*
598 * Select this drive
599 */
600 static void
601 fd_select(struct fd_data *fd)
602 {
603 struct fdc_data *fdc;
604
605 /* XXX: lock controller */
606 fdc = fd->fdc;
607 fdc->fdout &= ~FDO_FDSEL;
608 fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
609 fdout_wr(fdc, fdc->fdout);
610 }
611
612 static void
613 fd_turnon(void *arg)
614 {
615 struct fd_data *fd;
616 struct bio *bp;
617 int once;
618
619 fd = arg;
620 mtx_lock(&fd->fdc->fdc_mtx);
621 fd->flags &= ~FD_MOTORWAIT;
622 fd->flags |= FD_MOTOR;
623 once = 0;
624 for (;;) {
625 bp = bioq_takefirst(&fd->fd_bq);
626 if (bp == NULL)
627 break;
628 bioq_disksort(&fd->fdc->head, bp);
629 once = 1;
630 }
631 mtx_unlock(&fd->fdc->fdc_mtx);
632 if (once)
633 wakeup(&fd->fdc->head);
634 }
635
636 static void
637 fd_motor(struct fd_data *fd, int turnon)
638 {
639 struct fdc_data *fdc;
640
641 fdc = fd->fdc;
642 /*
643 mtx_assert(&fdc->fdc_mtx, MA_OWNED);
644 */
645 if (turnon) {
646 fd->flags |= FD_MOTORWAIT;
647 fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
648 callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
649 } else {
650 callout_drain(&fd->toffhandle);
651 fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
652 fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
653 }
654 fdout_wr(fdc, fdc->fdout);
655 }
656
657 static void
658 fd_turnoff(void *xfd)
659 {
660 struct fd_data *fd = xfd;
661
662 mtx_lock(&fd->fdc->fdc_mtx);
663 fd_motor(fd, 0);
664 mtx_unlock(&fd->fdc->fdc_mtx);
665 }
666
667 /*
668 * fdc_intr - wake up the worker thread.
669 */
670
671 static void
672 fdc_intr(void *arg)
673 {
674
675 wakeup(arg);
676 }
677
678 /*
679 * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
680 */
681 static void
682 fdc_pio(struct fdc_data *fdc)
683 {
684 u_char *cptr;
685 struct bio *bp;
686 u_int count;
687
688 bp = fdc->bp;
689 cptr = fdc->fd->fd_ioptr;
690 count = fdc->fd->fd_iosize;
691
692 if (bp->bio_cmd == BIO_READ) {
693 fdbcdr_wr(fdc, 0, count);
694 bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
695 fdc->ioff[FD_YE_DATAPORT], cptr, count);
696 } else {
697 bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
698 fdc->ioff[FD_YE_DATAPORT], cptr, count);
699 fdbcdr_wr(fdc, 0, count); /* needed? */
700 }
701 }
702
703 static int
704 fdc_biodone(struct fdc_data *fdc, int error)
705 {
706 struct fd_data *fd;
707 struct bio *bp;
708
709 fd = fdc->fd;
710 bp = fdc->bp;
711
712 mtx_lock(&fdc->fdc_mtx);
713 if (--fd->fd_iocount == 0)
714 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
715 fdc->bp = NULL;
716 fdc->fd = NULL;
717 mtx_unlock(&fdc->fdc_mtx);
718 if (bp->bio_to != NULL) {
719 if ((debugflags & 2) && fd->fdc->retry > 0)
720 printf("retries: %d\n", fd->fdc->retry);
721 g_io_deliver(bp, error);
722 return (0);
723 }
724 bp->bio_error = error;
725 bp->bio_flags |= BIO_DONE;
726 wakeup(bp);
727 return (0);
728 }
729
730 static int retry_line;
731
732 static int
733 fdc_worker(struct fdc_data *fdc)
734 {
735 struct fd_data *fd;
736 struct bio *bp;
737 int i, nsect;
738 int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
739 int head;
740 static int need_recal;
741 struct fdc_readid *idp;
742 struct fd_formb *finfo;
743
744 /* Have we exhausted our retries ? */
745 bp = fdc->bp;
746 fd = fdc->fd;
747 if (bp != NULL &&
748 (fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
749 if ((debugflags & 4))
750 printf("Too many retries (EIO)\n");
751 return (fdc_biodone(fdc, EIO));
752 }
753
754 /* Disable ISADMA if we bailed while it was active */
755 if (fd != NULL && (fd->flags & FD_ISADMA)) {
756 mtx_lock(&Giant);
757 isa_dmadone(
758 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
759 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
760 mtx_unlock(&Giant);
761 mtx_lock(&fdc->fdc_mtx);
762 fd->flags &= ~FD_ISADMA;
763 mtx_unlock(&fdc->fdc_mtx);
764 }
765
766 /* Unwedge the controller ? */
767 if (fdc->flags & FDC_NEEDS_RESET) {
768 fdc->flags &= ~FDC_NEEDS_RESET;
769 fdc_reset(fdc);
770 msleep(fdc, NULL, PRIBIO, "fdcrst", hz);
771 /* Discard results */
772 for (i = 0; i < 4; i++)
773 fdc_sense_int(fdc, &st0, &cyl);
774 /* All drives must recal */
775 need_recal = 0xf;
776 }
777
778 /* Pick up a request, if need be wait for it */
779 if (fdc->bp == NULL) {
780 mtx_lock(&fdc->fdc_mtx);
781 do {
782 fdc->bp = bioq_takefirst(&fdc->head);
783 if (fdc->bp == NULL)
784 msleep(&fdc->head, &fdc->fdc_mtx,
785 PRIBIO, "-", hz);
786 } while (fdc->bp == NULL &&
787 (fdc->flags & FDC_KTHREAD_EXIT) == 0);
788 mtx_unlock(&fdc->fdc_mtx);
789
790 if (fdc->bp == NULL)
791 /*
792 * Nothing to do, worker thread has been
793 * requested to stop.
794 */
795 return (0);
796
797 bp = fdc->bp;
798 fd = fdc->fd = bp->bio_driver1;
799 fdc->retry = 0;
800 fd->fd_ioptr = bp->bio_data;
801 if (bp->bio_cmd & BIO_FMT) {
802 i = offsetof(struct fd_formb, fd_formb_cylno(0));
803 fd->fd_ioptr += i;
804 fd->fd_iosize = bp->bio_length - i;
805 }
806 }
807
808 /* Select drive, setup params */
809 fd_select(fd);
810 fdctl_wr(fdc, fd->ft->trans);
811
812 if (bp->bio_cmd & BIO_PROBE) {
813
814 if (!(fdin_rd(fdc) & FDI_DCHG) && !(fd->flags & FD_EMPTY))
815 return (fdc_biodone(fdc, 0));
816
817 /*
818 * Try to find out if we have a disk in the drive
819 *
820 * First recal, then seek to cyl#1, this clears the
821 * old condition on the disk change line so we can
822 * examine it for current status
823 */
824 if (debugflags & 0x40)
825 printf("New disk in probe\n");
826 mtx_lock(&fdc->fdc_mtx);
827 fd->flags |= FD_NEWDISK;
828 mtx_unlock(&fdc->fdc_mtx);
829 retry_line = __LINE__;
830 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
831 return (1);
832 msleep(fdc, NULL, PRIBIO, "fdrecal", hz);
833 retry_line = __LINE__;
834 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
835 return (1); /* XXX */
836 retry_line = __LINE__;
837 if ((st0 & 0xc0) || cyl != 0)
838 return (1);
839
840 /* Seek to track 1 */
841 retry_line = __LINE__;
842 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
843 return (1);
844 msleep(fdc, NULL, PRIBIO, "fdseek", hz);
845 retry_line = __LINE__;
846 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
847 return (1); /* XXX */
848 need_recal |= (1 << fd->fdsu);
849 if (fdin_rd(fdc) & FDI_DCHG) {
850 if (debugflags & 0x40)
851 printf("Empty in probe\n");
852 mtx_lock(&fdc->fdc_mtx);
853 fd->flags |= FD_EMPTY;
854 mtx_unlock(&fdc->fdc_mtx);
855 } else {
856 if (debugflags & 0x40)
857 printf("Got disk in probe\n");
858 mtx_lock(&fdc->fdc_mtx);
859 fd->flags &= ~FD_EMPTY;
860 mtx_unlock(&fdc->fdc_mtx);
861 retry_line = __LINE__;
862 if(fdc_sense_drive(fdc, &st3) != 0)
863 return (1);
864 mtx_lock(&fdc->fdc_mtx);
865 if(st3 & NE7_ST3_WP)
866 fd->flags |= FD_WP;
867 else
868 fd->flags &= ~FD_WP;
869 mtx_unlock(&fdc->fdc_mtx);
870 }
871 return (fdc_biodone(fdc, 0));
872 }
873
874 /*
875 * If we are dead just flush the requests
876 */
877 if (fd->flags & FD_EMPTY)
878 return (fdc_biodone(fdc, ENXIO));
879
880 /* Check if we lost our media */
881 if (fdin_rd(fdc) & FDI_DCHG) {
882 if (debugflags & 0x40)
883 printf("Lost disk\n");
884 mtx_lock(&fdc->fdc_mtx);
885 fd->flags |= FD_EMPTY;
886 fd->flags |= FD_NEWDISK;
887 mtx_unlock(&fdc->fdc_mtx);
888 g_topology_lock();
889 g_orphan_provider(fd->fd_provider, EXDEV);
890 fd->fd_provider->flags |= G_PF_WITHER;
891 fd->fd_provider =
892 g_new_providerf(fd->fd_geom, fd->fd_geom->name);
893 g_error_provider(fd->fd_provider, 0);
894 g_topology_unlock();
895 return (fdc_biodone(fdc, ENXIO));
896 }
897
898 /* Check if the floppy is write-protected */
899 if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
900 retry_line = __LINE__;
901 if(fdc_sense_drive(fdc, &st3) != 0)
902 return (1);
903 if(st3 & NE7_ST3_WP)
904 return (fdc_biodone(fdc, EROFS));
905 }
906
907 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
908 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
909 i = fd->ft->sectrac * fd->ft->heads;
910 cylinder = bp->bio_pblkno / i;
911 descyl = cylinder * steptrac;
912 sec = bp->bio_pblkno % i;
913 nsect = i - sec;
914 head = sec / fd->ft->sectrac;
915 sec = sec % fd->ft->sectrac + 1;
916
917 /* If everything is going swimmingly, use multisector xfer */
918 if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
919 fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
920 nsect = fd->fd_iosize / fd->sectorsize;
921 } else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
922 fd->fd_iosize = fd->sectorsize;
923 nsect = 1;
924 }
925
926 /* Do RECAL if we need to or are going to track zero anyway */
927 if ((need_recal & (1 << fd->fdsu)) ||
928 (cylinder == 0 && fd->track != 0) ||
929 fdc->retry > 2) {
930 retry_line = __LINE__;
931 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
932 return (1);
933 msleep(fdc, NULL, PRIBIO, "fdrecal", hz);
934 retry_line = __LINE__;
935 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
936 return (1); /* XXX */
937 retry_line = __LINE__;
938 if ((st0 & 0xc0) || cyl != 0)
939 return (1);
940 need_recal &= ~(1 << fd->fdsu);
941 fd->track = 0;
942 /* let the heads settle */
943 if (settle)
944 msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle);
945 }
946
947 /*
948 * SEEK to where we want to be
949 *
950 * Enhanced controllers do implied seeks for read&write as long as
951 * we do not need multiple steps per track.
952 */
953 if (cylinder != fd->track && (
954 fdc->fdct != FDC_ENHANCED ||
955 descyl != cylinder ||
956 (bp->bio_cmd & (BIO_RDID|BIO_FMT)))) {
957 retry_line = __LINE__;
958 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
959 return (1);
960 msleep(fdc, NULL, PRIBIO, "fdseek", hz);
961 retry_line = __LINE__;
962 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
963 return (1); /* XXX */
964 retry_line = __LINE__;
965 if ((st0 & 0xc0) || cyl != descyl) {
966 need_recal |= (1 << fd->fdsu);
967 return (1);
968 }
969 /* let the heads settle */
970 if (settle)
971 msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle);
972 }
973 fd->track = cylinder;
974
975 if (debugflags & 8)
976 printf("op %x bn %ju siz %u ptr %p retry %d\n",
977 bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
978 fd->fd_ioptr, fdc->retry);
979
980 /* Setup ISADMA if we need it and have it */
981 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
982 && !(fdc->flags & FDC_NODMA)) {
983 mtx_lock(&Giant);
984 isa_dmastart(
985 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
986 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
987 mtx_unlock(&Giant);
988 mtx_lock(&fdc->fdc_mtx);
989 fd->flags |= FD_ISADMA;
990 mtx_unlock(&fdc->fdc_mtx);
991 }
992
993 /* Do PIO if we have to */
994 if (fdc->flags & FDC_NODMA) {
995 if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
996 fdbcdr_wr(fdc, 1, fd->fd_iosize);
997 if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
998 fdc_pio(fdc);
999 }
1000
1001 switch(bp->bio_cmd) {
1002 case BIO_FMT:
1003 /* formatting */
1004 finfo = (struct fd_formb *)bp->bio_data;
1005 retry_line = __LINE__;
1006 if (fdc_cmd(fdc, 6,
1007 NE7CMD_FORMAT | mfm,
1008 head << 2 | fd->fdsu,
1009 finfo->fd_formb_secshift,
1010 finfo->fd_formb_nsecs,
1011 finfo->fd_formb_gaplen,
1012 finfo->fd_formb_fillbyte, 0))
1013 return (1);
1014 break;
1015 case BIO_RDID:
1016 retry_line = __LINE__;
1017 if (fdc_cmd(fdc, 2,
1018 NE7CMD_READID | mfm,
1019 head << 2 | fd->fdsu, 0))
1020 return (1);
1021 break;
1022 case BIO_READ:
1023 retry_line = __LINE__;
1024 if (fdc_cmd(fdc, 9,
1025 NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
1026 head << 2 | fd->fdsu, /* head & unit */
1027 fd->track, /* track */
1028 head, /* head */
1029 sec, /* sector + 1 */
1030 fd->ft->secsize, /* sector size */
1031 fd->ft->sectrac, /* sectors/track */
1032 fd->ft->gap, /* gap size */
1033 fd->ft->datalen, /* data length */
1034 0))
1035 return (1);
1036 break;
1037 case BIO_WRITE:
1038 retry_line = __LINE__;
1039 if (fdc_cmd(fdc, 9,
1040 NE7CMD_WRITE | mfm | NE7CMD_MT,
1041 head << 2 | fd->fdsu, /* head & unit */
1042 fd->track, /* track */
1043 head, /* head */
1044 sec, /* sector + 1 */
1045 fd->ft->secsize, /* sector size */
1046 fd->ft->sectrac, /* sectors/track */
1047 fd->ft->gap, /* gap size */
1048 fd->ft->datalen, /* data length */
1049 0))
1050 return (1);
1051 break;
1052 default:
1053 KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
1054 }
1055
1056 /* Wait for interrupt */
1057 i = msleep(fdc, NULL, PRIBIO, "fddata", hz);
1058
1059 /* PIO if the read looks good */
1060 if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
1061 fdc_pio(fdc);
1062
1063 /* Finish DMA */
1064 if (fd->flags & FD_ISADMA) {
1065 mtx_lock(&Giant);
1066 isa_dmadone(
1067 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1068 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1069 mtx_unlock(&Giant);
1070 mtx_lock(&fdc->fdc_mtx);
1071 fd->flags &= ~FD_ISADMA;
1072 mtx_unlock(&fdc->fdc_mtx);
1073 }
1074
1075 if (i != 0) {
1076 /*
1077 * Timeout.
1078 *
1079 * Due to IBM's brain-dead design, the FDC has a faked ready
1080 * signal, hardwired to ready == true. Thus, any command
1081 * issued if there's no diskette in the drive will _never_
1082 * complete, and must be aborted by resetting the FDC.
1083 * Many thanks, Big Blue!
1084 */
1085 retry_line = __LINE__;
1086 fdc->flags |= FDC_NEEDS_RESET;
1087 return (1);
1088 }
1089
1090 retry_line = __LINE__;
1091 if (fdc_read_status(fdc))
1092 return (1);
1093
1094 if (debugflags & 0x10)
1095 printf(" -> %x %x %x %x\n",
1096 fdc->status[0], fdc->status[1],
1097 fdc->status[2], fdc->status[3]);
1098
1099 st0 = fdc->status[0] & NE7_ST0_IC;
1100 if (st0 != 0) {
1101 retry_line = __LINE__;
1102 if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
1103 /*
1104 * DMA overrun. Someone hogged the bus and
1105 * didn't release it in time for the next
1106 * FDC transfer.
1107 */
1108 return (1);
1109 }
1110 retry_line = __LINE__;
1111 if(st0 == NE7_ST0_IC_IV) {
1112 fdc->flags |= FDC_NEEDS_RESET;
1113 return (1);
1114 }
1115 retry_line = __LINE__;
1116 if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
1117 need_recal |= (1 << fd->fdsu);
1118 return (1);
1119 }
1120 if (debugflags & 0x20) {
1121 printf("status %02x %02x %02x %02x %02x %02x\n",
1122 fdc->status[0], fdc->status[1], fdc->status[2],
1123 fdc->status[3], fdc->status[4], fdc->status[5]);
1124 }
1125 retry_line = __LINE__;
1126 return (1);
1127 }
1128 /* All OK */
1129 switch(bp->bio_cmd) {
1130 case BIO_RDID:
1131 /* copy out ID field contents */
1132 idp = (struct fdc_readid *)bp->bio_data;
1133 idp->cyl = fdc->status[3];
1134 idp->head = fdc->status[4];
1135 idp->sec = fdc->status[5];
1136 idp->secshift = fdc->status[6];
1137 if (debugflags & 0x40)
1138 printf("c %d h %d s %d z %d\n",
1139 idp->cyl, idp->head, idp->sec, idp->secshift);
1140 break;
1141 case BIO_READ:
1142 case BIO_WRITE:
1143 bp->bio_pblkno += nsect;
1144 bp->bio_resid -= fd->fd_iosize;
1145 bp->bio_completed += fd->fd_iosize;
1146 fd->fd_ioptr += fd->fd_iosize;
1147 /* Since we managed to get something done, reset the retry */
1148 fdc->retry = 0;
1149 if (bp->bio_resid > 0)
1150 return (0);
1151 break;
1152 case BIO_FMT:
1153 break;
1154 }
1155 return (fdc_biodone(fdc, 0));
1156 }
1157
1158 static void
1159 fdc_thread(void *arg)
1160 {
1161 struct fdc_data *fdc;
1162
1163 fdc = arg;
1164 int i;
1165
1166 mtx_lock(&fdc->fdc_mtx);
1167 fdc->flags |= FDC_KTHREAD_ALIVE;
1168 while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
1169 mtx_unlock(&fdc->fdc_mtx);
1170 i = fdc_worker(fdc);
1171 if (i && debugflags & 0x20) {
1172 if (fdc->bp != NULL) {
1173 g_print_bio(fdc->bp);
1174 printf("\n");
1175 }
1176 printf("Retry line %d\n", retry_line);
1177 }
1178 fdc->retry += i;
1179 mtx_lock(&fdc->fdc_mtx);
1180 }
1181 fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1182 wakeup(&fdc->fdc_thread);
1183 mtx_unlock(&fdc->fdc_mtx);
1184
1185 kthread_exit(0);
1186 }
1187
1188 /*
1189 * Enqueue a request.
1190 */
1191 static void
1192 fd_enqueue(struct fd_data *fd, struct bio *bp)
1193 {
1194 struct fdc_data *fdc;
1195 int call;
1196
1197 call = 0;
1198 fdc = fd->fdc;
1199 mtx_lock(&fdc->fdc_mtx);
1200 /* If we go from idle, cancel motor turnoff */
1201 if (fd->fd_iocount++ == 0)
1202 callout_drain(&fd->toffhandle);
1203 if (fd->flags & FD_MOTOR) {
1204 /* The motor is on, send it directly to the controller */
1205 bioq_disksort(&fdc->head, bp);
1206 wakeup(&fdc->head);
1207 } else {
1208 /* Queue it on the drive until the motor has started */
1209 bioq_insert_tail(&fd->fd_bq, bp);
1210 if (!(fd->flags & FD_MOTORWAIT))
1211 fd_motor(fd, 1);
1212 }
1213 mtx_unlock(&fdc->fdc_mtx);
1214 }
1215
1216 static int
1217 fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
1218 {
1219 struct bio *bp;
1220 struct fd_formb *finfo;
1221 struct fdc_readid *idfield;
1222 int error;
1223
1224 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
1225
1226 /*
1227 * Set up a bio request for fdstrategy(). bio_offset is faked
1228 * so that fdstrategy() will seek to the the requested
1229 * cylinder, and use the desired head.
1230 */
1231 bp->bio_cmd = cmd;
1232 if (cmd == BIO_FMT) {
1233 finfo = (struct fd_formb *)data;
1234 bp->bio_pblkno =
1235 (finfo->cyl * fd->ft->heads + finfo->head) *
1236 fd->ft->sectrac;
1237 bp->bio_length = sizeof *finfo;
1238 } else if (cmd == BIO_RDID) {
1239 idfield = (struct fdc_readid *)data;
1240 bp->bio_pblkno =
1241 (idfield->cyl * fd->ft->heads + idfield->head) *
1242 fd->ft->sectrac;
1243 bp->bio_length = sizeof(struct fdc_readid);
1244 } else if (cmd == BIO_PROBE) {
1245 /* nothing */
1246 } else
1247 panic("wrong cmd in fdmisccmd()");
1248 bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
1249 bp->bio_data = data;
1250 bp->bio_driver1 = fd;
1251 bp->bio_flags = 0;
1252
1253 fd_enqueue(fd, bp);
1254
1255 do {
1256 msleep(bp, NULL, PRIBIO, "fdwait", hz);
1257 } while (!(bp->bio_flags & BIO_DONE));
1258 error = bp->bio_error;
1259
1260 free(bp, M_TEMP);
1261 return (error);
1262 }
1263
1264 /*
1265 * Try figuring out the density of the media present in our device.
1266 */
1267 static int
1268 fdautoselect(struct fd_data *fd)
1269 {
1270 struct fd_type *fdtp;
1271 struct fdc_readid id;
1272 int oopts, rv;
1273
1274 if (!(fd->ft->flags & FL_AUTO))
1275 return (0);
1276
1277 fdtp = fd_native_types[fd->type];
1278 fdsettype(fd, fdtp);
1279 if (!(fd->ft->flags & FL_AUTO))
1280 return (0);
1281
1282 /*
1283 * Try reading sector ID fields, first at cylinder 0, head 0,
1284 * then at cylinder 2, head N. We don't probe cylinder 1,
1285 * since for 5.25in DD media in a HD drive, there are no data
1286 * to read (2 step pulses per media cylinder required). For
1287 * two-sided media, the second probe always goes to head 1, so
1288 * we can tell them apart from single-sided media. As a
1289 * side-effect this means that single-sided media should be
1290 * mentioned in the search list after two-sided media of an
1291 * otherwise identical density. Media with a different number
1292 * of sectors per track but otherwise identical parameters
1293 * cannot be distinguished at all.
1294 *
1295 * If we successfully read an ID field on both cylinders where
1296 * the recorded values match our expectation, we are done.
1297 * Otherwise, we try the next density entry from the table.
1298 *
1299 * Stepping to cylinder 2 has the side-effect of clearing the
1300 * unit attention bit.
1301 */
1302 oopts = fd->options;
1303 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1304 for (; fdtp->heads; fdtp++) {
1305 fdsettype(fd, fdtp);
1306
1307 id.cyl = id.head = 0;
1308 rv = fdmisccmd(fd, BIO_RDID, &id);
1309 if (rv != 0)
1310 continue;
1311 if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
1312 continue;
1313 id.cyl = 2;
1314 id.head = fd->ft->heads - 1;
1315 rv = fdmisccmd(fd, BIO_RDID, &id);
1316 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1317 id.secshift != fdtp->secsize)
1318 continue;
1319 if (rv == 0)
1320 break;
1321 }
1322
1323 fd->options = oopts;
1324 if (fdtp->heads == 0) {
1325 if (debugflags & 0x40)
1326 device_printf(fd->dev, "autoselection failed\n");
1327 fdsettype(fd, fd_native_types[fd->type]);
1328 return (0);
1329 } else {
1330 if (debugflags & 0x40) {
1331 device_printf(fd->dev,
1332 "autoselected %d KB medium\n", fd->ft->size / 2);
1333 fdprinttype(fd->ft);
1334 }
1335 return (0);
1336 }
1337 }
1338
1339 /*
1340 * GEOM class implementation
1341 */
1342
1343 static g_access_t fd_access;
1344 static g_start_t fd_start;
1345 static g_ioctl_t fd_ioctl;
1346
1347 struct g_class g_fd_class = {
1348 .name = "FD",
1349 .version = G_VERSION,
1350 .start = fd_start,
1351 .access = fd_access,
1352 .ioctl = fd_ioctl,
1353 };
1354
1355 static int
1356 fd_access(struct g_provider *pp, int r, int w, int e)
1357 {
1358 struct fd_data *fd;
1359 struct fdc_data *fdc;
1360 int ar, aw, ae;
1361
1362 fd = pp->geom->softc;
1363 fdc = fd->fdc;
1364
1365 /*
1366 * If our provider is withering, we can only get negative requests
1367 * and we don't want to even see them
1368 */
1369 if (pp->flags & G_PF_WITHER)
1370 return (0);
1371
1372 ar = r + pp->acr;
1373 aw = w + pp->acw;
1374 ae = e + pp->ace;
1375
1376 if (ar == 0 && aw == 0 && ae == 0) {
1377 device_unbusy(fd->dev);
1378 return (0);
1379 }
1380
1381 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
1382 if (fdmisccmd(fd, BIO_PROBE, NULL))
1383 return (ENXIO);
1384 if (fd->flags & FD_EMPTY)
1385 return (ENXIO);
1386 if (fd->flags & FD_NEWDISK) {
1387 fdautoselect(fd);
1388 mtx_lock(&fdc->fdc_mtx);
1389 fd->flags &= ~FD_NEWDISK;
1390 mtx_unlock(&fdc->fdc_mtx);
1391 }
1392 device_busy(fd->dev);
1393 }
1394
1395 if (w > 0 && (fd->flags & FD_WP))
1396 return (EROFS);
1397
1398 pp->sectorsize = fd->sectorsize;
1399 pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
1400 pp->mediasize = pp->stripesize * fd->ft->tracks;
1401 return (0);
1402 }
1403
1404 static void
1405 fd_start(struct bio *bp)
1406 {
1407 struct fdc_data * fdc;
1408 struct fd_data * fd;
1409
1410 fd = bp->bio_to->geom->softc;
1411 fdc = fd->fdc;
1412 bp->bio_driver1 = fd;
1413 if (bp->bio_cmd & BIO_GETATTR) {
1414 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
1415 return;
1416 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
1417 return;
1418 g_io_deliver(bp, ENOIOCTL);
1419 return;
1420 }
1421 if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
1422 g_io_deliver(bp, EOPNOTSUPP);
1423 return;
1424 }
1425 bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
1426 bp->bio_resid = bp->bio_length;
1427 fd_enqueue(fd, bp);
1428 return;
1429 }
1430
1431 static int
1432 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
1433 {
1434 struct fd_data *fd;
1435 struct fdc_status *fsp;
1436 struct fdc_readid *rid;
1437 int error;
1438
1439 fd = pp->geom->softc;
1440
1441 switch (cmd) {
1442 case FD_GTYPE: /* get drive type */
1443 *(struct fd_type *)data = *fd->ft;
1444 return (0);
1445
1446 case FD_STYPE: /* set drive type */
1447 if (!(fflag & FWRITE))
1448 return (EPERM);
1449 /*
1450 * Allow setting drive type temporarily iff
1451 * currently unset. Used for fdformat so any
1452 * user can set it, and then start formatting.
1453 */
1454 fd->fts = *(struct fd_type *)data;
1455 if (fd->fts.sectrac) {
1456 /* XXX: check for rubbish */
1457 fdsettype(fd, &fd->fts);
1458 } else {
1459 fdsettype(fd, fd_native_types[fd->type]);
1460 }
1461 if (debugflags & 0x40)
1462 fdprinttype(fd->ft);
1463 return (0);
1464
1465 case FD_GOPTS: /* get drive options */
1466 *(int *)data = fd->options;
1467 return (0);
1468
1469 case FD_SOPTS: /* set drive options */
1470 if (!(fflag & FWRITE))
1471 return (EPERM);
1472 fd->options = *(int *)data;
1473 return (0);
1474
1475 case FD_CLRERR:
1476 if (suser(td) != 0)
1477 return (EPERM);
1478 fd->fdc->fdc_errs = 0;
1479 return (0);
1480
1481 case FD_GSTAT:
1482 fsp = (struct fdc_status *)data;
1483 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
1484 return (EINVAL);
1485 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
1486 return (0);
1487
1488 case FD_GDTYPE:
1489 *(enum fd_drivetype *)data = fd->type;
1490 return (0);
1491
1492 case FD_FORM:
1493 if (!(fflag & FWRITE))
1494 return (EPERM);
1495 if (((struct fd_formb *)data)->format_version !=
1496 FD_FORMAT_VERSION)
1497 return (EINVAL); /* wrong version of formatting prog */
1498 error = fdmisccmd(fd, BIO_FMT, data);
1499 mtx_lock(&fd->fdc->fdc_mtx);
1500 fd->flags |= FD_NEWDISK;
1501 mtx_unlock(&fd->fdc->fdc_mtx);
1502 break;
1503
1504 case FD_READID:
1505 rid = (struct fdc_readid *)data;
1506 if (rid->cyl > 85 || rid->head > 1)
1507 return (EINVAL);
1508 error = fdmisccmd(fd, BIO_RDID, data);
1509 break;
1510
1511 case FIONBIO:
1512 case FIOASYNC:
1513 /* For backwards compat with old fd*(8) tools */
1514 error = 0;
1515 break;
1516
1517 default:
1518 if (debugflags & 0x80)
1519 printf("Unknown ioctl %lx\n", cmd);
1520 error = ENOIOCTL;
1521 break;
1522 }
1523 return (error);
1524 };
1525
1526
1527
1528 /*
1529 * Configuration/initialization stuff, per controller.
1530 */
1531
1532 devclass_t fdc_devclass;
1533 static devclass_t fd_devclass;
1534
1535 struct fdc_ivars {
1536 int fdunit;
1537 int fdtype;
1538 };
1539
1540 void
1541 fdc_release_resources(struct fdc_data *fdc)
1542 {
1543 device_t dev;
1544 struct resource *last;
1545 int i;
1546
1547 dev = fdc->fdc_dev;
1548 if (fdc->fdc_intr)
1549 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1550 fdc->fdc_intr = NULL;
1551 if (fdc->res_irq != NULL)
1552 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1553 fdc->res_irq);
1554 fdc->res_irq = NULL;
1555 last = NULL;
1556 for (i = 0; i < FDC_MAXREG; i++) {
1557 if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1558 bus_release_resource(dev, SYS_RES_IOPORT,
1559 fdc->ridio[i], fdc->resio[i]);
1560 last = fdc->resio[i];
1561 fdc->resio[i] = NULL;
1562 }
1563 }
1564 if (fdc->res_drq != NULL)
1565 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1566 fdc->res_drq);
1567 fdc->res_drq = NULL;
1568 }
1569
1570 int
1571 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1572 {
1573 struct fdc_ivars *ivars = device_get_ivars(child);
1574
1575 switch (which) {
1576 case FDC_IVAR_FDUNIT:
1577 *result = ivars->fdunit;
1578 break;
1579 case FDC_IVAR_FDTYPE:
1580 *result = ivars->fdtype;
1581 break;
1582 default:
1583 return (ENOENT);
1584 }
1585 return (0);
1586 }
1587
1588 int
1589 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1590 {
1591 struct fdc_ivars *ivars = device_get_ivars(child);
1592
1593 switch (which) {
1594 case FDC_IVAR_FDUNIT:
1595 ivars->fdunit = value;
1596 break;
1597 case FDC_IVAR_FDTYPE:
1598 ivars->fdtype = value;
1599 break;
1600 default:
1601 return (ENOENT);
1602 }
1603 return (0);
1604 }
1605
1606 int
1607 fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1608 {
1609 int ic_type, part_id;
1610
1611 /*
1612 * A status value of 0xff is very unlikely, but not theoretically
1613 * impossible, but it is far more likely to indicate an empty bus.
1614 */
1615 if (fdsts_rd(fdc) == 0xff)
1616 return (ENXIO);
1617
1618 /*
1619 * Assert a reset to the floppy controller and check that the status
1620 * register goes to zero.
1621 */
1622 fdout_wr(fdc, 0);
1623 fdout_wr(fdc, 0);
1624 if (fdsts_rd(fdc) != 0)
1625 return (ENXIO);
1626
1627 /*
1628 * Clear the reset and see it come ready.
1629 */
1630 fdout_wr(fdc, FDO_FRST);
1631 DELAY(100);
1632 if (fdsts_rd(fdc) != 0x80)
1633 return (ENXIO);
1634
1635 /* Then, see if it can handle a command. */
1636 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
1637 return (ENXIO);
1638
1639 /*
1640 * Try to identify the chip.
1641 *
1642 * The i8272 datasheet documents that unknown commands
1643 * will return ST0 as 0x80. The i8272 is supposedly identical
1644 * to the NEC765.
1645 * The i82077SL datasheet says 0x90 for the VERSION command,
1646 * and several "superio" chips emulate this.
1647 */
1648 if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
1649 return (ENXIO);
1650 if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
1651 return (ENXIO);
1652 if (bootverbose)
1653 device_printf(dev,
1654 "ic_type %02x part_id %02x\n", ic_type, part_id);
1655 switch (ic_type & 0xff) {
1656 case 0x80:
1657 device_set_desc(dev, "NEC 765 or clone");
1658 fdc->fdct = FDC_NE765;
1659 break;
1660 case 0x81:
1661 case 0x90:
1662 device_set_desc(dev,
1663 "Enhanced floppy controller");
1664 fdc->fdct = FDC_ENHANCED;
1665 break;
1666 default:
1667 device_set_desc(dev, "Generic floppy controller");
1668 fdc->fdct = FDC_UNKNOWN;
1669 break;
1670 }
1671 return (0);
1672 }
1673
1674 int
1675 fdc_detach(device_t dev)
1676 {
1677 struct fdc_data *fdc;
1678 int error;
1679
1680 fdc = device_get_softc(dev);
1681
1682 /* have our children detached first */
1683 if ((error = bus_generic_detach(dev)))
1684 return (error);
1685
1686 /* kill worker thread */
1687 fdc->flags |= FDC_KTHREAD_EXIT;
1688 mtx_lock(&fdc->fdc_mtx);
1689 wakeup(&fdc->head);
1690 while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
1691 msleep(&fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
1692 mtx_unlock(&fdc->fdc_mtx);
1693
1694 /* reset controller, turn motor off */
1695 fdout_wr(fdc, 0);
1696
1697 if (!(fdc->flags & FDC_NODMA))
1698 isa_dma_release(fdc->dmachan);
1699 fdc_release_resources(fdc);
1700 mtx_destroy(&fdc->fdc_mtx);
1701 return (0);
1702 }
1703
1704 /*
1705 * Add a child device to the fdc controller. It will then be probed etc.
1706 */
1707 device_t
1708 fdc_add_child(device_t dev, const char *name, int unit)
1709 {
1710 struct fdc_ivars *ivar;
1711 device_t child;
1712
1713 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1714 if (ivar == NULL)
1715 return (NULL);
1716 child = device_add_child(dev, name, unit);
1717 if (child == NULL) {
1718 free(ivar, M_DEVBUF);
1719 return (NULL);
1720 }
1721 device_set_ivars(child, ivar);
1722 ivar->fdunit = unit;
1723 ivar->fdtype = FDT_NONE;
1724 if (resource_disabled(name, unit))
1725 device_disable(child);
1726 return (child);
1727 }
1728
1729 int
1730 fdc_attach(device_t dev)
1731 {
1732 struct fdc_data *fdc;
1733 int error;
1734
1735 fdc = device_get_softc(dev);
1736 fdc->fdc_dev = dev;
1737 error = fdc_initial_reset(dev, fdc);
1738 if (error) {
1739 device_printf(dev, "does not respond\n");
1740 return (error);
1741 }
1742 error = bus_setup_intr(dev, fdc->res_irq,
1743 INTR_TYPE_BIO | INTR_ENTROPY | INTR_MPSAFE |
1744 ((fdc->flags & FDC_NOFAST) ? 0 : INTR_FAST),
1745 fdc_intr, fdc, &fdc->fdc_intr);
1746 if (error) {
1747 device_printf(dev, "cannot setup interrupt\n");
1748 return (error);
1749 }
1750 if (!(fdc->flags & FDC_NODMA)) {
1751 error = isa_dma_acquire(fdc->dmachan);
1752 if (!error) {
1753 error = isa_dma_init(fdc->dmachan,
1754 MAX_BYTES_PER_CYL, M_WAITOK);
1755 if (error)
1756 isa_dma_release(fdc->dmachan);
1757 }
1758 if (error)
1759 return (error);
1760 }
1761 fdc->fdcu = device_get_unit(dev);
1762 fdc->flags |= FDC_NEEDS_RESET;
1763
1764 mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
1765
1766 /* reset controller, turn motor off, clear fdout mirror reg */
1767 fdout_wr(fdc, fdc->fdout = 0);
1768 bioq_init(&fdc->head);
1769
1770 kthread_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
1771 "fdc%d", device_get_unit(dev));
1772
1773 settle = hz / 8;
1774
1775 return (0);
1776 }
1777
1778 int
1779 fdc_hints_probe(device_t dev)
1780 {
1781 const char *name, *dname;
1782 int i, error, dunit;
1783
1784 /*
1785 * Probe and attach any children. We should probably detect
1786 * devices from the BIOS unless overridden.
1787 */
1788 name = device_get_nameunit(dev);
1789 i = 0;
1790 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1791 resource_int_value(dname, dunit, "drive", &dunit);
1792 fdc_add_child(dev, dname, dunit);
1793 }
1794
1795 if ((error = bus_generic_attach(dev)) != 0)
1796 return (error);
1797 return (0);
1798 }
1799
1800 int
1801 fdc_print_child(device_t me, device_t child)
1802 {
1803 int retval = 0, flags;
1804
1805 retval += bus_print_child_header(me, child);
1806 retval += printf(" on %s drive %d", device_get_nameunit(me),
1807 fdc_get_fdunit(child));
1808 if ((flags = device_get_flags(me)) != 0)
1809 retval += printf(" flags %#x", flags);
1810 retval += printf("\n");
1811
1812 return (retval);
1813 }
1814
1815 /*
1816 * Configuration/initialization, per drive.
1817 */
1818 static int
1819 fd_probe(device_t dev)
1820 {
1821 int i, unit;
1822 u_int st0, st3;
1823 struct fd_data *fd;
1824 struct fdc_data *fdc;
1825 int fdsu;
1826 int flags, type;
1827
1828 fdsu = fdc_get_fdunit(dev);
1829 fd = device_get_softc(dev);
1830 fdc = device_get_softc(device_get_parent(dev));
1831 flags = device_get_flags(dev);
1832
1833 fd->dev = dev;
1834 fd->fdc = fdc;
1835 fd->fdsu = fdsu;
1836 unit = device_get_unit(dev);
1837
1838 /* Auto-probe if fdinfo is present, but always allow override. */
1839 type = flags & FD_TYPEMASK;
1840 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1841 fd->type = type;
1842 goto done;
1843 } else {
1844 /* make sure fdautoselect() will be called */
1845 fd->flags = FD_EMPTY;
1846 fd->type = type;
1847 }
1848
1849 #if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
1850 if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
1851 /* Look up what the BIOS thinks we have. */
1852 if (unit == 0)
1853 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1854 else
1855 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1856 if (fd->type == FDT_288M_1)
1857 fd->type = FDT_288M;
1858 }
1859 #endif /* __i386__ || __amd64__ */
1860 /* is there a unit? */
1861 if (fd->type == FDT_NONE)
1862 return (ENXIO);
1863
1864 /*
1865 mtx_lock(&fdc->fdc_mtx);
1866 */
1867 /* select it */
1868 fd_select(fd);
1869 fd_motor(fd, 1);
1870 fdc->fd = fd;
1871 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
1872 DELAY(1000000); /* 1 sec */
1873
1874 if ((flags & FD_NO_PROBE) == 0) {
1875 /* If we're at track 0 first seek inwards. */
1876 if ((fdc_sense_drive(fdc, &st3) == 0) &&
1877 (st3 & NE7_ST3_T0)) {
1878 /* Seek some steps... */
1879 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1880 /* ...wait a moment... */
1881 DELAY(300000);
1882 /* make ctrlr happy: */
1883 fdc_sense_int(fdc, NULL, NULL);
1884 }
1885 }
1886
1887 for (i = 0; i < 2; i++) {
1888 /*
1889 * we must recalibrate twice, just in case the
1890 * heads have been beyond cylinder 76, since
1891 * most FDCs still barf when attempting to
1892 * recalibrate more than 77 steps
1893 */
1894 /* go back to 0: */
1895 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1896 /* a second being enough for full stroke seek*/
1897 DELAY(i == 0 ? 1000000 : 300000);
1898
1899 /* anything responding? */
1900 if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1901 (st0 & NE7_ST0_EC) == 0)
1902 break; /* already probed succesfully */
1903 }
1904 }
1905 }
1906
1907 fd_motor(fd, 0);
1908 fdc->fd = NULL;
1909 /*
1910 mtx_unlock(&fdc->fdc_mtx);
1911 */
1912
1913 if ((flags & FD_NO_PROBE) == 0 &&
1914 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1915 return (ENXIO);
1916
1917 done:
1918
1919 switch (fd->type) {
1920 case FDT_12M:
1921 device_set_desc(dev, "1200-KB 5.25\" drive");
1922 break;
1923 case FDT_144M:
1924 device_set_desc(dev, "1440-KB 3.5\" drive");
1925 break;
1926 case FDT_288M:
1927 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1928 break;
1929 case FDT_360K:
1930 device_set_desc(dev, "360-KB 5.25\" drive");
1931 break;
1932 case FDT_720K:
1933 device_set_desc(dev, "720-KB 3.5\" drive");
1934 break;
1935 default:
1936 return (ENXIO);
1937 }
1938 fd->track = FD_NO_TRACK;
1939 fd->fdc = fdc;
1940 fd->fdsu = fdsu;
1941 fd->options = 0;
1942 callout_init(&fd->toffhandle, 1);
1943 callout_init(&fd->tohandle, 1);
1944
1945 /* initialize densities for subdevices */
1946 fdsettype(fd, fd_native_types[fd->type]);
1947 return (0);
1948 }
1949
1950 /*
1951 * We have to do this in a geom event because GEOM is not running
1952 * when fd_attach() is.
1953 * XXX: move fd_attach after geom like ata/scsi disks
1954 */
1955 static void
1956 fd_attach2(void *arg, int flag)
1957 {
1958 struct fd_data *fd;
1959
1960 fd = arg;
1961
1962 fd->fd_geom = g_new_geomf(&g_fd_class,
1963 "fd%d", device_get_unit(fd->dev));
1964 fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name);
1965 fd->fd_geom->softc = fd;
1966 g_error_provider(fd->fd_provider, 0);
1967 }
1968
1969 static int
1970 fd_attach(device_t dev)
1971 {
1972 struct fd_data *fd;
1973
1974 fd = device_get_softc(dev);
1975 g_post_event(fd_attach2, fd, M_WAITOK, NULL);
1976 fd->flags |= FD_EMPTY;
1977 bioq_init(&fd->fd_bq);
1978
1979 return (0);
1980 }
1981
1982 static int
1983 fd_detach(device_t dev)
1984 {
1985 struct fd_data *fd;
1986
1987 fd = device_get_softc(dev);
1988 g_topology_lock();
1989 g_wither_geom(fd->fd_geom, ENXIO);
1990 g_topology_unlock();
1991 while (device_get_state(dev) == DS_BUSY)
1992 tsleep(fd, PZERO, "fdd", hz/10);
1993 callout_drain(&fd->toffhandle);
1994
1995 return (0);
1996 }
1997
1998 static device_method_t fd_methods[] = {
1999 /* Device interface */
2000 DEVMETHOD(device_probe, fd_probe),
2001 DEVMETHOD(device_attach, fd_attach),
2002 DEVMETHOD(device_detach, fd_detach),
2003 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2004 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
2005 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
2006 { 0, 0 }
2007 };
2008
2009 static driver_t fd_driver = {
2010 "fd",
2011 fd_methods,
2012 sizeof(struct fd_data)
2013 };
2014
2015 static int
2016 fdc_modevent(module_t mod, int type, void *data)
2017 {
2018
2019 g_modevent(NULL, type, &g_fd_class);
2020 return (0);
2021 }
2022
2023 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);
Cache object: f51d532823fbb63de307b5712632c4f1
|