summaryrefslogtreecommitdiffstats
path: root/drivers/char/ftape/lowlevel/ftape-rw.c
blob: c0d6dc2cbfd31b23d25bfc874bc0c5bfa465f684 (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
/*
 *      Copyright (C) 1993-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/ftape-rw.c,v $
 * $Revision: 1.7 $
 * $Date: 1997/10/28 14:26:49 $
 *
 *      This file contains some common code for the segment read and
 *      segment write routines for the QIC-117 floppy-tape driver for
 *      Linux.
 */

#include <linux/string.h>
#include <linux/errno.h>

#include <linux/ftape.h>
#include <linux/qic117.h>
#include "../lowlevel/ftape-tracing.h"
#include "../lowlevel/ftape-rw.h"
#include "../lowlevel/fdc-io.h"
#include "../lowlevel/ftape-init.h"
#include "../lowlevel/ftape-io.h"
#include "../lowlevel/ftape-ctl.h"
#include "../lowlevel/ftape-read.h"
#include "../lowlevel/ftape-ecc.h"
#include "../lowlevel/ftape-bsm.h"

/*      Global vars.
 */
int ft_nr_buffers;
buffer_struct *ft_buffer[FT_MAX_NR_BUFFERS];
static volatile int ft_head;
static volatile int ft_tail;	/* not volatile but need same type as head */
int fdc_setup_error;
location_record ft_location = {-1, 0};
volatile int ftape_tape_running;

/*      Local vars.
 */
static int overrun_count_offset;
static int inhibit_correction;

/*  maxmimal allowed overshoot when fast seeking
 */
#define OVERSHOOT_LIMIT 10

/*      Increment cyclic buffer nr.
 */
buffer_struct *ftape_next_buffer(ft_buffer_queue_t pos)
{
	switch (pos) {
	case ft_queue_head:
		if (++ft_head >= ft_nr_buffers) {
			ft_head = 0;
		}
		return ft_buffer[ft_head];
	case ft_queue_tail:
		if (++ft_tail >= ft_nr_buffers) {
			ft_tail = 0;
		}
		return ft_buffer[ft_tail];
	default:
		return NULL;
	}
}
int ftape_buffer_id(ft_buffer_queue_t pos)
{
	switch(pos) {
	case ft_queue_head: return ft_head;
	case ft_queue_tail: return ft_tail;
	default: return -1;
	}
}
buffer_struct *ftape_get_buffer(ft_buffer_queue_t pos)
{
	switch(pos) {
	case ft_queue_head: return ft_buffer[ft_head];
	case ft_queue_tail: return ft_buffer[ft_tail];
	default: return NULL;
	}
}
void ftape_reset_buffer(void)
{
	ft_head = ft_tail = 0;
}

buffer_state_enum ftape_set_state(buffer_state_enum new_state)
{
	buffer_state_enum old_state = ft_driver_state;

	ft_driver_state = new_state;
	return old_state;
}
/*      Calculate Floppy Disk Controller and DMA parameters for a segment.
 *      head:   selects buffer struct in array.
 *      offset: number of physical sectors to skip (including bad ones).
 *      count:  number of physical sectors to handle (including bad ones).
 */
static int setup_segment(buffer_struct * buff, 
			 int segment_id,
			 unsigned int sector_offset, 
			 unsigned int sector_count, 
			 int retry)
{
	SectorMap offset_mask;
	SectorMap mask;
	TRACE_FUN(ft_t_any);

	buff->segment_id = segment_id;
	buff->sector_offset = sector_offset;
	buff->remaining = sector_count;
	buff->head = segment_id / ftape_segments_per_head;
	buff->cyl = (segment_id % ftape_segments_per_head) / ftape_segments_per_cylinder;
	buff->sect = (segment_id % ftape_segments_per_cylinder) * FT_SECTORS_PER_SEGMENT + 1;
	buff->deleted = 0;
	offset_mask = (1 << buff->sector_offset) - 1;
	mask = ftape_get_bad_sector_entry(segment_id) & offset_mask;
	while (mask) {
		if (mask & 1) {
			offset_mask >>= 1;	/* don't count bad sector */
		}
		mask >>= 1;
	}
	buff->data_offset = count_ones(offset_mask);	/* good sectors to skip */
	buff->ptr = buff->address + buff->data_offset * FT_SECTOR_SIZE;
	TRACE(ft_t_flow, "data offset = %d sectors", buff->data_offset);
	if (retry) {
		buff->soft_error_map &= offset_mask;	/* keep skipped part */
	} else {
		buff->hard_error_map = buff->soft_error_map = 0;
	}
	buff->bad_sector_map = ftape_get_bad_sector_entry(buff->segment_id);
	if (buff->bad_sector_map != 0) {
		TRACE(ft_t_noise, "segment: %d, bad sector map: %08lx",
			buff->segment_id, (long)buff->bad_sector_map);
	} else {
		TRACE(ft_t_flow, "segment: %d", buff->segment_id);
	}
	if (buff->sector_offset > 0) {
		buff->bad_sector_map >>= buff->sector_offset;
	}
	if (buff->sector_offset != 0 || buff->remaining != FT_SECTORS_PER_SEGMENT) {
		TRACE(ft_t_flow, "sector offset = %d, count = %d",
			buff->sector_offset, buff->remaining);
	}
	/*    Segments with 3 or less sectors are not written with valid
	 *    data because there is no space left for the ecc.  The
	 *    data written is whatever happens to be in the buffer.
	 *    Reading such a segment will return a zero byte-count.
	 *    To allow us to read/write segments with all bad sectors
	 *    we fake one readable sector in the segment. This
	 *    prevents having to handle these segments in a very
	 *    special way.  It is not important if the reading of this
	 *    bad sector fails or not (the data is ignored). It is
	 *    only read to keep the driver running.
	 *
	 *    The QIC-40/80 spec. has no information on how to handle
	 *    this case, so this is my interpretation.  
	 */
	if (buff->bad_sector_map == EMPTY_SEGMENT) {
		TRACE(ft_t_flow, "empty segment %d, fake first sector good",
		      buff->segment_id);
		if (buff->ptr != buff->address) {
			TRACE(ft_t_bug, "This is a bug: %p/%p",
			      buff->ptr, buff->address);
		}
		buff->bad_sector_map = FAKE_SEGMENT;
	}
	fdc_setup_error = 0;
	buff->next_segment = segment_id + 1;
	TRACE_EXIT 0;
}

/*      Calculate Floppy Disk Controller and DMA parameters for a new segment.
 */
int ftape_setup_new_segment(buffer_struct * buff, int segment_id, int skip)
{
	int result = 0;
	static int old_segment_id = -1;
	static buffer_state_enum old_ft_driver_state = idle;
	int retry = 0;
	unsigned offset = 0;
	int count = FT_SECTORS_PER_SEGMENT;
	TRACE_FUN(ft_t_flow);

	TRACE(ft_t_flow, "%s segment %d (old = %d)",
	      (ft_driver_state == reading || ft_driver_state == verifying) 
	      ? "reading" : "writing",
	      segment_id, old_segment_id);
	if (ft_driver_state != old_ft_driver_state) {	/* when verifying */
		old_segment_id = -1;
		old_ft_driver_state = ft_driver_state;
	}
	if (segment_id == old_segment_id) {
		++buff->retry;
		++ft_history.retries;
		TRACE(ft_t_flow, "setting up for retry nr %d", buff->retry);
		retry = 1;
		if (skip && buff->skip > 0) {	/* allow skip on retry */
			offset = buff->skip;
			count -= offset;
			TRACE(ft_t_flow, "skipping %d sectors", offset);
		}
	} else {
		buff->retry = 0;
		buff->skip = 0;
		old_segment_id = segment_id;
	}
	result = setup_segment(buff, segment_id, offset, count, retry);
	TRACE_EXIT result;
}

/*      Determine size of next cluster of good sectors.
 */
int ftape_calc_next_cluster(buffer_struct * buff)
{
	/* Skip bad sectors.
	 */
	while (buff->remaining > 0 && (buff->bad_sector_map & 1) != 0) {
		buff->bad_sector_map >>= 1;
		++buff->sector_offset;
		--buff->remaining;
	}
	/* Find next cluster of good sectors
	 */
	if (buff->bad_sector_map == 0) {	/* speed up */
		buff->sector_count = buff->remaining;
	} else {
		SectorMap map = buff->bad_sector_map;

		buff->sector_count = 0;
		while (buff->sector_count < buff->remaining && (map & 1) == 0) {
			++buff->sector_count;
			map >>= 1;
		}
	}
	return buff->sector_count;
}

/*  if just passed the last segment on a track, wait for BOT
 *  or EOT mark.
 */
int ftape_handle_logical_eot(void)
{
	TRACE_FUN(ft_t_flow);

	if (ft_runner_status == logical_eot) {
		int status;

		TRACE(ft_t_noise, "tape at logical EOT");
		TRACE_CATCH(ftape_ready_wait(ftape_timeout.seek, &status),);
		if ((status & (QIC_STATUS_AT_BOT | QIC_STATUS_AT_EOT)) == 0) {
			TRACE_ABORT(-EIO, ft_t_err, "eot/bot not reached");
		}
		ft_runner_status = end_of_tape;
	}
	if (ft_runner_status == end_of_tape) {
		TRACE(ft_t_noise, "runner stopped because of logical EOT");
		ft_runner_status = idle;
	}
	TRACE_EXIT 0;
}

static int check_bot_eot(int status)
{
	TRACE_FUN(ft_t_flow);

	if (status & (QIC_STATUS_AT_BOT | QIC_STATUS_AT_EOT)) {
		ft_location.bot = ((ft_location.track & 1) == 0 ?
				(status & QIC_STATUS_AT_BOT) != 0:
				(status & QIC_STATUS_AT_EOT) != 0);
		ft_location.eot = !ft_location.bot;
		ft_location.segment = (ft_location.track +
			(ft_location.bot ? 0 : 1)) * ft_segments_per_track - 1;
		ft_location.sector = -1;
		ft_location.known  = 1;
		TRACE(ft_t_flow, "tape at logical %s",
		      ft_location.bot ? "bot" : "eot");
		TRACE(ft_t_flow, "segment = %d", ft_location.segment);
	} else {
		ft_location.known = 0;
	}
	TRACE_EXIT ft_location.known;
}

/*      Read Id of first sector passing tape head.
 */
static int ftape_read_id(void)
{
	int status;
	__u8 out[2];
	TRACE_FUN(ft_t_any);

	/* Assume tape is running on entry, be able to handle
	 * situation where it stopped or is stopping.
	 */
	ft_location.known = 0;	/* default is location not known */
	out[0] = FDC_READID;
	out[1] = ft_drive_sel;
	TRACE_CATCH(fdc_command(out, 2),);
	switch (fdc_interrupt_wait(20 * FT_SECOND)) {
	case 0:
		if (fdc_sect == 0) {
			if (ftape_report_drive_status(&status) >= 0 &&
			    (status & QIC_STATUS_READY)) {
				ftape_tape_running = 0;
				TRACE(ft_t_flow, "tape has stopped");
				check_bot_eot(status);
			}
		} else {
			ft_location.known = 1;
			ft_location.segment = (ftape_segments_per_head
					       * fdc_head
					       + ftape_segments_per_cylinder
					       * fdc_cyl
					       + (fdc_sect - 1)
					       / FT_SECTORS_PER_SEGMENT);
			ft_location.sector = ((fdc_sect - 1)
					      % FT_SECTORS_PER_SEGMENT);
			ft_location.eot = ft_location.bot = 0;
		}
		break;
	case -ETIME:
		/*  Didn't find id on tape, must be near end: Wait
		 *  until stopped.
		 */
		if (ftape_ready_wait(FT_FOREVER, &status) >= 0) {
			ftape_tape_running = 0;
			TRACE(ft_t_flow, "tape has stopped");
			check_bot_eot(status);
		}
		break;
	default:
		/*  Interrupted or otherwise failing
		 *  fdc_interrupt_wait() 
		 */
		TRACE(ft_t_err, "fdc_interrupt_wait failed");
		break;
	}
	if (!ft_location.known) {
		TRACE_ABORT(-EIO, ft_t_flow, "no id found");
	}
	if (ft_location.sector == 0) {
		TRACE(ft_t_flow, "passing segment %d/%d",
		      ft_location.segment, ft_location.sector);
	} else {
		TRACE(ft_t_fdc_dma, "passing segment %d/%d",
		      ft_location.segment, ft_location.sector);
	}
	TRACE_EXIT 0;
}

static int logical_forward(void)
{
	ftape_tape_running = 1;
	return ftape_command(QIC_LOGICAL_FORWARD);
}

int ftape_stop_tape(int *pstatus)
{
	int retry = 0;
	int result;
	TRACE_FUN(ft_t_flow);

	do {
		result = ftape_command_wait(QIC_STOP_TAPE,
					    ftape_timeout.stop, pstatus);
		if (result == 0) {
			if ((*pstatus & QIC_STATUS_READY) == 0) {
				result = -EIO;
			} else {
				ftape_tape_running = 0;
			}
		}
	} while (result < 0 && ++retry <= 3);
	if (result < 0) {
		TRACE(ft_t_err, "failed ! (fatal)");
	}
	TRACE_EXIT result;
}

int ftape_dumb_stop(void)
{
	int result;
	int status;
	TRACE_FUN(ft_t_flow);

	/*  Abort current fdc operation if it's busy (probably read
	 *  or write operation pending) with a reset.
	 */
	if (fdc_ready_wait(100 /* usec */) < 0) {
		TRACE(ft_t_noise, "aborting fdc operation");
		fdc_reset();
	}
	/*  Reading id's after the last segment on a track may fail
	 *  but eventually the drive will become ready (logical eot).
	 */
	result = ftape_report_drive_status(&status);
	ft_location.known = 0;
	do {
		if (result == 0 && status & QIC_STATUS_READY) {
			/* Tape is not running any more.
			 */
			TRACE(ft_t_noise, "tape already halted");
			check_bot_eot(status);
			ftape_tape_running = 0;
		} else if (ftape_tape_running) {
			/*  Tape is (was) still moving.
			 */
#ifdef TESTING
			ftape_read_id();
#endif
			result = ftape_stop_tape(&status);
		} else {
			/*  Tape not yet ready but stopped.
			 */
			result = ftape_ready_wait(ftape_timeout.pause,&status);
		}
	} while (ftape_tape_running
		 && !(sigtestsetmask(&current->pending.signal, _NEVER_BLOCK)));
#ifndef TESTING
	ft_location.known = 0;
#endif
	if (ft_runner_status == aborting || ft_runner_status == do_abort) {
		ft_runner_status = idle;
	}
	TRACE_EXIT result;
}

/*      Wait until runner has finished tail buffer.
 *
 */
int ftape_wait_segment(buffer_state_enum state)
{
	int status;
	int result = 0;
	TRACE_FUN(ft_t_flow);

	while (ft_buffer[ft_tail]->status == state) {
		TRACE(ft_t_flow, "state: %d", ft_buffer[ft_tail]->status);
		/*  First buffer still being worked on, wait up to timeout.
		 *
		 *  Note: we check two times for being killed. 50
		 *  seconds are quite long. Note that
		 *  fdc_interrupt_wait() is not killable by any
		 *  means. ftape_read_segment() wants us to return
		 *  -EINTR in case of a signal.  
		 */
		FT_SIGNAL_EXIT(_DONT_BLOCK);
		result = fdc_interrupt_wait(50 * FT_SECOND);
		FT_SIGNAL_EXIT(_DONT_BLOCK);
		if (result < 0) {
			TRACE_ABORT(result,
				    ft_t_err, "fdc_interrupt_wait failed");
		}
		if (fdc_setup_error) {
			/* recover... FIXME */
			TRACE_ABORT(-EIO, ft_t_err, "setup error");
		}
	}
	if (ft_buffer[ft_tail]->status != error) {
		TRACE_EXIT 0;
	}
	TRACE_CATCH(ftape_report_drive_status(&status),);
	TRACE(ft_t_noise, "ftape_report_drive_status: 0x%02x", status);
	if ((status & QIC_STATUS_READY) && 
	    (status & QIC_STATUS_ERROR)) {
		unsigned int error;
		qic117_cmd_t command;
		
		/*  Report and clear error state.
		 *  In case the drive can't operate at the selected
		 *  rate, select the next lower data rate.
		 */
		ftape_report_error(&error, &command, 1);
		if (error == 31 && command == QIC_LOGICAL_FORWARD) {
			/* drive does not accept this data rate */
			if (ft_data_rate > 250) {
				TRACE(ft_t_info,
				      "Probable data rate conflict");
				TRACE(ft_t_info,
				      "Lowering data rate to %d Kbps",
				      ft_data_rate / 2);
				ftape_half_data_rate();
				if (ft_buffer[ft_tail]->retry > 0) {
					/* give it a chance */
					--ft_buffer[ft_tail]->retry;
				}
			} else {
				/* no rate is accepted... */
				TRACE(ft_t_err, "We're dead :(");
			}
		} else {
			TRACE(ft_t_err, "Unknown error");
		}
		TRACE_EXIT -EIO;   /* g.p. error */
	}
	TRACE_EXIT 0;
}

/* forward */ static int seek_forward(int segment_id, int fast);

static int fast_seek(int count, int reverse)
{
	int result = 0;
	int status;
	TRACE_FUN(ft_t_flow);

	if (count > 0) {
		/*  If positioned at begin or end of tape, fast seeking needs
		 *  special treatment.
		 *  Starting from logical bot needs a (slow) seek to the first
		 *  segment before the high speed seek. Most drives do this
		 *  automatically but some older don't, so we treat them
		 *  all the same.
		 *  Starting from logical eot is even more difficult because
		 *  we cannot (slow) reverse seek to the last segment.
		 *  TO BE IMPLEMENTED.
		 */
		inhibit_correction = 0;
		if (ft_location.known &&
		    ((ft_location.bot && !reverse) ||
		     (ft_location.eot && reverse))) {
			if (!reverse) {
				/*  (slow) skip to first segment on a track
				 */
				seek_forward(ft_location.track * ft_segments_per_track, 0);
				--count;
			} else {
				/*  When seeking backwards from
				 *  end-of-tape the number of erased
				 *  gaps found seems to be higher than
				 *  expected.  Therefor the drive must
				 *  skip some more segments than
				 *  calculated, but we don't know how
				 *  many.  Thus we will prevent the
				 *  re-calculation of offset and
				 *  overshoot when seeking backwards.
				 */
				inhibit_correction = 1;
				count += 3;	/* best guess */
			}
		}
	} else {
		TRACE(ft_t_flow, "warning: zero or negative count: %d", count);
	}
	if (count > 0) {
		int i;
		int nibbles = count > 255 ? 3 : 2;

		if (count > 4095) {
			TRACE(ft_t_noise, "skipping clipped at 4095 segment");
			count = 4095;
		}
		/* Issue this tape command first. */
		if (!reverse) {
			TRACE(ft_t_noise, "skipping %d segment(s)", count);
			result = ftape_command(nibbles == 3 ?
			   QIC_SKIP_EXTENDED_FORWARD : QIC_SKIP_FORWARD);
		} else {
			TRACE(ft_t_noise, "backing up %d segment(s)", count);
			result = ftape_command(nibbles == 3 ?
			   QIC_SKIP_EXTENDED_REVERSE : QIC_SKIP_REVERSE);
		}
		if (result < 0) {
			TRACE(ft_t_noise, "Skip command failed");
		} else {
			--count;	/* 0 means one gap etc. */
			for (i = 0; i < nibbles; ++i) {
				if (result >= 0) {
					result = ftape_parameter(count & 15);
					count /= 16;
				}
			}
			result = ftape_ready_wait(ftape_timeout.rewind, &status);
			if (result >= 0) {
				ftape_tape_running = 0;
			}
		}
	}
	TRACE_EXIT result;
}

static int validate(int id)
{
	/* Check to see if position found is off-track as reported
	 *  once.  Because all tracks in one direction lie next to
	 *  each other, if off-track the error will be approximately
	 *  2 * ft_segments_per_track.
	 */
	if (ft_location.track == -1) {
		return 1; /* unforseen situation, don't generate error */
	} else {
		/* Use margin of ft_segments_per_track on both sides
		 * because ftape needs some margin and the error we're
		 * looking for is much larger !
		 */
		int lo = (ft_location.track - 1) * ft_segments_per_track;
		int hi = (ft_location.track + 2) * ft_segments_per_track;

		return (id >= lo && id < hi);
	}
}

static int seek_forward(int segment_id, int fast)
{
	int failures = 0;
	int count;
	static int margin = 1;	/* fixed: stop this before target */
	static int overshoot = 1;
	static int min_count = 8;
	int expected = -1;
	int target = segment_id - margin;
	int fast_seeking;
	int prev_segment = ft_location.segment;
	TRACE_FUN(ft_t_flow);

	if (!ft_location.known) {
		TRACE_ABORT(-EIO, ft_t_err,
			    "fatal: cannot seek from unknown location");
	}
	if (!validate(segment_id)) {
		ftape_sleep(1 * FT_SECOND);
		ft_failure = 1;
		TRACE_ABORT(-EIO, ft_t_err,
			    "fatal: head off track (bad hardware?)");
	}
	TRACE(ft_t_noise, "from %d/%d to %d/0 - %d",
	      ft_location.segment, ft_location.sector,segment_id,margin);
	count = target - ft_location.segment - overshoot;
	fast_seeking = (fast &&
			count > (min_count + (ft_location.bot ? 1 : 0)));
	if (fast_seeking) {
		TRACE(ft_t_noise, "fast skipping %d segments", count);
		expected = segment_id - margin;
		fast_seek(count, 0);
	}
	if (!ftape_tape_running) {
		logical_forward();
	}
	while (ft_location.segment < segment_id) {
		/*  This requires at least one sector in a (bad) segment to
		 *  have a valid and readable sector id !
		 *  It looks like this is not guaranteed, so we must try
		 *  to find a way to skip an EMPTY_SEGMENT. !!! FIXME !!!
		 */
		if (ftape_read_id() < 0 || !ft_location.known ||
		    sigtestsetmask(&current->pending.signal, _DONT_BLOCK)) {
			ft_location.known = 0;
			if (!ftape_tape_running ||
			    ++failures > FT_SECTORS_PER_SEGMENT) {
				TRACE_ABORT(-EIO, ft_t_err,
					    "read_id failed completely");
			}
			FT_SIGNAL_EXIT(_DONT_BLOCK);
			TRACE(ft_t_flow, "read_id failed, retry (%d)",
			      failures);
			continue;
		}
		if (fast_seeking) {
			TRACE(ft_t_noise, "ended at %d/%d (%d,%d)",
			      ft_location.segment, ft_location.sector,
			      overshoot, inhibit_correction);
			if (!inhibit_correction &&
			    (ft_location.segment < expected ||
			     ft_location.segment > expected + margin)) {
				int error = ft_location.segment - expected;
				TRACE(ft_t_noise,
				      "adjusting overshoot from %d to %d",
				      overshoot, overshoot + error);
				overshoot += error;
				/*  All overshoots have the same
				 *  direction, so it should never
				 *  become negative, but who knows.
				 */
				if (overshoot < -5 ||
				    overshoot > OVERSHOOT_LIMIT) {
					if (overshoot < 0) {
						/* keep sane value */
						overshoot = -5;
					} else {
						/* keep sane value */
						overshoot = OVERSHOOT_LIMIT;
					}
					TRACE(ft_t_noise,
					      "clipped overshoot to %d",
					      overshoot);
				}
			}
			fast_seeking = 0;
		}
		if (ft_location.known) {
			if (ft_location.segment > prev_segment + 1) {
				TRACE(ft_t_noise,
				      "missed segment %d while skipping",
				      prev_segment + 1);
			}
			prev_segment = ft_location.segment;
		}
	}
	if (ft_location.segment > segment_id) {
		TRACE_ABORT(-EIO,
			    ft_t_noise, "failed: skip ended at segment %d/%d",
			    ft_location.segment, ft_location.sector);
	}
	TRACE_EXIT 0;
}

static int skip_reverse(int segment_id, int *pstatus)
{
	int failures = 0;
	static int overshoot = 1;
	static int min_rewind = 2;	/* 1 + overshoot */
	static const int margin = 1;	/* stop this before target */
	int expected = 0;
	int count = 1;
	int short_seek;
	int target = segment_id - margin;
	TRACE_FUN(ft_t_flow);

	if (ft_location.known && !validate(segment_id)) {
		ftape_sleep(1 * FT_SECOND);
		ft_failure = 1;
		TRACE_ABORT(-EIO, ft_t_err,
			    "fatal: head off track (bad hardware?)");
	}
	do {
		if (!ft_location.known) {
			TRACE(ft_t_warn, "warning: location not known");
		}
		TRACE(ft_t_noise, "from %d/%d to %d/0 - %d",
		      ft_location.segment, ft_location.sector,
		      segment_id, margin);
		/*  min_rewind == 1 + overshoot_when_doing_minimum_rewind
		 *  overshoot  == overshoot_when_doing_larger_rewind
		 *  Initially min_rewind == 1 + overshoot, optimization
		 *  of both values will be done separately.
		 *  overshoot and min_rewind can be negative as both are
		 *  sums of three components:
		 *  any_overshoot == rewind_overshoot - 
		 *                   stop_overshoot   -
		 *                   start_overshoot
		 */
		if (ft_location.segment - target - (min_rewind - 1) < 1) {
			short_seek = 1;
		} else {
			count = ft_location.segment - target - overshoot;
			short_seek = (count < 1);
		}
		if (short_seek) {
			count = 1;	/* do shortest rewind */
			expected = ft_location.segment - min_rewind;
			if (expected/ft_segments_per_track != ft_location.track) {
				expected = (ft_location.track * 
					    ft_segments_per_track);
			}
		} else {
			expected = target;
		}
		fast_seek(count, 1);
		logical_forward();
		if (ftape_read_id() < 0 || !ft_location.known ||
		    (sigtestsetmask(&current->pending.signal, _DONT_BLOCK))) {
			if ((!ftape_tape_running && !ft_location.known) ||
			    ++failures > FT_SECTORS_PER_SEGMENT) {
				TRACE_ABORT(-EIO, ft_t_err,
					    "read_id failed completely");
			}
			FT_SIGNAL_EXIT(_DONT_BLOCK);
			TRACE_CATCH(ftape_report_drive_status(pstatus),);
			TRACE(ft_t_noise, "ftape_read_id failed, retry (%d)",
			      failures);
			continue;
		}
		TRACE(ft_t_noise, "ended at %d/%d (%d,%d,%d)", 
		      ft_location.segment, ft_location.sector,
		      min_rewind, overshoot, inhibit_correction);
		if (!inhibit_correction &&
		    (ft_location.segment < expected ||
		     ft_location.segment > expected + margin)) {
			int error = expected - ft_location.segment;
			if (short_seek) {
				TRACE(ft_t_noise,
				      "adjusting min_rewind from %d to %d",
				      min_rewind, min_rewind + error);
				min_rewind += error;
				if (min_rewind < -5) {
					/* is this right ? FIXME ! */
					/* keep sane value */
					min_rewind = -5;
					TRACE(ft_t_noise, 
					      "clipped min_rewind to %d",
					      min_rewind);
				}
			} else {
				TRACE(ft_t_noise,
				      "adjusting overshoot from %d to %d",
				      overshoot, overshoot + error);
				overshoot += error;
				if (overshoot < -5 ||
				    overshoot > OVERSHOOT_LIMIT) {
					if (overshoot < 0) {
						/* keep sane value */
						overshoot = -5;
					} else {
						/* keep sane value */
						overshoot = OVERSHOOT_LIMIT;
					}
					TRACE(ft_t_noise,
					      "clipped overshoot to %d",
					      overshoot);
				}
			}
		}
	} while (ft_location.segment > segment_id);
	if (ft_location.known) {
		TRACE(ft_t_noise, "current location: %d/%d",
		      ft_location.segment, ft_location.sector);
	}
	TRACE_EXIT 0;
}

static int determine_position(void)
{
	int retry = 0;
	int status;
	int result;
	TRACE_FUN(ft_t_flow);

	if (!ftape_tape_running) {
		/*  This should only happen if tape is stopped by isr.
		 */
		TRACE(ft_t_flow, "waiting for tape stop");
		if (ftape_ready_wait(ftape_timeout.pause, &status) < 0) {
			TRACE(ft_t_flow, "drive still running (fatal)");
			ftape_tape_running = 1;	/* ? */
		}
	} else {
		ftape_report_drive_status(&status);
	}
	if (status & QIC_STATUS_READY) {
		/*  Drive must be ready to check error state !
		 */
		TRACE(ft_t_flow, "drive is ready");
		if (status & QIC_STATUS_ERROR) {
			unsigned int error;
			qic117_cmd_t command;

			/*  Report and clear error state, try to continue.
			 */
			TRACE(ft_t_flow, "error status set");
			ftape_report_error(&error, &command, 1);
			ftape_ready_wait(ftape_timeout.reset, &status);
			ftape_tape_running = 0;	/* ? */
		}
		if (check_bot_eot(status)) {
			if (ft_location.bot) {
				if ((status & QIC_STATUS_READY) == 0) {
					/* tape moving away from
					 * bot/eot, let's see if we
					 * can catch up with the first
					 * segment on this track.
					 */
				} else {
					TRACE(ft_t_flow,
					      "start tape from logical bot");
					logical_forward();	/* start moving */
				}
			} else {
				if ((status & QIC_STATUS_READY) == 0) {
					TRACE(ft_t_noise, "waiting for logical end of track");
					result = ftape_ready_wait(ftape_timeout.reset, &status);
					/* error handling needed ? */
				} else {
					TRACE(ft_t_noise,
					      "tape at logical end of track");
				}
			}
		} else {
			TRACE(ft_t_flow, "start tape");
			logical_forward();	/* start moving */
			ft_location.known = 0;	/* not cleared by logical forward ! */
		}
	}
	/* tape should be moving now, start reading id's
	 */
	while (!ft_location.known &&
	       retry++ < FT_SECTORS_PER_SEGMENT &&
	       (result = ftape_read_id()) < 0) {

		TRACE(ft_t_flow, "location unknown");

		/* exit on signal
		 */
		FT_SIGNAL_EXIT(_DONT_BLOCK);

		/*  read-id somehow failed, tape may
		 *  have reached end or some other
		 *  error happened.
		 */
		TRACE(ft_t_flow, "read-id failed");
		TRACE_CATCH(ftape_report_drive_status(&status),);
		TRACE(ft_t_err, "ftape_report_drive_status: 0x%02x", status);
		if (status & QIC_STATUS_READY) {
			ftape_tape_running = 0;
			TRACE(ft_t_noise, "tape stopped for unknown reason! "
			      "status = 0x%02x", status);
			if (status & QIC_STATUS_ERROR ||
			    !check_bot_eot(status)) {
				/* oops, tape stopped but not at end!
				 */
				TRACE_EXIT -EIO;
			}
		}
	}
	TRACE(ft_t_flow,
	      "tape is positioned at segment %d", ft_location.segment);
	TRACE_EXIT ft_location.known ? 0 : -EIO;
}

/*      Get the tape running and position it just before the
 *      requested segment.
 *      Seek tape-track and reposition as needed.
 */
int ftape_start_tape(int segment_id, int sector_offset)
{
	int track = segment_id / ft_segments_per_track;
	int result = -EIO;
	int status;
	static int last_segment = -1;
	static int bad_bus_timing = 0;
	/* number of segments passing the head between starting the tape
	 * and being able to access the first sector.
	 */
	static int start_offset = 1;
	int retry;
	TRACE_FUN(ft_t_flow);

	/* If sector_offset > 0, seek into wanted segment instead of
	 * into previous.
	 * This allows error recovery if a part of the segment is bad
	 * (erased) causing the tape drive to generate an index pulse
	 * thus causing a no-data error before the requested sector
	 * is reached.
	 */
	ftape_tape_running = 0;
	TRACE(ft_t_noise, "target segment: %d/%d%s", segment_id, sector_offset,
		ft_buffer[ft_head]->retry > 0 ? " retry" : "");
	if (ft_buffer[ft_head]->retry > 0) {	/* this is a retry */
		int dist = segment_id - last_segment;

		if ((int)ft_history.overrun_errors < overrun_count_offset) {
			overrun_count_offset = ft_history.overrun_errors;
		} else if (dist < 0 || dist > 50) {
			overrun_count_offset = ft_history.overrun_errors;
		} else if ((ft_history.overrun_errors -
			    overrun_count_offset) >= 8) {
			if (ftape_increase_threshold() >= 0) {
				--ft_buffer[ft_head]->retry;
				overrun_count_offset =
					ft_history.overrun_errors;
				TRACE(ft_t_warn, "increased threshold because "
				      "of excessive overrun errors");
			} else if (!bad_bus_timing && ft_data_rate >= 1000) {
				ftape_half_data_rate();
				--ft_buffer[ft_head]->retry;
				bad_bus_timing = 1;
				overrun_count_offset =
					ft_history.overrun_errors;
				TRACE(ft_t_warn, "reduced datarate because "
				      "of excessive overrun errors");
			}
		}
	}
	last_segment = segment_id;
	if (ft_location.track != track ||
	    (ftape_might_be_off_track && ft_buffer[ft_head]->retry== 0)) {
		/* current track unknown or not equal to destination
		 */
		ftape_ready_wait(ftape_timeout.seek, &status);
		ftape_seek_head_to_track(track);
		/* overrun_count_offset = ft_history.overrun_errors; */
	}
	result = -EIO;
	retry = 0;
	while (result < 0     &&
	       retry++ <= 5   &&
	       !ft_failure &&
	       !(sigtestsetmask(&current->pending.signal, _DONT_BLOCK))) {
		
		if (retry && start_offset < 5) {
			start_offset ++;
		}
		/*  Check if we are able to catch the requested
		 *  segment in time.
		 */
		if ((ft_location.known || (determine_position() == 0)) &&
		    ft_location.segment >=
		    (segment_id -
		     ((ftape_tape_running || ft_location.bot)
		      ? 0 : start_offset))) {
			/*  Too far ahead (in or past target segment).
			 */
			if (ftape_tape_running) {
				if ((result = ftape_stop_tape(&status)) < 0) {
					TRACE(ft_t_err,
					      "stop tape failed with code %d",
					      result);
					break;
				}
				TRACE(ft_t_noise, "tape stopped");
				ftape_tape_running = 0;
			}
			TRACE(ft_t_noise, "repositioning");
			++ft_history.rewinds;
			if (segment_id % ft_segments_per_track < start_offset){
				TRACE(ft_t_noise, "end of track condition\n"
				      KERN_INFO "segment_id        : %d\n"
				      KERN_INFO "ft_segments_per_track: %d\n"
				      KERN_INFO "start_offset      : %d",
				      segment_id, ft_segments_per_track, 
				      start_offset);
				      
				/*  If seeking to first segments on
				 *  track better do a complete rewind
				 *  to logical begin of track to get a
				 *  more steady tape motion.  
				 */
				result = ftape_command_wait(
					(ft_location.track & 1)
					? QIC_PHYSICAL_FORWARD
					: QIC_PHYSICAL_REVERSE,
					ftape_timeout.rewind, &status);
				check_bot_eot(status);	/* update location */
			} else {
				result= skip_reverse(segment_id - start_offset,
						     &status);
			}
		}
		if (!ft_location.known) {
			TRACE(ft_t_bug, "panic: location not known");
			result = -EIO;
			continue; /* while() will check for failure */
		}
		TRACE(ft_t_noise, "current segment: %d/%d",
		      ft_location.segment, ft_location.sector);
		/*  We're on the right track somewhere before the
		 *  wanted segment.  Start tape movement if needed and
		 *  skip to just before or inside the requested
		 *  segment. Keep tape running.  
		 */
		result = 0;
		if (ft_location.segment < 
		    (segment_id - ((ftape_tape_running || ft_location.bot)
				   ? 0 : start_offset))) {
			if (sector_offset > 0) {
				result = seek_forward(segment_id,
						      retry <= 3);
			} else {
				result = seek_forward(segment_id - 1,
						      retry <= 3);
			}
		}
		if (result == 0 &&
		    ft_location.segment !=
		    (segment_id - (sector_offset > 0 ? 0 : 1))) {
			result = -EIO;
		}
	}
	if (result < 0) {
		TRACE(ft_t_err, "failed to reposition");
	} else {
		ft_runner_status = running;
	}
	TRACE_EXIT result;
}
OpenPOWER on IntegriCloud