aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/ftape/lowlevel/fdc-isr.c
blob: ad2bc733ae1ba86a9676b4efb7df342a40000f7a (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
/*
 *      Copyright (C) 1994-1996 Bas Laarhoven,
 *                (C) 1996-1997 Claus-Justus Heine.

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; see the file COPYING.  If not, write to
 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

 *
 * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/fdc-isr.c,v $
 * $Revision: 1.9 $
 * $Date: 1997/10/17 23:01:53 $
 *
 *      This file contains the interrupt service routine and
 *      associated code for the QIC-40/80/3010/3020 floppy-tape driver
 *      "ftape" for Linux.
 */

#include <asm/io.h>
#include <asm/dma.h>

#define volatile		/* */

#include <linux/ftape.h>
#include <linux/qic117.h>
#include "../lowlevel/ftape-tracing.h"
#include "../lowlevel/fdc-isr.h"
#include "../lowlevel/fdc-io.h"
#include "../lowlevel/ftape-ctl.h"
#include "../lowlevel/ftape-rw.h"
#include "../lowlevel/ftape-io.h"
#include "../lowlevel/ftape-calibr.h"
#include "../lowlevel/ftape-bsm.h"

/*      Global vars.
 */
volatile int ft_expected_stray_interrupts;
volatile int ft_interrupt_seen;
volatile int ft_seek_completed;
volatile int ft_hide_interrupt;
/*      Local vars.
 */
typedef enum {
	no_error = 0, id_am_error = 0x01, id_crc_error = 0x02,
	data_am_error = 0x04, data_crc_error = 0x08,
	no_data_error = 0x10, overrun_error = 0x20,
} error_cause;
static int stop_read_ahead;


static void print_error_cause(int cause)
{
	TRACE_FUN(ft_t_any);

	switch (cause) {
	case no_data_error:
		TRACE(ft_t_noise, "no data error");
		break;
	case id_am_error:
		TRACE(ft_t_noise, "id am error");
		break;
	case id_crc_error:
		TRACE(ft_t_noise, "id crc error");
		break;
	case data_am_error:
		TRACE(ft_t_noise, "data am error");
		break;
	case data_crc_error:
		TRACE(ft_t_noise, "data crc error");
		break;
	case overrun_error:
		TRACE(ft_t_noise, "overrun error");
		break;
	default:;
	}
	TRACE_EXIT;
}

static char *fdc_mode_txt(fdc_mode_enum mode)
{
	switch (mode) {
	case fdc_idle:
		return "fdc_idle";
	case fdc_reading_data:
		return "fdc_reading_data";
	case fdc_seeking:
		return "fdc_seeking";
	case fdc_writing_data:
		return "fdc_writing_data";
	case fdc_reading_id:
		return "fdc_reading_id";
	case fdc_recalibrating:
		return "fdc_recalibrating";
	case fdc_formatting:
		return "fdc_formatting";
	case fdc_verifying:
		return "fdc_verifying";
	default:
		return "unknown";
	}
}

static inline error_cause decode_irq_cause(fdc_mode_enum mode, __u8 st[])
{
	error_cause cause = no_error;
	TRACE_FUN(ft_t_any);

	/*  Valid st[], decode cause of interrupt.
	 */
	switch (st[0] & ST0_INT_MASK) {
	case FDC_INT_NORMAL:
		TRACE(ft_t_fdc_dma,"normal completion: %s",fdc_mode_txt(mode));
		break;
	case FDC_INT_ABNORMAL:
		TRACE(ft_t_flow, "abnormal completion %s", fdc_mode_txt(mode));
		TRACE(ft_t_fdc_dma, "ST0: 0x%02x, ST1: 0x%02x, ST2: 0x%02x",
		      st[0], st[1], st[2]);
		TRACE(ft_t_fdc_dma,
		      "C: 0x%02x, H: 0x%02x, R: 0x%02x, N: 0x%02x",
		      st[3], st[4], st[5], st[6]);
		if (st[1] & 0x01) {
			if (st[2] & 0x01) {
				cause = data_am_error;
			} else {
				cause = id_am_error;
			}
		} else if (st[1] & 0x20) {
			if (st[2] & 0x20) {
				cause = data_crc_error;
			} else {
				cause = id_crc_error;
			}
		} else if (st[1] & 0x04) {
			cause = no_data_error;
		} else if (st[1] & 0x10) {
			cause = overrun_error;
		}
		print_error_cause(cause);
		break;
	case FDC_INT_INVALID:
		TRACE(ft_t_flow, "invalid completion %s", fdc_mode_txt(mode));
		break;
	case FDC_INT_READYCH:
		if (st[0] & ST0_SEEK_END) {
			TRACE(ft_t_flow, "drive poll completed");
		} else {
			TRACE(ft_t_flow, "ready change %s",fdc_mode_txt(mode));
		}
		break;
	default:
		break;
	}
	TRACE_EXIT cause;
}

static void update_history(error_cause cause)
{
	switch (cause) {
	case id_am_error:
		ft_history.id_am_errors++;
		break;
	case id_crc_error:
		ft_history.id_crc_errors++;
		break;
	case data_am_error:
		ft_history.data_am_errors++;
		break;
	case data_crc_error:
		ft_history.data_crc_errors++;
		break;
	case overrun_error:
		ft_history.overrun_errors++;
		break;
	case no_data_error:
		ft_history.no_data_errors++;
		break;
	default:;
	}
}

static void skip_bad_sector(buffer_struct * buff)
{
	TRACE_FUN(ft_t_any);

	/*  Mark sector as soft error and skip it
	 */
	if (buff->remaining > 0) {
		++buff->sector_offset;
		++buff->data_offset;
		--buff->remaining;
		buff->ptr += FT_SECTOR_SIZE;
		buff->bad_sector_map >>= 1;
	} else {
		/*  Hey, what is this????????????? C code: if we shift 
		 *  more than 31 bits, we get no shift. That's bad!!!!!!
		 */
		++buff->sector_offset;	/* hack for error maps */
		TRACE(ft_t_warn, "skipping last sector in segment");
	}
	TRACE_EXIT;
}

static void update_error_maps(buffer_struct * buff, unsigned int error_offset)
{
	int hard = 0;
	TRACE_FUN(ft_t_any);

	if (buff->retry < FT_SOFT_RETRIES) {
		buff->soft_error_map |= (1 << error_offset);
	} else {
		buff->hard_error_map |= (1 << error_offset);
		buff->soft_error_map &= ~buff->hard_error_map;
		buff->retry = -1;  /* will be set to 0 in setup_segment */
		hard = 1;
	}
	TRACE(ft_t_noise, "sector %d : %s error\n"
	      KERN_INFO "hard map: 0x%08lx\n"
	      KERN_INFO "soft map: 0x%08lx",
	      FT_SECTOR(error_offset), hard ? "hard" : "soft",
	      (long) buff->hard_error_map, (long) buff->soft_error_map);
	TRACE_EXIT;
}

static void print_progress(buffer_struct *buff, error_cause cause)
{
	TRACE_FUN(ft_t_any);

	switch (cause) {
	case no_error: 
		TRACE(ft_t_flow,"%d Sector(s) transferred", buff->sector_count);
		break;
	case no_data_error:
		TRACE(ft_t_flow, "Sector %d not found",
		      FT_SECTOR(buff->sector_offset));
		break;
	case overrun_error:
		/*  got an overrun error on the first byte, must be a
		 *  hardware problem
		 */
		TRACE(ft_t_bug,
		      "Unexpected error: failing DMA or FDC controller ?");
		break;
	case data_crc_error:
		TRACE(ft_t_flow, "Error in sector %d",
		      FT_SECTOR(buff->sector_offset - 1));
		break;
	case id_crc_error:
	case id_am_error:
	case data_am_error:
		TRACE(ft_t_flow, "Error in sector %d",
		      FT_SECTOR(buff->sector_offset));
		break;
	default:
		TRACE(ft_t_flow, "Unexpected error at sector %d",
		      FT_SECTOR(buff->sector_offset));
		break;
	}
	TRACE_EXIT;
}

/*
 *  Error cause:   Amount xferred:  Action:
 *
 *  id_am_error         0           mark bad and skip
 *  id_crc_error        0           mark bad and skip
 *  data_am_error       0           mark bad and skip
 *  data_crc_error    % 1024        mark bad and skip
 *  no_data_error       0           retry on write
 *                                  mark bad and skip on read
 *  overrun_error  [ 0..all-1 ]     mark bad and skip
 *  no_error           all          continue
 */

/*  the arg `sector' is returned by the fdc and tells us at which sector we
 *  are positioned at (relative to starting sector of segment)
 */
static void determine_verify_progress(buffer_struct *buff,
				      error_cause cause,
				      __u8 sector)
{
	TRACE_FUN(ft_t_any);

	if (cause == no_error && sector == 1) {
		buff->sector_offset = FT_SECTORS_PER_SEGMENT;
		buff->remaining     = 0;
		if (TRACE_LEVEL >= ft_t_flow) {
			print_progress(buff, cause);
		}
	} else {
		buff->sector_offset = sector - buff->sect;
		buff->remaining = FT_SECTORS_PER_SEGMENT - buff->sector_offset;
		TRACE(ft_t_noise, "%ssector offset: 0x%04x", 
		      (cause == no_error) ? "unexpected " : "",
		      buff->sector_offset);
		switch (cause) {
		case overrun_error:
			break;
#if 0
		case no_data_error:
			buff->retry = FT_SOFT_RETRIES;
			if (buff->hard_error_map    &&
			    buff->sector_offset > 1 &&
			    (buff->hard_error_map & 
			     (1 << (buff->sector_offset-2)))) {
				buff->retry --;
			}
			break;
#endif
		default:
			buff->retry = FT_SOFT_RETRIES;
			break;
		}
		if (TRACE_LEVEL >= ft_t_flow) {
			print_progress(buff, cause);
		}
		/*  Sector_offset points to the problem area Now adjust
		 *  sector_offset so it always points one past he failing
		 *  sector. I.e. skip the bad sector.
		 */
		++buff->sector_offset;
		--buff->remaining;
		update_error_maps(buff, buff->sector_offset - 1);
	}
	TRACE_EXIT;
}

static void determine_progress(buffer_struct *buff,
			       error_cause cause,
			       __u8 sector)
{
	unsigned int dma_residue;
	TRACE_FUN(ft_t_any);

	/*  Using less preferred order of disable_dma and
	 *  get_dma_residue because this seems to fail on at least one
	 *  system if reversed!
	 */
	dma_residue = get_dma_residue(fdc.dma);
	disable_dma(fdc.dma);
	if (cause != no_error || dma_residue != 0) {
		TRACE(ft_t_noise, "%sDMA residue: 0x%04x", 
		      (cause == no_error) ? "unexpected " : "",
		      dma_residue);
		/* adjust to actual value: */
		if (dma_residue == 0) {
			/* this happens sometimes with overrun errors.
			 * I don't know whether we could ignore the
			 * overrun error. Play save.
			 */
			buff->sector_count --;
		} else {
			buff->sector_count -= ((dma_residue + 
						(FT_SECTOR_SIZE - 1)) /
					       FT_SECTOR_SIZE);
		}
	}
	/*  Update var's influenced by the DMA operation.
	 */
	if (buff->sector_count > 0) {
		buff->sector_offset   += buff->sector_count;
		buff->data_offset     += buff->sector_count;
		buff->ptr             += (buff->sector_count *
					  FT_SECTOR_SIZE);
		buff->remaining       -= buff->sector_count;
		buff->bad_sector_map >>= buff->sector_count;
	}
	if (TRACE_LEVEL >= ft_t_flow) {
		print_progress(buff, cause);
	}
	if (cause != no_error) {
		if (buff->remaining == 0) {
			TRACE(ft_t_warn, "foo?\n"
			      KERN_INFO "count : %d\n"
			      KERN_INFO "offset: %d\n"
			      KERN_INFO "soft  : %08x\n"
			      KERN_INFO "hard  : %08x",
			      buff->sector_count,
			      buff->sector_offset,
			      buff->soft_error_map,
			      buff->hard_error_map);
		}
		/*  Sector_offset points to the problem area, except if we got
		 *  a data_crc_error. In that case it points one past the
		 *  failing sector.
		 *
		 *  Now adjust sector_offset so it always points one past he
		 *  failing sector. I.e. skip the bad sector.  
		 */
		if (cause != data_crc_error) {
			skip_bad_sector(buff);
		}
		update_error_maps(buff, buff->sector_offset - 1);
	}
	TRACE_EXIT;
}

static int calc_steps(int cmd)
{
	if (ftape_current_cylinder > cmd) {
		return ftape_current_cylinder - cmd;
	} else {
		return ftape_current_cylinder + cmd;
	}
}

static void pause_tape(int retry, int mode)
{
	int result;
	__u8 out[3] = {FDC_SEEK, ft_drive_sel, 0};
	TRACE_FUN(ft_t_any);

	/*  We'll use a raw seek command to get the tape to rewind and
	 *  stop for a retry.
	 */
	++ft_history.rewinds;
	if (qic117_cmds[ftape_current_command].non_intr) {
		TRACE(ft_t_warn, "motion command may be issued too soon");
	}
	if (retry && (mode == fdc_reading_data ||
		      mode == fdc_reading_id   ||
		      mode == fdc_verifying)) {
		ftape_current_command = QIC_MICRO_STEP_PAUSE;
		ftape_might_be_off_track = 1;
	} else {
		ftape_current_command = QIC_PAUSE;
	}
	out[2] = calc_steps(ftape_current_command);
	result = fdc_command(out, 3); /* issue QIC_117 command */
	ftape_current_cylinder = out[ 2];
	if (result < 0) {
		TRACE(ft_t_noise, "qic-pause failed, status = %d", result);
	} else {
		ft_location.known  = 0;
		ft_runner_status   = idle;
		ft_hide_interrupt     = 1;
		ftape_tape_running = 0;
	}
	TRACE_EXIT;
}

static void continue_xfer(buffer_struct *buff,
			  fdc_mode_enum mode, 
			  unsigned int skip)
{
	int write = 0;
 	TRACE_FUN(ft_t_any);

	if (mode == fdc_writing_data || mode == fdc_deleting) {
		write = 1;
	}
	/*  This part can be removed if it never happens
	 */
	if (skip > 0 &&
	    (ft_runner_status != running ||
	     (write && (buff->status != writing)) ||
	     (!write && (buff->status != reading && 
			 buff->status != verifying)))) {
		TRACE(ft_t_err, "unexpected runner/buffer state %d/%d",
		      ft_runner_status, buff->status);
		buff->status = error;
		/* finish this buffer: */
		(void)ftape_next_buffer(ft_queue_head);
		ft_runner_status = aborting;
		fdc_mode         = fdc_idle;
	} else if (buff->remaining > 0 && ftape_calc_next_cluster(buff) > 0) {
		/*  still sectors left in current segment, continue
		 *  with this segment
		 */
		if (fdc_setup_read_write(buff, mode) < 0) {
			/* failed, abort operation
			 */
			buff->bytes = buff->ptr - buff->address;
			buff->status = error;
			/* finish this buffer: */
			(void)ftape_next_buffer(ft_queue_head);
			ft_runner_status = aborting;
			fdc_mode         = fdc_idle;
		}
	} else {
		/* current segment completed
		 */
		unsigned int last_segment = buff->segment_id;
		int eot = ((last_segment + 1) % ft_segments_per_track) == 0;
		unsigned int next = buff->next_segment;	/* 0 means stop ! */

		buff->bytes = buff->ptr - buff->address;
		buff->status = done;
		buff = ftape_next_buffer(ft_queue_head);
		if (eot) {
			/*  finished last segment on current track,
			 *  can't continue
			 */
			ft_runner_status = logical_eot;
			fdc_mode         = fdc_idle;
			TRACE_EXIT;
		}
		if (next <= 0) {
			/*  don't continue with next segment
			 */
			TRACE(ft_t_noise, "no %s allowed, stopping tape",
			      (write) ? "write next" : "read ahead");
			pause_tape(0, mode);
			ft_runner_status = idle;  /*  not quite true until
						   *  next irq 
						   */
			TRACE_EXIT;
		}
		/*  continue with next segment
		 */
		if (buff->status != waiting) {
			TRACE(ft_t_noise, "all input buffers %s, pausing tape",
			      (write) ? "empty" : "full");
			pause_tape(0, mode);
			ft_runner_status = idle;  /*  not quite true until
						   *  next irq 
						   */
			TRACE_EXIT;
		}
		if (write && next != buff->segment_id) {
			TRACE(ft_t_noise, 
			      "segments out of order, aborting write");
			ft_runner_status = do_abort;
			fdc_mode         = fdc_idle;
			TRACE_EXIT;
		}
		ftape_setup_new_segment(buff, next, 0);
		if (stop_read_ahead) {
			buff->next_segment = 0;
			stop_read_ahead = 0;
		}
		if (ftape_calc_next_cluster(buff) == 0 ||
		    fdc_setup_read_write(buff, mode) != 0) {
			TRACE(ft_t_err, "couldn't start %s-ahead",
			      write ? "write" : "read");
			ft_runner_status = do_abort;
			fdc_mode         = fdc_idle;
		} else {
			/* keep on going */
			switch (ft_driver_state) {
			case   reading: buff->status = reading;   break;
			case verifying: buff->status = verifying; break;
			case   writing: buff->status = writing;   break;
			case  deleting: buff->status = deleting;  break;
			default:
				TRACE(ft_t_err, 
		      "BUG: ft_driver_state %d should be one out of "
		      "{reading, writing, verifying, deleting}",
				      ft_driver_state);
				buff->status = write ? writing : reading;
				break;
			}
		}
	}
	TRACE_EXIT;
}

static void retry_sector(buffer_struct *buff, 
			 int mode,
			 unsigned int skip)
{
	TRACE_FUN(ft_t_any);

	TRACE(ft_t_noise, "%s error, will retry",
	      (mode == fdc_writing_data || mode == fdc_deleting) ? "write" : "read");
	pause_tape(1, mode);
	ft_runner_status = aborting;
	buff->status     = error;
	buff->skip       = skip;
	TRACE_EXIT;
}

static unsigned int find_resume_point(buffer_struct *buff)
{
	int i = 0;
	SectorMap mask;
	SectorMap map;
	TRACE_FUN(ft_t_any);

	/*  This function is to be called after all variables have been
	 *  updated to point past the failing sector.
	 *  If there are any soft errors before the failing sector,
	 *  find the first soft error and return the sector offset.
	 *  Otherwise find the last hard error.
	 *  Note: there should always be at least one hard or soft error !
	 */
	if (buff->sector_offset < 1 || buff->sector_offset > 32) {
		TRACE(ft_t_bug, "BUG: sector_offset = %d",
		      buff->sector_offset);
		TRACE_EXIT 0;
	}
	if (buff->sector_offset >= 32) { /* C-limitation on shift ! */
		mask = 0xffffffff;
	} else {
		mask = (1 << buff->sector_offset) - 1;
	}
	map = buff->soft_error_map & mask;
	if (map) {
		while ((map & (1 << i)) == 0) {
			++i;
		}
		TRACE(ft_t_noise, "at sector %d", FT_SECTOR(i));
	} else {
		map = buff->hard_error_map & mask;
		i = buff->sector_offset - 1;
		if (map) {
			while ((map & (1 << i)) == 0) {
				--i;
			}
			TRACE(ft_t_noise, "after sector %d", FT_SECTOR(i));
			++i; /* first sector after last hard error */
		} else {
			TRACE(ft_t_bug, "BUG: no soft or hard errors");
		}
	}
	TRACE_EXIT i;
}

/*  check possible dma residue when formatting, update position record in
 *  buffer struct. This is, of course, modelled after determine_progress(), but
 *  we don't need to set up for retries because the format process cannot be
 *  interrupted (except at the end of the tape track).
 */
static int determine_fmt_progress(buffer_struct *buff, error_cause cause)
{
	unsigned int dma_residue;
	TRACE_FUN(ft_t_any);

	/*  Using less preferred order of disable_dma and
	 *  get_dma_residue because this seems to fail on at least one
	 *  system if reversed!
	 */
	dma_residue = get_dma_residue(fdc.dma);
	disable_dma(fdc.dma);
	if (cause != no_error || dma_residue != 0) {
		TRACE(ft_t_info, "DMA residue = 0x%04x", dma_residue);
		fdc_mode = fdc_idle;
		switch(cause) {
		case no_error:
			ft_runner_status = aborting;
			buff->status = idle;
			break;
		case overrun_error:
			/*  got an overrun error on the first byte, must be a
			 *  hardware problem 
			 */
			TRACE(ft_t_bug, 
			      "Unexpected error: failing DMA controller ?");
			ft_runner_status = do_abort;
			buff->status = error;
			break;
		default:
			TRACE(ft_t_noise, "Unexpected error at segment %d",
			      buff->segment_id);
			ft_runner_status = do_abort;
			buff->status = error;
			break;
		}
		TRACE_EXIT -EIO; /* can only retry entire track in format mode
				  */
	}
	/*  Update var's influenced by the DMA operation.
	 */
	buff->ptr             += FT_SECTORS_PER_SEGMENT * 4;
	buff->bytes           -= FT_SECTORS_PER_SEGMENT * 4;
	buff->remaining       -= FT_SECTORS_PER_SEGMENT;
	buff->segment_id ++; /* done with segment */
	TRACE_EXIT 0;
}

/*
 *  Continue formatting, switch buffers if there is no data left in
 *  current buffer. This is, of course, modelled after
 *  continue_xfer(), but we don't need to set up for retries because
 *  the format process cannot be interrupted (except at the end of the
 *  tape track).
 */
static void continue_formatting(buffer_struct *buff)
{
	TRACE_FUN(ft_t_any);

	if (buff->remaining <= 0) { /*  no space left in dma buffer */
		unsigned int next = buff->next_segment; 

		if (next == 0) { /* end of tape track */
			buff->status     = done;
			ft_runner_status = logical_eot;
			fdc_mode         = fdc_idle;
			TRACE(ft_t_noise, "Done formatting track %d",
			      ft_location.track);
			TRACE_EXIT;
		}
		/*
		 *  switch to next buffer!
		 */
		buff->status   = done;
		buff = ftape_next_buffer(ft_queue_head);

		if (buff->status != waiting  || next != buff->segment_id) {
			goto format_setup_error;
		}
	}
	if (fdc_setup_formatting(buff) < 0) {
		goto format_setup_error;
	}
	buff->status = formatting;
	TRACE(ft_t_fdc_dma, "Formatting segment %d on track %d",
	      buff->segment_id, ft_location.track);
	TRACE_EXIT;
 format_setup_error:
	ft_runner_status = do_abort;
	fdc_mode         = fdc_idle;
	buff->status     = error;
	TRACE(ft_t_err, "Error setting up for segment %d on track %d",
	      buff->segment_id, ft_location.track);
	TRACE_EXIT;

}

/*  this handles writing, read id, reading and formatting
 */
static void handle_fdc_busy(buffer_struct *buff)
{
	static int no_data_error_count;
	int retry = 0;
	error_cause cause;
	__u8 in[7];
	int skip;
	fdc_mode_enum fmode = fdc_mode;
	TRACE_FUN(ft_t_any);

	if (fdc_result(in, 7) < 0) { /* better get it fast ! */
		TRACE(ft_t_err, 
		      "Probably fatal error during FDC Result Phase\n"
		      KERN_INFO
		      "drive may hang until (power on) reset :-(");
		/*  what to do next ????
		 */
		TRACE_EXIT;
	}
	cause = decode_irq_cause(fdc_mode, in);
#ifdef TESTING
	{ int i;
	for (i = 0; i < (int)ft_nr_buffers; ++i)
		TRACE(ft_t_any, "buffer[%d] status: %d, segment_id: %d",
		      i, ft_buffer[i]->status, ft_buffer[i]->segment_id);
	}
#endif
	if (fmode == fdc_reading_data && ft_driver_state == verifying) {
		fmode = fdc_verifying;
	}
	switch (fmode) {
	case fdc_verifying:
		if (ft_runner_status == aborting ||
		    ft_runner_status == do_abort) {
			TRACE(ft_t_noise,"aborting %s",fdc_mode_txt(fdc_mode));
			break;
		}
		if (buff->retry > 0) {
			TRACE(ft_t_flow, "this is retry nr %d", buff->retry);
		}
		switch (cause) {
		case no_error:
			no_data_error_count = 0;
			determine_verify_progress(buff, cause, in[5]);
			if (in[2] & 0x40) {
				/*  This should not happen when verifying
				 */
				TRACE(ft_t_warn,
				      "deleted data in segment %d/%d",
				      buff->segment_id,
				      FT_SECTOR(buff->sector_offset - 1));
				buff->remaining = 0; /* abort transfer */
				buff->hard_error_map = EMPTY_SEGMENT;
				skip = 1;
			} else {
				skip = 0;
			}
			continue_xfer(buff, fdc_mode, skip);
			break;
		case no_data_error:
			no_data_error_count ++;
		case overrun_error:
			retry ++;
		case id_am_error:
		case id_crc_error:
		case data_am_error:
		case data_crc_error:
			determine_verify_progress(buff, cause, in[5]); 
			if (cause == no_data_error) {
				if (no_data_error_count >= 2) {
					TRACE(ft_t_warn,
					      "retrying because of successive "
					      "no data errors");
					no_data_error_count = 0;
				} else {
					retry --;
				}
			} else {
				no_data_error_count = 0;
			}
			if (retry) {
				skip = find_resume_point(buff);
			} else {
				skip = buff->sector_offset;
			}
			if (retry && skip < 32) {
				retry_sector(buff, fdc_mode, skip);
			} else {
				continue_xfer(buff, fdc_mode, skip);
			}
			update_history(cause);
			break;
		default:
			/*  Don't know why this could happen 
			 *  but find out.
			 */
			determine_verify_progress(buff, cause, in[5]);
			retry_sector(buff, fdc_mode, 0);
			TRACE(ft_t_err, "Error: unexpected error");
			break;
		}
		break;
	case fdc_reading_data:
#ifdef TESTING
		/* I'm sorry, but: NOBODY ever used this trace
		 * messages for ages. I guess that Bas was the last person
		 * that ever really used this (thank you, between the lines)
		 */
		if (cause == no_error) {
			TRACE(ft_t_flow,"reading segment %d",buff->segment_id);
		} else {
			TRACE(ft_t_noise, "error reading segment %d",
			      buff->segment_id);
			TRACE(ft_t_noise, "\n"
			      KERN_INFO
			     "IRQ:C: 0x%02x, H: 0x%02x, R: 0x%02x, N: 0x%02x\n"
			      KERN_INFO
			      "BUF:C: 0x%02x, H: 0x%02x, R: 0x%02x",
			      in[3], in[4], in[5], in[6],
			      buff->cyl, buff->head, buff->sect);
		}
#endif
		if (ft_runner_status == aborting ||
		    ft_runner_status == do_abort) {
			TRACE(ft_t_noise,"aborting %s",fdc_mode_txt(fdc_mode));
			break;
		}
		if (buff->bad_sector_map == FAKE_SEGMENT) {
			/* This condition occurs when reading a `fake'
			 * sector that's not accessible. Doesn't
			 * really matter as we would have ignored it
			 * anyway !
			 *
			 * Chance is that we're past the next segment
			 * now, so the next operation may fail and
			 * result in a retry.  
			 */
			buff->remaining = 0;	/* skip failing sector */
			/* buff->ptr       = buff->address; */
			/* fake success: */
			continue_xfer(buff, fdc_mode, 1);
			/*  trace calls are expensive: place them AFTER
			 *  the real stuff has been done.
			 *  
			 */
			TRACE(ft_t_noise, "skipping empty segment %d (read), size? %d",
			      buff->segment_id, buff->ptr - buff->address);
			TRACE_EXIT;
		}
		if (buff->retry > 0) {
			TRACE(ft_t_flow, "this is retry nr %d", buff->retry);
		}
		switch (cause) {
		case no_error:
			determine_progress(buff, cause, in[5]);
			if (in[2] & 0x40) {
				/*  Handle deleted data in header segments.
				 *  Skip segment and force read-ahead.
				 */
				TRACE(ft_t_warn,
				      "deleted data in segment %d/%d",
				      buff->segment_id,
				      FT_SECTOR(buff->sector_offset - 1));
				buff->deleted = 1;
				buff->remaining = 0;/*abort transfer */
				buff->soft_error_map |=
						(-1L << buff->sector_offset);
				if (buff->segment_id == 0) {
					/* stop on next segment */
					stop_read_ahead = 1;
				}
				/* force read-ahead: */
				buff->next_segment = 
					buff->segment_id + 1;
				skip = (FT_SECTORS_PER_SEGMENT - 
					buff->sector_offset);
			} else {
				skip = 0;
			}
			continue_xfer(buff, fdc_mode, skip);
			break;
		case no_data_error:
			/* Tape started too far ahead of or behind the
			 * right sector.  This may also happen in the
			 * middle of a segment !
			 *
			 * Handle no-data as soft error. If next
			 * sector fails too, a retry (with needed
			 * reposition) will follow.
			 */
			retry ++;
		case id_am_error:
		case id_crc_error:
		case data_am_error:
		case data_crc_error:
		case overrun_error:
			retry += (buff->soft_error_map != 0 ||
				  buff->hard_error_map != 0);
			determine_progress(buff, cause, in[5]); 
#if 1 || defined(TESTING)
			if (cause == overrun_error) retry ++;
#endif
			if (retry) {
				skip = find_resume_point(buff);
			} else {
				skip = buff->sector_offset;
			}
			/*  Try to resume with next sector on single
			 *  errors (let ecc correct it), but retry on
			 *  no_data (we'll be past the target when we
			 *  get here so we cannot retry) or on
			 *  multiple errors (reduce chance on ecc
			 *  failure).
			 */
			/*  cH: 23/02/97: if the last sector in the 
			 *  segment was a hard error, then there is 
			 *  no sense in a retry. This occasion seldom
			 *  occurs but ... @:���`@%&�$
			 */
			if (retry && skip < 32) {
				retry_sector(buff, fdc_mode, skip);
			} else {
				continue_xfer(buff, fdc_mode, skip);
			}
			update_history(cause);
			break;
		default:
			/*  Don't know why this could happen 
			 *  but find out.
			 */
			determine_progress(buff, cause, in[5]);
			retry_sector(buff, fdc_mode, 0);
			TRACE(ft_t_err, "Error: unexpected error");
			break;
		}
		break;
	case fdc_reading_id:
		if (cause == no_error) {
			fdc_cyl = in[3];
			fdc_head = in[4];
			fdc_sect = in[5];
			TRACE(ft_t_fdc_dma,
			      "id read: C: 0x%02x, H: 0x%02x, R: 0x%02x",
			      fdc_cyl, fdc_head, fdc_sect);
		} else {	/* no valid information, use invalid sector */
			fdc_cyl = fdc_head = fdc_sect = 0;
			TRACE(ft_t_flow, "Didn't find valid sector Id");
		}
		fdc_mode = fdc_idle;
		break;
	case fdc_deleting:
	case fdc_writing_data:
#ifdef TESTING
		if (cause == no_error) {
			TRACE(ft_t_flow, "writing segment %d", buff->segment_id);
		} else {
			TRACE(ft_t_noise, "error writing segment %d",
			      buff->segment_id);
		}
#endif
		if (ft_runner_status == aborting ||
		    ft_runner_status == do_abort) {
			TRACE(ft_t_flow, "aborting %s",fdc_mode_txt(fdc_mode));
			break;
		}
		if (buff->retry > 0) {
			TRACE(ft_t_flow, "this is retry nr %d", buff->retry);
		}
		if (buff->bad_sector_map == FAKE_SEGMENT) {
			/* This condition occurs when trying to write to a
			 * `fake' sector that's not accessible. Doesn't really
			 * matter as it isn't used anyway ! Might be located
			 * at wrong segment, then we'll fail on the next
			 * segment.
			 */
			TRACE(ft_t_noise, "skipping empty segment (write)");
			buff->remaining = 0;	/* skip failing sector */
			/* fake success: */
			continue_xfer(buff, fdc_mode, 1);
			break;
		}
		switch (cause) {
		case no_error:
			determine_progress(buff, cause, in[5]);
			continue_xfer(buff, fdc_mode, 0);
			break;
		case no_data_error:
		case id_am_error:
		case id_crc_error:
		case data_am_error:
		case overrun_error:
			update_history(cause);
			determine_progress(buff, cause, in[5]);
			skip = find_resume_point(buff);
			retry_sector(buff, fdc_mode, skip);
			break;
		default:
			if (in[1] & 0x02) {
				TRACE(ft_t_err, "media not writable");
			} else {
				TRACE(ft_t_bug, "unforeseen write error");
			}
			fdc_mode = fdc_idle;
			break;
		}
		break; /* fdc_deleting || fdc_writing_data */
	case fdc_formatting:
		/*  The interrupt comes after formatting a segment. We then
		 *  have to set up QUICKLY for the next segment. But
		 *  afterwards, there is plenty of time.
		 */
		switch (cause) {
		case no_error:
			/*  would like to keep most of the formatting stuff
			 *  outside the isr code, but timing is too critical
			 */
			if (determine_fmt_progress(buff, cause) >= 0) {
				continue_formatting(buff);
			}
			break;
		case no_data_error:
		case id_am_error:
		case id_crc_error:
		case data_am_error:
		case overrun_error:
		default:
			determine_fmt_progress(buff, cause);
			update_history(cause);
			if (in[1] & 0x02) {
				TRACE(ft_t_err, "media not writable");
			} else {
				TRACE(ft_t_bug, "unforeseen write error");
			}
			break;
		} /* cause */
		break;
	default:
		TRACE(ft_t_warn, "Warning: unexpected irq during: %s",
		      fdc_mode_txt(fdc_mode));
		fdc_mode = fdc_idle;
		break;
	}
	TRACE_EXIT;
}

/*      FDC interrupt service routine.
 */
void fdc_isr(void)
{
	static int isr_active;
#ifdef TESTING
	unsigned int t0 = ftape_timestamp();
#endif
	TRACE_FUN(ft_t_any);

 	if (isr_active++) {
		--isr_active;
		TRACE(ft_t_bug, "BUG: nested interrupt, not good !");
		*fdc.hook = fdc_isr; /*  hook our handler into the fdc
				      *  code again 
				      */
		TRACE_EXIT;
	}
	sti();
	if (inb_p(fdc.msr) & FDC_BUSY) {	/*  Entering Result Phase */
		ft_hide_interrupt = 0;
		handle_fdc_busy(ftape_get_buffer(ft_queue_head));
		if (ft_runner_status == do_abort) {
			/*      cease operation, remember tape position
			 */
			TRACE(ft_t_flow, "runner aborting");
			ft_runner_status = aborting;
			++ft_expected_stray_interrupts;
		}
	} else { /* !FDC_BUSY */
		/*  clear interrupt, cause should be gotten by issuing
		 *  a Sense Interrupt Status command.
		 */
		if (fdc_mode == fdc_recalibrating || fdc_mode == fdc_seeking) {
			if (ft_hide_interrupt) {
				int st0;
				int pcn;

				if (fdc_sense_interrupt_status(&st0, &pcn) < 0)
					TRACE(ft_t_err,
					      "sense interrupt status failed");
				ftape_current_cylinder = pcn;
				TRACE(ft_t_flow, "handled hidden interrupt");
			}
			ft_seek_completed = 1;
			fdc_mode = fdc_idle;
		} else if (!waitqueue_active(&ftape_wait_intr)) {
			if (ft_expected_stray_interrupts == 0) {
				TRACE(ft_t_warn, "unexpected stray interrupt");
			} else {
				TRACE(ft_t_flow, "expected stray interrupt");
				--ft_expected_stray_interrupts;
			}
		} else {
			if (fdc_mode == fdc_reading_data ||
			    fdc_mode == fdc_verifying    ||
			    fdc_mode == fdc_writing_data ||
			    fdc_mode == fdc_deleting     ||
			    fdc_mode == fdc_formatting   ||
			    fdc_mode == fdc_reading_id) {
				if (inb_p(fdc.msr) & FDC_BUSY) {
					TRACE(ft_t_bug,
					"***** FDC failure, busy too late");
				} else {
					TRACE(ft_t_bug,
					      "***** FDC failure, no busy");
				}
			} else {
				TRACE(ft_t_fdc_dma, "awaited stray interrupt");
			}
		}
		ft_hide_interrupt = 0;
	}
	/*    Handle sleep code.
	 */
	if (!ft_hide_interrupt) {
		ft_interrupt_seen ++;
		if (waitqueue_active(&ftape_wait_intr)) {
			wake_up_interruptible(&ftape_wait_intr);
		}
	} else {
		TRACE(ft_t_flow, "hiding interrupt while %s", 
		      waitqueue_active(&ftape_wait_intr) ? "waiting":"active");
	}
#ifdef TESTING
	t0 = ftape_timediff(t0, ftape_timestamp());
	if (t0 >= 1000) {
		/* only tell us about long calls */
		TRACE(ft_t_noise, "isr() duration: %5d usec", t0);
	}
#endif
	*fdc.hook = fdc_isr;	/* hook our handler into the fdc code again */
	--isr_active;
	TRACE_EXIT;
}