forked from freebsd/freebsd-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdc.c
2107 lines (1857 loc) · 49 KB
/
fdc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2004 Poul-Henning Kamp
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Don Ahn.
*
* Libretto PCMCIA floppy support by David Horwitt ([email protected])
* aided by the Linux floppy driver modifications from David Bateman
* ([email protected]).
*
* Copyright (c) 1993, 1994 by
* [email protected] (John Capo)
* [email protected] (Serge Vakulenko)
* [email protected] (Andrew A. Chernov)
*
* Copyright (c) 1993, 1994, 1995 by
* [email protected] (Joerg Wunsch)
* [email protected] (Peter Dufault)
*
* Copyright (c) 2001 Joerg Wunsch,
* [email protected] (Joerg Wunsch)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/cdefs.h>
#include "opt_fdc.h"
#include <sys/param.h>
#include <sys/bio.h>
#include <sys/bus.h>
#include <sys/devicestat.h>
#include <sys/disk.h>
#include <sys/fcntl.h>
#include <sys/fdcio.h>
#include <sys/filio.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/rman.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <geom/geom.h>
#include <machine/bus.h>
#include <machine/clock.h>
#include <machine/stdarg.h>
#include <isa/isavar.h>
#include <isa/isareg.h>
#include <isa/rtc.h>
#include <dev/fdc/fdcvar.h>
#include <dev/ic/nec765.h>
/*
* Runtime configuration hints/flags
*/
/* configuration flags for fd */
#define FD_TYPEMASK 0x0f /* drive type, matches enum
* fd_drivetype; on i386 machines, if
* given as 0, use RTC type for fd0
* and fd1 */
#define FD_NO_CHLINE 0x10 /* drive does not support changeline
* aka. unit attention */
#define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
* assume it is there */
/*
* Things that could conceiveably considered parameters or tweakables
*/
/*
* Maximal number of bytes in a cylinder.
* This is used for ISADMA bouncebuffer allocation and sets the max
* xfersize we support.
*
* 2.88M format has 2 x 36 x 512, allow for hacked up density.
*/
#define MAX_BYTES_PER_CYL (2 * 40 * 512)
/*
* Timeout value for the PIO loops to wait until the FDC main status
* register matches our expectations (request for master, direction
* bit). This is supposed to be a number of microseconds, although
* timing might actually not be very accurate.
*
* Timeouts of 100 msec are believed to be required for some broken
* (old) hardware.
*/
#define FDSTS_TIMEOUT 100000
/*
* After this many errors, stop whining. Close will reset this count.
*/
#define FDC_ERRMAX 100
/*
* AutoDensity search lists for each drive type.
*/
static struct fd_type fd_searchlist_360k[] = {
{ FDF_5_360 },
{ 0 }
};
static struct fd_type fd_searchlist_12m[] = {
{ FDF_5_1200 | FL_AUTO },
{ FDF_5_400 | FL_AUTO },
{ FDF_5_360 | FL_2STEP | FL_AUTO},
{ 0 }
};
static struct fd_type fd_searchlist_720k[] = {
{ FDF_3_720 },
{ 0 }
};
static struct fd_type fd_searchlist_144m[] = {
{ FDF_3_1440 | FL_AUTO},
{ FDF_3_720 | FL_AUTO},
{ 0 }
};
static struct fd_type fd_searchlist_288m[] = {
{ FDF_3_1440 | FL_AUTO },
#if 0
{ FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
#endif
{ FDF_3_720 | FL_AUTO},
{ 0 }
};
/*
* Order must match enum fd_drivetype in <sys/fdcio.h>.
*/
static struct fd_type *fd_native_types[] = {
NULL, /* FDT_NONE */
fd_searchlist_360k, /* FDT_360K */
fd_searchlist_12m, /* FDT_12M */
fd_searchlist_720k, /* FDT_720K */
fd_searchlist_144m, /* FDT_144M */
fd_searchlist_288m, /* FDT_288M_1 (mapped to FDT_288M) */
fd_searchlist_288m, /* FDT_288M */
};
/*
* Internals start here
*/
/* registers */
#define FDOUT 2 /* Digital Output Register (W) */
#define FDO_FDSEL 0x03 /* floppy device select */
#define FDO_FRST 0x04 /* floppy controller reset */
#define FDO_FDMAEN 0x08 /* enable floppy DMA and Interrupt */
#define FDO_MOEN0 0x10 /* motor enable drive 0 */
#define FDO_MOEN1 0x20 /* motor enable drive 1 */
#define FDO_MOEN2 0x40 /* motor enable drive 2 */
#define FDO_MOEN3 0x80 /* motor enable drive 3 */
#define FDSTS 4 /* NEC 765 Main Status Register (R) */
#define FDDSR 4 /* Data Rate Select Register (W) */
#define FDDATA 5 /* NEC 765 Data Register (R/W) */
#define FDCTL 7 /* Control Register (W) */
/*
* The YE-DATA PC Card floppies use PIO to read in the data rather
* than DMA due to the wild variability of DMA for the PC Card
* devices. DMA was deleted from the PC Card specification in version
* 7.2 of the standard, but that post-dates the YE-DATA devices by many
* years.
*
* In addition, if we cannot setup the DMA resources for the ISA
* attachment, we'll use this same offset for data transfer. However,
* that almost certainly won't work.
*
* For this mode, offset 0 and 1 must be used to setup the transfer
* for this floppy. This is OK for PC Card YE Data devices, but for
* ISA this is likely wrong. These registers are only available on
* those systems that map them to the floppy drive. Newer systems do
* not do this, and we should likely prohibit access to them (or
* disallow NODMA to be set).
*/
#define FDBCDR 0 /* And 1 */
#define FD_YE_DATAPORT 6 /* Drive Data port */
#define FDI_DCHG 0x80 /* diskette has been changed */
/* requires drive and motor being selected */
/* is cleared by any step pulse to drive */
/*
* We have three private BIO commands.
*/
#define BIO_PROBE BIO_CMD0
#define BIO_RDID BIO_CMD1
#define BIO_FMT BIO_CMD2
/*
* Per drive structure (softc).
*/
struct fd_data {
u_char *fd_ioptr; /* IO pointer */
u_int fd_iosize; /* Size of IO chunks */
u_int fd_iocount; /* Outstanding requests */
struct fdc_data *fdc; /* pointer to controller structure */
int fdsu; /* this units number on this controller */
enum fd_drivetype type; /* drive type */
struct fd_type *ft; /* pointer to current type descriptor */
struct fd_type fts; /* type descriptors */
int sectorsize;
int flags;
#define FD_WP (1<<0) /* Write protected */
#define FD_MOTOR (1<<1) /* motor should be on */
#define FD_MOTORWAIT (1<<2) /* motor should be on */
#define FD_EMPTY (1<<3) /* no media */
#define FD_NEWDISK (1<<4) /* media changed */
#define FD_ISADMA (1<<5) /* isa dma started */
int track; /* where we think the head is */
#define FD_NO_TRACK -2
int options; /* FDOPT_* */
struct callout toffhandle;
struct g_geom *fd_geom;
struct g_provider *fd_provider;
device_t dev;
struct bio_queue_head fd_bq;
bool gone;
};
#define FD_NOT_VALID -2
static driver_intr_t fdc_intr;
static driver_filter_t fdc_intr_fast;
static void fdc_reset(struct fdc_data *);
static int fd_probe_disk(struct fd_data *, int *);
static SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"fdc driver");
static int fifo_threshold = 8;
SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
"FIFO threshold setting");
static int debugflags = 0;
SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
"Debug flags");
static int retries = 10;
SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
"Number of retries to attempt");
static int spec1 = NE7_SPEC_1(6, 240);
SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
"Specification byte one (step-rate + head unload)");
static int spec2 = NE7_SPEC_2(16, 0);
SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
"Specification byte two (head load time + no-dma)");
static int settle;
SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
"Head settling time in sec/hz");
static void
fdprinttype(struct fd_type *ft)
{
printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
ft->offset_side2, ft->flags);
}
static void
fdsettype(struct fd_data *fd, struct fd_type *ft)
{
fd->ft = ft;
ft->size = ft->sectrac * ft->heads * ft->tracks;
fd->sectorsize = 128 << fd->ft->secsize;
}
/*
* Bus space handling (access to low-level IO).
*/
static inline void
fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
{
bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
}
static inline uint8_t
fdregrd(struct fdc_data *fdc, int reg)
{
return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
}
static void
fdctl_wr(struct fdc_data *fdc, u_int8_t v)
{
fdregwr(fdc, FDCTL, v);
}
static void
fdout_wr(struct fdc_data *fdc, u_int8_t v)
{
fdregwr(fdc, FDOUT, v);
}
static u_int8_t
fdsts_rd(struct fdc_data *fdc)
{
return fdregrd(fdc, FDSTS);
}
static void
fddsr_wr(struct fdc_data *fdc, u_int8_t v)
{
fdregwr(fdc, FDDSR, v);
}
static void
fddata_wr(struct fdc_data *fdc, u_int8_t v)
{
fdregwr(fdc, FDDATA, v);
}
static u_int8_t
fddata_rd(struct fdc_data *fdc)
{
return fdregrd(fdc, FDDATA);
}
static u_int8_t
fdin_rd(struct fdc_data *fdc)
{
return fdregrd(fdc, FDCTL);
}
/*
* Magic pseudo-DMA initialization for YE FDC. Sets count and
* direction.
*/
static void
fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
{
fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
fdregwr(fdc, FDBCDR + 1,
(iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
}
static int
fdc_err(struct fdc_data *fdc, const char *s)
{
fdc->fdc_errs++;
if (s) {
if (fdc->fdc_errs < FDC_ERRMAX)
device_printf(fdc->fdc_dev, "%s", s);
else if (fdc->fdc_errs == FDC_ERRMAX)
device_printf(fdc->fdc_dev, "too many errors, not "
"logging any more\n");
}
return (1);
}
/*
* FDC IO functions, take care of the main status register, timeout
* in case the desired status bits are never set.
*
* These PIO loops initially start out with short delays between
* each iteration in the expectation that the required condition
* is usually met quickly, so it can be handled immediately.
*/
static int
fdc_in(struct fdc_data *fdc, int *ptr)
{
int i, j, step;
step = 1;
for (j = 0; j < FDSTS_TIMEOUT; j += step) {
i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
if (i == (NE7_DIO|NE7_RQM)) {
i = fddata_rd(fdc);
if (ptr)
*ptr = i;
return (0);
}
if (i == NE7_RQM)
return (fdc_err(fdc, "ready for output in input\n"));
step += step;
DELAY(step);
}
return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
}
static int
fdc_out(struct fdc_data *fdc, int x)
{
int i, j, step;
step = 1;
for (j = 0; j < FDSTS_TIMEOUT; j += step) {
i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
if (i == NE7_RQM) {
fddata_wr(fdc, x);
return (0);
}
if (i == (NE7_DIO|NE7_RQM))
return (fdc_err(fdc, "ready for input in output\n"));
step += step;
DELAY(step);
}
return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
}
/*
* fdc_cmd: Send a command to the chip.
* Takes a varargs with this structure:
* # of output bytes
* output bytes as int [...]
* # of input bytes
* input bytes as int* [...]
*/
static int
fdc_cmd(struct fdc_data *fdc, int n_out, ...)
{
u_char cmd = 0;
int n_in;
int n, i;
va_list ap;
va_start(ap, n_out);
for (n = 0; n < n_out; n++) {
i = va_arg(ap, int);
if (n == 0)
cmd = i;
if (fdc_out(fdc, i) < 0) {
char msg[50];
snprintf(msg, sizeof(msg),
"cmd %x failed at out byte %d of %d\n",
cmd, n + 1, n_out);
fdc->flags |= FDC_NEEDS_RESET;
va_end(ap);
return fdc_err(fdc, msg);
}
}
n_in = va_arg(ap, int);
for (n = 0; n < n_in; n++) {
int *ptr = va_arg(ap, int *);
if (fdc_in(fdc, ptr) != 0) {
char msg[50];
snprintf(msg, sizeof(msg),
"cmd %02x failed at in byte %d of %d\n",
cmd, n + 1, n_in);
fdc->flags |= FDC_NEEDS_RESET;
va_end(ap);
return fdc_err(fdc, msg);
}
}
va_end(ap);
return (0);
}
static void
fdc_reset(struct fdc_data *fdc)
{
int i, r[10];
if (fdc->fdct == FDC_ENHANCED) {
/* Try a software reset, default precomp, and 500 kb/s */
fddsr_wr(fdc, I8207X_DSR_SR);
} else {
/* Try a hardware reset, keep motor on */
fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
DELAY(100);
/* enable FDC, but defer interrupts a moment */
fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
}
DELAY(100);
fdout_wr(fdc, fdc->fdout);
/* XXX after a reset, silently believe the FDC will accept commands */
if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
if (fdc->fdct == FDC_ENHANCED) {
if (fdc_cmd(fdc, 4,
I8207X_CONFIG,
0,
/* 0x40 | */ /* Enable Implied Seek -
* breaks 2step! */
0x10 | /* Polling disabled */
(fifo_threshold - 1), /* Fifo threshold */
0x00, /* Precomp track */
0))
device_printf(fdc->fdc_dev,
" CONFIGURE failed in reset\n");
if (debugflags & 1) {
if (fdc_cmd(fdc, 1,
I8207X_DUMPREG,
10, &r[0], &r[1], &r[2], &r[3], &r[4],
&r[5], &r[6], &r[7], &r[8], &r[9]))
device_printf(fdc->fdc_dev,
" DUMPREG failed in reset\n");
for (i = 0; i < 10; i++)
printf(" %02x", r[i]);
printf("\n");
}
}
}
static int
fdc_sense_drive(struct fdc_data *fdc, int *st3p)
{
int st3;
if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
return (fdc_err(fdc, "Sense Drive Status failed\n"));
if (st3p)
*st3p = st3;
return (0);
}
static int
fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
{
int cyl, st0, ret;
ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
if (ret) {
(void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
return (ret);
}
if (st0p)
*st0p = st0;
if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
/*
* There doesn't seem to have been an interrupt.
*/
return (FD_NOT_VALID);
}
if (fdc_in(fdc, &cyl) != 0)
return fdc_err(fdc, "can't get cyl num\n");
if (cylp)
*cylp = cyl;
return (0);
}
static int
fdc_read_status(struct fdc_data *fdc)
{
int i, ret, status;
for (i = ret = 0; i < 7; i++) {
ret = fdc_in(fdc, &status);
fdc->status[i] = status;
if (ret != 0)
break;
}
if (ret == 0)
fdc->flags |= FDC_STAT_VALID;
else
fdc->flags &= ~FDC_STAT_VALID;
return ret;
}
/*
* Select this drive
*/
static void
fd_select(struct fd_data *fd)
{
struct fdc_data *fdc;
/* XXX: lock controller */
fdc = fd->fdc;
fdc->fdout &= ~FDO_FDSEL;
fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
fdout_wr(fdc, fdc->fdout);
}
static void
fd_turnon(void *arg)
{
struct fd_data *fd;
struct bio *bp;
int once;
fd = arg;
mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
fd->flags &= ~FD_MOTORWAIT;
fd->flags |= FD_MOTOR;
once = 0;
for (;;) {
bp = bioq_takefirst(&fd->fd_bq);
if (bp == NULL)
break;
bioq_disksort(&fd->fdc->head, bp);
once = 1;
}
if (once)
wakeup(&fd->fdc->head);
}
static void
fd_motor(struct fd_data *fd, int turnon)
{
struct fdc_data *fdc;
fdc = fd->fdc;
/*
mtx_assert(&fdc->fdc_mtx, MA_OWNED);
*/
if (turnon) {
fd->flags |= FD_MOTORWAIT;
fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
} else {
callout_stop(&fd->toffhandle);
fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
}
fdout_wr(fdc, fdc->fdout);
}
static void
fd_turnoff(void *xfd)
{
struct fd_data *fd = xfd;
mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
fd_motor(fd, 0);
}
/*
* fdc_intr - wake up the worker thread.
*/
static void
fdc_intr(void *arg)
{
wakeup(arg);
}
static int
fdc_intr_fast(void *arg)
{
wakeup(arg);
return(FILTER_HANDLED);
}
/*
* fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
*/
static void
fdc_pio(struct fdc_data *fdc)
{
u_char *cptr;
struct bio *bp;
u_int count;
bp = fdc->bp;
cptr = fdc->fd->fd_ioptr;
count = fdc->fd->fd_iosize;
if (bp->bio_cmd == BIO_READ) {
fdbcdr_wr(fdc, 0, count);
bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
fdc->ioff[FD_YE_DATAPORT], cptr, count);
} else {
bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
fdc->ioff[FD_YE_DATAPORT], cptr, count);
fdbcdr_wr(fdc, 0, count); /* needed? */
}
}
static int
fdc_biodone(struct fdc_data *fdc, int error)
{
struct fd_data *fd;
struct bio *bp;
fd = fdc->fd;
bp = fdc->bp;
mtx_lock(&fdc->fdc_mtx);
if (--fd->fd_iocount == 0)
callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
fdc->bp = NULL;
fdc->fd = NULL;
mtx_unlock(&fdc->fdc_mtx);
if (bp->bio_to != NULL) {
if ((debugflags & 2) && fd->fdc->retry > 0)
printf("retries: %d\n", fd->fdc->retry);
g_io_deliver(bp, error);
return (0);
}
bp->bio_error = error;
bp->bio_flags |= BIO_DONE;
wakeup(bp);
return (0);
}
static int retry_line;
static int
fdc_worker(struct fdc_data *fdc)
{
struct fd_data *fd;
struct bio *bp;
int i, nsect;
int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
int head;
int override_error;
static int need_recal;
struct fdc_readid *idp;
struct fd_formb *finfo;
override_error = 0;
/* Have we exhausted our retries ? */
bp = fdc->bp;
fd = fdc->fd;
if (bp != NULL &&
(fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
if ((debugflags & 4))
printf("Too many retries (EIO)\n");
if (fdc->flags & FDC_NEEDS_RESET) {
mtx_lock(&fdc->fdc_mtx);
fd->flags |= FD_EMPTY;
mtx_unlock(&fdc->fdc_mtx);
}
return (fdc_biodone(fdc, EIO));
}
/* Disable ISADMA if we bailed while it was active */
if (fd != NULL && (fd->flags & FD_ISADMA)) {
isa_dmadone(
bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
mtx_lock(&fdc->fdc_mtx);
fd->flags &= ~FD_ISADMA;
mtx_unlock(&fdc->fdc_mtx);
}
/* Unwedge the controller ? */
if (fdc->flags & FDC_NEEDS_RESET) {
fdc->flags &= ~FDC_NEEDS_RESET;
fdc_reset(fdc);
if (cold)
DELAY(1000000);
else
tsleep(fdc, PRIBIO, "fdcrst", hz);
/* Discard results */
for (i = 0; i < 4; i++)
fdc_sense_int(fdc, &st0, &cyl);
/* All drives must recal */
need_recal = 0xf;
}
/* Pick up a request, if need be wait for it */
if (fdc->bp == NULL) {
mtx_lock(&fdc->fdc_mtx);
do {
fdc->bp = bioq_takefirst(&fdc->head);
if (fdc->bp == NULL)
msleep(&fdc->head, &fdc->fdc_mtx,
PRIBIO, "-", 0);
} while (fdc->bp == NULL &&
(fdc->flags & FDC_KTHREAD_EXIT) == 0);
mtx_unlock(&fdc->fdc_mtx);
if (fdc->bp == NULL)
/*
* Nothing to do, worker thread has been
* requested to stop.
*/
return (0);
bp = fdc->bp;
fd = fdc->fd = bp->bio_driver1;
fdc->retry = 0;
fd->fd_ioptr = bp->bio_data;
if (bp->bio_cmd == BIO_FMT) {
i = offsetof(struct fd_formb, fd_formb_cylno(0));
fd->fd_ioptr += i;
fd->fd_iosize = bp->bio_length - i;
}
}
/* Select drive, setup params */
fd_select(fd);
if (fdc->fdct == FDC_ENHANCED)
fddsr_wr(fdc, fd->ft->trans);
else
fdctl_wr(fdc, fd->ft->trans);
if (bp->bio_cmd == BIO_PROBE) {
if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
!(fdin_rd(fdc) & FDI_DCHG) &&
!(fd->flags & FD_EMPTY)) ||
fd_probe_disk(fd, &need_recal) == 0)
return (fdc_biodone(fdc, 0));
return (1);
}
/*
* If we are dead just flush the requests
*/
if (fd->flags & FD_EMPTY)
return (fdc_biodone(fdc, ENXIO));
/* Check if we lost our media */
if (fdin_rd(fdc) & FDI_DCHG) {
if (debugflags & 0x40)
printf("Lost disk\n");
mtx_lock(&fdc->fdc_mtx);
fd->flags |= FD_EMPTY;
fd->flags |= FD_NEWDISK;
mtx_unlock(&fdc->fdc_mtx);
g_topology_lock();
g_orphan_provider(fd->fd_provider, ENXIO);
fd->fd_provider->flags |= G_PF_WITHER;
fd->fd_provider =
g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
g_error_provider(fd->fd_provider, 0);
g_topology_unlock();
return (fdc_biodone(fdc, ENXIO));
}
/* Check if the floppy is write-protected */
if (bp->bio_cmd == BIO_FMT || bp->bio_cmd == BIO_WRITE) {
retry_line = __LINE__;
if(fdc_sense_drive(fdc, &st3) != 0)
return (1);
if(st3 & NE7_ST3_WP)
return (fdc_biodone(fdc, EROFS));
}
mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
i = fd->ft->sectrac * fd->ft->heads;
cylinder = bp->bio_pblkno / i;
descyl = cylinder * steptrac;
sec = bp->bio_pblkno % i;
nsect = i - sec;
head = sec / fd->ft->sectrac;
sec = sec % fd->ft->sectrac + 1;
/* If everything is going swimmingly, use multisector xfer */
if (fdc->retry == 0 &&
(bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
nsect = fd->fd_iosize / fd->sectorsize;
} else if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
fd->fd_iosize = fd->sectorsize;
nsect = 1;
}
/* Do RECAL if we need to or are going to track zero anyway */
if ((need_recal & (1 << fd->fdsu)) ||
(cylinder == 0 && fd->track != 0) ||
fdc->retry > 2) {
retry_line = __LINE__;
if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
return (1);
tsleep(fdc, PRIBIO, "fdrecal", hz);
retry_line = __LINE__;
if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
return (1); /* XXX */
retry_line = __LINE__;
if ((st0 & 0xc0) || cyl != 0)
return (1);
need_recal &= ~(1 << fd->fdsu);
fd->track = 0;
/* let the heads settle */
if (settle)
tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
}
/*
* SEEK to where we want to be
*/
if (cylinder != fd->track) {
retry_line = __LINE__;
if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
return (1);
tsleep(fdc, PRIBIO, "fdseek", hz);
retry_line = __LINE__;
if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
return (1); /* XXX */
retry_line = __LINE__;
if ((st0 & 0xc0) || cyl != descyl) {
need_recal |= (1 << fd->fdsu);
return (1);
}
/* let the heads settle */
if (settle)
tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
}
fd->track = cylinder;
if (debugflags & 8)
printf("op %x bn %ju siz %u ptr %p retry %d\n",
bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
fd->fd_ioptr, fdc->retry);
/* Setup ISADMA if we need it and have it */
if ((bp->bio_cmd == BIO_READ ||
bp->bio_cmd == BIO_WRITE ||
bp->bio_cmd == BIO_FMT)
&& !(fdc->flags & FDC_NODMA)) {
isa_dmastart(
bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
mtx_lock(&fdc->fdc_mtx);
fd->flags |= FD_ISADMA;
mtx_unlock(&fdc->fdc_mtx);
}
/* Do PIO if we have to */
if (fdc->flags & FDC_NODMA) {
if (bp->bio_cmd == BIO_READ ||
bp->bio_cmd == BIO_WRITE ||
bp->bio_cmd == BIO_FMT)
fdbcdr_wr(fdc, 1, fd->fd_iosize);
if (bp->bio_cmd == BIO_WRITE ||
bp->bio_cmd == BIO_FMT)
fdc_pio(fdc);
}
switch(bp->bio_cmd) {
case BIO_FMT:
/* formatting */
finfo = (struct fd_formb *)bp->bio_data;
retry_line = __LINE__;
if (fdc_cmd(fdc, 6,
NE7CMD_FORMAT | mfm,
head << 2 | fd->fdsu,
finfo->fd_formb_secshift,
finfo->fd_formb_nsecs,
finfo->fd_formb_gaplen,
finfo->fd_formb_fillbyte, 0))
return (1);
break;
case BIO_RDID:
retry_line = __LINE__;
if (fdc_cmd(fdc, 2,
NE7CMD_READID | mfm,