summaryrefslogtreecommitdiffstats
path: root/drivers/char/ftape/lowlevel/ftape-ctl.c
blob: 32e0439117900d1cb21aaae87bbf9750590b536c (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
/*
 *      Copyright (C) 1993-1996 Bas Laarhoven,
 *                    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-ctl.c,v $
 * $Revision: 1.4 $
 * $Date: 1997/11/11 14:37:44 $
 *
 *      This file contains the non-read/write ftape functions for the
 *      QIC-40/80/3010/3020 floppy-tape driver "ftape" for Linux.
 */

#include <linux/config.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/mman.h>

#include <linux/ftape.h>
#include <linux/qic117.h>
#include <asm/uaccess.h>
#include <asm/io.h>

/* ease porting between pre-2.4.x and later kernels */
#define vma_get_pgoff(v)      ((v)->vm_pgoff)

#include "../lowlevel/ftape-tracing.h"
#include "../lowlevel/ftape-io.h"
#include "../lowlevel/ftape-ctl.h"
#include "../lowlevel/ftape-write.h"
#include "../lowlevel/ftape-read.h"
#include "../lowlevel/ftape-rw.h"
#include "../lowlevel/ftape-bsm.h"

/*      Global vars.
 */
ftape_info ftape_status = {
/*  vendor information */
	{ 0, },     /* drive type */
/*  data rates */
	500,        /* used data rate */
	500,        /* drive max rate */
	500,        /* fdc max rate   */
/*  drive selection, either FTAPE_SEL_A/B/C/D */
	-1,     /* drive selection */
/*  flags set after decode the drive and tape status   */
	0,          /* formatted */
	1,          /* no tape */
	1,          /* write protected */
	1,          /* new tape */
/*  values of last queried drive/tape status and error */
	{{0,}},     /* last error code */
	{{0,}},     /* drive status, configuration, tape status */
/*  cartridge geometry */
        20,         /* tracks_per_tape */
        102,        /* segments_per_track */
/*  location of header segments, etc. */
	-1,     /* used_header_segment */
	-1,     /* header_segment_1 */
	-1,     /* header_segment_2 */
	-1,     /* first_data_segment */
        -1,     /* last_data_segment */
/*  the format code as stored in the header segment  */
	fmt_normal, /* format code */
/*  the default for the qic std: unknown */
	-1,
/*  is tape running? */
	idle,       /* runner_state */
/*  is tape reading/writing/verifying/formatting/deleting */
	idle,       /* driver state */
/*  flags fatal hardware error */
	1,          /* failure */
/*  history record */
	{ 0, }      /* history record */
};
	
int ftape_segments_per_head     = 1020;
int ftape_segments_per_cylinder = 4;
int ftape_init_drive_needed = 1; /* need to be global for ftape_reset_drive()
				  * in ftape-io.c
				  */

/*      Local vars.
 */
static const vendor_struct vendors[] = QIC117_VENDORS;
static const wakeup_method methods[] = WAKEUP_METHODS;

const ftape_info *ftape_get_status(void)
{
#if defined(STATUS_PARANOYA)
	static ftape_info get_status;

	get_status = ftape_status;
	return &get_status;
#else
	return &ftape_status; /*  maybe return only a copy of it to assure 
			       *  read only access
			       */
#endif
}

static int ftape_not_operational(int status)
{
	/* return true if status indicates tape can not be used.
	 */
	return ((status ^ QIC_STATUS_CARTRIDGE_PRESENT) &
		(QIC_STATUS_ERROR |
		 QIC_STATUS_CARTRIDGE_PRESENT |
		 QIC_STATUS_NEW_CARTRIDGE));
}

int ftape_seek_to_eot(void)
{
	int status;
	TRACE_FUN(ft_t_any);

	TRACE_CATCH(ftape_ready_wait(ftape_timeout.pause, &status),);
	while ((status & QIC_STATUS_AT_EOT) == 0) {
		if (ftape_not_operational(status)) {
			TRACE_EXIT -EIO;
		}
		TRACE_CATCH(ftape_command_wait(QIC_PHYSICAL_FORWARD,
					       ftape_timeout.rewind,&status),);
	}
	TRACE_EXIT 0;
}

int ftape_seek_to_bot(void)
{
	int status;
	TRACE_FUN(ft_t_any);

	TRACE_CATCH(ftape_ready_wait(ftape_timeout.pause, &status),);
	while ((status & QIC_STATUS_AT_BOT) == 0) {
		if (ftape_not_operational(status)) {
			TRACE_EXIT -EIO;
		}
		TRACE_CATCH(ftape_command_wait(QIC_PHYSICAL_REVERSE,
					       ftape_timeout.rewind,&status),);
	}
	TRACE_EXIT 0;
}

static int ftape_new_cartridge(void)
{
	ft_location.track = -1; /* force seek on first access */
	ftape_zap_read_buffers();
	ftape_zap_write_buffers();
	return 0;
}

int ftape_abort_operation(void)
{
	int result = 0;
	int status;
	TRACE_FUN(ft_t_flow);

	if (ft_runner_status == running) {
		TRACE(ft_t_noise, "aborting runner, waiting");
		
		ft_runner_status = do_abort;
		/* set timeout so that the tape will run to logical EOT
		 * if we missed the last sector and there are no queue pulses.
		 */
		result = ftape_dumb_stop();
	}
	if (ft_runner_status != idle) {
		if (ft_runner_status == do_abort) {
			TRACE(ft_t_noise, "forcing runner abort");
		}
		TRACE(ft_t_noise, "stopping tape");
		result = ftape_stop_tape(&status);
		ft_location.known = 0;
		ft_runner_status  = idle;
	}
	ftape_reset_buffer();
	ftape_zap_read_buffers();
	ftape_set_state(idle);
	TRACE_EXIT result;
}

static int lookup_vendor_id(unsigned int vendor_id)
{
	int i = 0;

	while (vendors[i].vendor_id != vendor_id) {
		if (++i >= NR_ITEMS(vendors)) {
			return -1;
		}
	}
	return i;
}

static void ftape_detach_drive(void)
{
	TRACE_FUN(ft_t_any);

	TRACE(ft_t_flow, "disabling tape drive and fdc");
	ftape_put_drive_to_sleep(ft_drive_type.wake_up);
	fdc_catch_stray_interrupts(1);	/* one always comes */
	fdc_disable();
	fdc_release_irq_and_dma();
	fdc_release_regions();
	TRACE_EXIT;
}

static void clear_history(void)
{
	ft_history.used = 0;
	ft_history.id_am_errors =
		ft_history.id_crc_errors =
		ft_history.data_am_errors =
		ft_history.data_crc_errors =
		ft_history.overrun_errors =
		ft_history.no_data_errors =
		ft_history.retries =
		ft_history.crc_errors =
		ft_history.crc_failures =
		ft_history.ecc_failures =
		ft_history.corrected =
		ft_history.defects =
		ft_history.rewinds = 0;
}

static int ftape_activate_drive(vendor_struct * drive_type)
{
	int result = 0;
	TRACE_FUN(ft_t_flow);

	/* If we already know the drive type, wake it up.
	 * Else try to find out what kind of drive is attached.
	 */
	if (drive_type->wake_up != unknown_wake_up) {
		TRACE(ft_t_flow, "enabling tape drive and fdc");
		result = ftape_wakeup_drive(drive_type->wake_up);
		if (result < 0) {
			TRACE(ft_t_err, "known wakeup method failed");
		}
	} else {
		wake_up_types method;
		const ft_trace_t old_tracing = TRACE_LEVEL;
		if (TRACE_LEVEL < ft_t_flow) {
			SET_TRACE_LEVEL(ft_t_bug);
		}

		/*  Try to awaken the drive using all known methods.
		 *  Lower tracing for a while.
		 */
		for (method=no_wake_up; method < NR_ITEMS(methods); ++method) {
			drive_type->wake_up = method;
#ifdef CONFIG_FT_TWO_DRIVES
			/*  Test setup for dual drive configuration.
			 *  /dev/rft2 uses mountain wakeup
			 *  /dev/rft3 uses colorado wakeup
			 *  Other systems will use the normal scheme.
			 */
			if ((ft_drive_sel < 2)                            ||
			    (ft_drive_sel == 2 && method == FT_WAKE_UP_1) ||
			    (ft_drive_sel == 3 && method == FT_WAKE_UP_2)) {
				result=ftape_wakeup_drive(drive_type->wake_up);
			} else {
				result = -EIO;
			}
#else
			result = ftape_wakeup_drive(drive_type->wake_up);
#endif
			if (result >= 0) {
				TRACE(ft_t_warn, "drive wakeup method: %s",
				      methods[drive_type->wake_up].name);
				break;
			}
		}
		SET_TRACE_LEVEL(old_tracing);

		if (method >= NR_ITEMS(methods)) {
			/* no response at all, cannot open this drive */
			drive_type->wake_up = unknown_wake_up;
			TRACE(ft_t_err, "no tape drive found !");
			result = -ENODEV;
		}
	}
	TRACE_EXIT result;
}

static int ftape_get_drive_status(void)
{
	int result;
	int status;
	TRACE_FUN(ft_t_flow);

	ft_no_tape = ft_write_protected = 0;
	/*    Tape drive is activated now.
	 *    First clear error status if present.
	 */
	do {
		result = ftape_ready_wait(ftape_timeout.reset, &status);
		if (result < 0) {
			if (result == -ETIME) {
				TRACE(ft_t_err, "ftape_ready_wait timeout");
			} else if (result == -EINTR) {
				TRACE(ft_t_err, "ftape_ready_wait aborted");
			} else {
				TRACE(ft_t_err, "ftape_ready_wait failed");
			}
			TRACE_EXIT -EIO;
		}
		/*  Clear error condition (drive is ready !)
		 */
		if (status & QIC_STATUS_ERROR) {
			unsigned int error;
			qic117_cmd_t command;

			TRACE(ft_t_err, "error status set");
			result = ftape_report_error(&error, &command, 1);
			if (result < 0) {
				TRACE(ft_t_err,
				      "report_error_code failed: %d", result);
				/* hope it's working next time */
				ftape_reset_drive();
				TRACE_EXIT -EIO;
			} else if (error != 0) {
				TRACE(ft_t_noise, "error code   : %d", error);
				TRACE(ft_t_noise, "error command: %d", command);
			}
		}
		if (status & QIC_STATUS_NEW_CARTRIDGE) {
			unsigned int error;
			qic117_cmd_t command;
			const ft_trace_t old_tracing = TRACE_LEVEL;
			SET_TRACE_LEVEL(ft_t_bug);

			/*  Undocumented feature: Must clear (not present!)
			 *  error here or we'll fail later.
			 */
			ftape_report_error(&error, &command, 1);

			SET_TRACE_LEVEL(old_tracing);
			TRACE(ft_t_info, "status: new cartridge");
			ft_new_tape = 1;
		} else {
			ft_new_tape = 0;
		}
		FT_SIGNAL_EXIT(_DONT_BLOCK);
	} while (status & QIC_STATUS_ERROR);
	
	ft_no_tape = !(status & QIC_STATUS_CARTRIDGE_PRESENT);
	ft_write_protected = (status & QIC_STATUS_WRITE_PROTECT) != 0;
	if (ft_no_tape) {
		TRACE(ft_t_warn, "no cartridge present");
	} else {
		if (ft_write_protected) {
			TRACE(ft_t_noise, "Write protected cartridge");
		}
	}
	TRACE_EXIT 0;
}

static void ftape_log_vendor_id(void)
{
	int vendor_index;
	TRACE_FUN(ft_t_flow);

	ftape_report_vendor_id(&ft_drive_type.vendor_id);
	vendor_index = lookup_vendor_id(ft_drive_type.vendor_id);
	if (ft_drive_type.vendor_id == UNKNOWN_VENDOR &&
	    ft_drive_type.wake_up == wake_up_colorado) {
		vendor_index = 0;
		/* hack to get rid of all this mail */
		ft_drive_type.vendor_id = 0;
	}
	if (vendor_index < 0) {
		/* Unknown vendor id, first time opening device.  The
		 * drive_type remains set to type found at wakeup
		 * time, this will probably keep the driver operating
		 * for this new vendor.  
		 */
		TRACE(ft_t_warn, "\n"
		      KERN_INFO "============ unknown vendor id ===========\n"
		      KERN_INFO "A new, yet unsupported tape drive is found\n"
		      KERN_INFO "Please report the following values:\n"
		      KERN_INFO "   Vendor id     : 0x%04x\n"
		      KERN_INFO "   Wakeup method : %s\n"
		      KERN_INFO "And a description of your tape drive\n"
		      KERN_INFO "to "THE_FTAPE_MAINTAINER"\n"
		      KERN_INFO "==========================================",
		      ft_drive_type.vendor_id,
		      methods[ft_drive_type.wake_up].name);
		ft_drive_type.speed = 0;		/* unknown */
	} else {
		ft_drive_type.name  = vendors[vendor_index].name;
		ft_drive_type.speed = vendors[vendor_index].speed;
		TRACE(ft_t_info, "tape drive type: %s", ft_drive_type.name);
		/* scan all methods for this vendor_id in table */
		while(ft_drive_type.wake_up != vendors[vendor_index].wake_up) {
			if (vendor_index < NR_ITEMS(vendors) - 1 &&
			    vendors[vendor_index + 1].vendor_id 
			    == 
			    ft_drive_type.vendor_id) {
				++vendor_index;
			} else {
				break;
			}
		}
		if (ft_drive_type.wake_up != vendors[vendor_index].wake_up) {
			TRACE(ft_t_warn, "\n"
		     KERN_INFO "==========================================\n"
		     KERN_INFO "wakeup type mismatch:\n"
		     KERN_INFO "found: %s, expected: %s\n"
		     KERN_INFO "please report this to "THE_FTAPE_MAINTAINER"\n"
		     KERN_INFO "==========================================",
			      methods[ft_drive_type.wake_up].name,
			      methods[vendors[vendor_index].wake_up].name);
		}
	}
	TRACE_EXIT;
}

void ftape_calc_timeouts(unsigned int qic_std,
			 unsigned int data_rate,
			 unsigned int tape_len)
{
	int speed;		/* deci-ips ! */
	int ff_speed;
	int length;
	TRACE_FUN(ft_t_any);

	/*                           tape transport speed
	 *  data rate:        QIC-40   QIC-80   QIC-3010 QIC-3020
	 *
	 *    250 Kbps        25 ips     n/a      n/a      n/a
	 *    500 Kbps        50 ips   34 ips   22.6 ips   n/a
	 *      1 Mbps          n/a    68 ips   45.2 ips 22.6 ips
	 *      2 Mbps          n/a      n/a      n/a    45.2 ips
	 *
	 *  fast tape transport speed is at least 68 ips.
	 */
	switch (qic_std) {
	case QIC_TAPE_QIC40:
		speed = (data_rate == 250) ? 250 : 500;
		break;
	case QIC_TAPE_QIC80:
		speed = (data_rate == 500) ? 340 : 680;
		break;
	case QIC_TAPE_QIC3010:
		speed = (data_rate == 500) ? 226 : 452;
		break;
	case QIC_TAPE_QIC3020:
		speed = (data_rate == 1000) ? 226 : 452;
		break;
	default:
		TRACE(ft_t_bug, "Unknown qic_std (bug) ?");
		speed = 500;
		break;
	}
	if (ft_drive_type.speed == 0) {
		unsigned long t0;
		static int dt = 0;     /* keep gcc from complaining */
		static int first_time = 1;

		/*  Measure the time it takes to wind to EOT and back to BOT.
		 *  If the tape length is known, calculate the rewind speed.
		 *  Else keep the time value for calculation of the rewind
		 *  speed later on, when the length _is_ known.
		 *  Ask for a report only when length and speed are both known.
		 */
		if (first_time) {
			ftape_seek_to_bot();
			t0 = jiffies;
			ftape_seek_to_eot();
			ftape_seek_to_bot();
			dt = (int) (((jiffies - t0) * FT_USPT) / 1000);
			if (dt < 1) {
				dt = 1;	/* prevent div by zero on failures */
			}
			first_time = 0;
			TRACE(ft_t_info,
			      "trying to determine seek timeout, got %d msec",
			      dt);
		}
		if (tape_len != 0) {
			ft_drive_type.speed = 
				(2 * 12 * tape_len * 1000) / dt;
			TRACE(ft_t_warn, "\n"
		     KERN_INFO "==========================================\n"
		     KERN_INFO "drive type: %s\n"
		     KERN_INFO "delta time = %d ms, length = %d ft\n"
		     KERN_INFO "has a maximum tape speed of %d ips\n"
		     KERN_INFO "please report this to "THE_FTAPE_MAINTAINER"\n"
		     KERN_INFO "==========================================",
			      ft_drive_type.name, dt, tape_len, 
			      ft_drive_type.speed);
		}
	}
	/*  Handle unknown length tapes as very long ones. We'll
	 *  determine the actual length from a header segment later.
	 *  This is normal for all modern (Wide,TR1/2/3) formats.
	 */
	if (tape_len <= 0) {
		TRACE(ft_t_noise,
		      "Unknown tape length, using maximal timeouts");
		length = QIC_TOP_TAPE_LEN;	/* use worst case values */
	} else {
		length = tape_len;		/* use actual values */
	}
	if (ft_drive_type.speed == 0) {
		ff_speed = speed; 
	} else {
		ff_speed = ft_drive_type.speed;
	}
	/*  time to go from bot to eot at normal speed (data rate):
	 *  time = (1+delta) * length (ft) * 12 (inch/ft) / speed (ips)
	 *  delta = 10 % for seek speed, 20 % for rewind speed.
	 */
	ftape_timeout.seek = (length * 132 * FT_SECOND) / speed;
	ftape_timeout.rewind = (length * 144 * FT_SECOND) / (10 * ff_speed);
	ftape_timeout.reset = 20 * FT_SECOND + ftape_timeout.rewind;
	TRACE(ft_t_noise, "timeouts for speed = %d, length = %d\n"
	      KERN_INFO "seek timeout  : %d sec\n"
	      KERN_INFO "rewind timeout: %d sec\n"
	      KERN_INFO "reset timeout : %d sec",
	      speed, length,
	      (ftape_timeout.seek + 500) / 1000,
	      (ftape_timeout.rewind + 500) / 1000,
	      (ftape_timeout.reset + 500) / 1000);
	TRACE_EXIT;
}

/* This function calibrates the datarate (i.e. determines the maximal
 * usable data rate) and sets the global variable ft_qic_std to qic_std
 *
 */
int ftape_calibrate_data_rate(unsigned int qic_std)
{
	int rate = ft_fdc_rate_limit;
	int result;
	TRACE_FUN(ft_t_flow);

	ft_qic_std = qic_std;

	if (ft_qic_std == -1) {
		TRACE_ABORT(-EIO, ft_t_err,
		"Unable to determine data rate if QIC standard is unknown");
	}

	/*  Select highest rate supported by both fdc and drive.
	 *  Start with highest rate supported by the fdc.
	 */
	while (fdc_set_data_rate(rate) < 0 && rate > 250) {
		rate /= 2;
	}
	TRACE(ft_t_info,
	      "Highest FDC supported data rate: %d Kbps", rate);
	ft_fdc_max_rate = rate;
	do {
		result = ftape_set_data_rate(rate, ft_qic_std);
	} while (result == -EINVAL && (rate /= 2) > 250);
	if (result < 0) {
		TRACE_ABORT(-EIO, ft_t_err, "set datarate failed");
	}
	ft_data_rate = rate;
	TRACE_EXIT 0;
}

static int ftape_init_drive(void)
{
	int status;
	qic_model model;
	unsigned int qic_std;
	unsigned int data_rate;
	TRACE_FUN(ft_t_flow);

	ftape_init_drive_needed = 0; /* don't retry if this fails ? */
	TRACE_CATCH(ftape_report_raw_drive_status(&status),);
	if (status & QIC_STATUS_CARTRIDGE_PRESENT) {
		if (!(status & QIC_STATUS_AT_BOT)) {
			/*  Antique drives will get here after a soft reset,
			 *  modern ones only if the driver is loaded when the
			 *  tape wasn't rewound properly.
			 */
			/* Tape should be at bot if new cartridge ! */
			ftape_seek_to_bot();
		}
		if (!(status & QIC_STATUS_REFERENCED)) {
			TRACE(ft_t_flow, "starting seek_load_point");
			TRACE_CATCH(ftape_command_wait(QIC_SEEK_LOAD_POINT,
						       ftape_timeout.reset,
						       &status),);
		}
	}
	ft_formatted = (status & QIC_STATUS_REFERENCED) != 0;
	if (!ft_formatted) {
		TRACE(ft_t_warn, "Warning: tape is not formatted !");
	}

	/*  report configuration aborts when ftape_tape_len == -1
	 *  unknown qic_std is okay if not formatted.
	 */
	TRACE_CATCH(ftape_report_configuration(&model,
					       &data_rate,
					       &qic_std,
					       &ftape_tape_len),);

	/*  Maybe add the following to the /proc entry
	 */
	TRACE(ft_t_info, "%s drive @ %d Kbps",
	      (model == prehistoric) ? "prehistoric" :
	      ((model == pre_qic117c) ? "pre QIC-117C" :
	       ((model == post_qic117b) ? "post QIC-117B" :
		"post QIC-117D")), data_rate);

	if (ft_formatted) {
		/*  initialize ft_used_data_rate to maximum value 
		 *  and set ft_qic_std
		 */
		TRACE_CATCH(ftape_calibrate_data_rate(qic_std),);
		if (ftape_tape_len == 0) {
			TRACE(ft_t_info, "unknown length QIC-%s tape",
			      (ft_qic_std == QIC_TAPE_QIC40) ? "40" :
			      ((ft_qic_std == QIC_TAPE_QIC80) ? "80" :
			       ((ft_qic_std == QIC_TAPE_QIC3010) 
				? "3010" : "3020")));
		} else {
			TRACE(ft_t_info, "%d ft. QIC-%s tape", ftape_tape_len,
			      (ft_qic_std == QIC_TAPE_QIC40) ? "40" :
			      ((ft_qic_std == QIC_TAPE_QIC80) ? "80" :
			       ((ft_qic_std == QIC_TAPE_QIC3010)
				? "3010" : "3020")));
		}
		ftape_calc_timeouts(ft_qic_std, ft_data_rate, ftape_tape_len);
		/* soft write-protect QIC-40/QIC-80 cartridges used with a
		 * Colorado T3000 drive. Buggy hardware!
		 */
		if ((ft_drive_type.vendor_id == 0x011c6) &&
		    ((ft_qic_std == QIC_TAPE_QIC40 ||
		      ft_qic_std == QIC_TAPE_QIC80) &&
		     !ft_write_protected)) {
			TRACE(ft_t_warn, "\n"
	KERN_INFO "The famous Colorado T3000 bug:\n"
	KERN_INFO "%s drives can't write QIC40 and QIC80\n"
	KERN_INFO "cartridges but don't set the write-protect flag!",
			      ft_drive_type.name);
			ft_write_protected = 1;
		}
	} else {
		/*  Doesn't make too much sense to set the data rate
		 *  because we don't know what to use for the write
		 *  precompensation.
		 *  Need to do this again when formatting the cartridge.
		 */
		ft_data_rate = data_rate;
		ftape_calc_timeouts(QIC_TAPE_QIC40,
				    data_rate,
				    ftape_tape_len);
	}
	ftape_new_cartridge();
	TRACE_EXIT 0;
}

static void ftape_munmap(void)
{
	int i;
	TRACE_FUN(ft_t_flow);
	
	for (i = 0; i < ft_nr_buffers; i++) {
		ft_buffer[i]->mmapped = 0;
	}
	TRACE_EXIT;
}

/*   Map the dma buffers into the virtual address range given by vma.
 *   We only check the caller doesn't map non-existent buffers. We
 *   don't check for multiple mappings.
 */
int ftape_mmap(struct vm_area_struct *vma)
{
	int num_buffers;
	int i;
	TRACE_FUN(ft_t_flow);
	
	if (ft_failure) {
		TRACE_EXIT -ENODEV;
	}
	if (!(vma->vm_flags & (VM_READ|VM_WRITE))) {
		TRACE_ABORT(-EINVAL, ft_t_err, "Undefined mmap() access");
	}
	if (vma_get_pgoff(vma) != 0) {
		TRACE_ABORT(-EINVAL, ft_t_err, "page offset must be 0");
	}
	if ((vma->vm_end - vma->vm_start) % FT_BUFF_SIZE != 0) {
		TRACE_ABORT(-EINVAL, ft_t_err,
			    "size = %ld, should be a multiple of %d",
			    vma->vm_end - vma->vm_start,
			    FT_BUFF_SIZE);
	}
	num_buffers = (vma->vm_end - vma->vm_start) / FT_BUFF_SIZE;
	if (num_buffers > ft_nr_buffers) {
		TRACE_ABORT(-EINVAL,
			    ft_t_err, "size = %ld, should be less than %d",
			    vma->vm_end - vma->vm_start,
			    ft_nr_buffers * FT_BUFF_SIZE);
	}
	if (ft_driver_state != idle) {
		/* this also clears the buffer states 
		 */
		ftape_abort_operation();
	} else {
		ftape_reset_buffer();
	}
	for (i = 0; i < num_buffers; i++) {
		unsigned long pfn;

		pfn = virt_to_phys(ft_buffer[i]->address) >> PAGE_SHIFT;
		TRACE_CATCH(remap_pfn_range(vma, vma->vm_start +
					     i * FT_BUFF_SIZE,
					     pfn,
					     FT_BUFF_SIZE,
					     vma->vm_page_prot),
			    _res = -EAGAIN);
		TRACE(ft_t_noise, "remapped dma buffer @ %p to location @ %p",
		      ft_buffer[i]->address,
		      (void *)(vma->vm_start + i * FT_BUFF_SIZE));
	}
	for (i = 0; i < num_buffers; i++) {
		memset(ft_buffer[i]->address, 0xAA, FT_BUFF_SIZE);
		ft_buffer[i]->mmapped++;
	}	
	TRACE_EXIT 0;
}

static void ftape_init_driver(void); /* forward declaration */

/*      OPEN routine called by kernel-interface code
 */
int ftape_enable(int drive_selection)
{
	TRACE_FUN(ft_t_any);

	if (ft_drive_sel == -1 || ft_drive_sel != drive_selection) {
		/* Other selection than last time
		 */
		ftape_init_driver();
	}
	ft_drive_sel = FTAPE_SEL(drive_selection);
	ft_failure = 0;
	TRACE_CATCH(fdc_init(),); /* init & detect fdc */
	TRACE_CATCH(ftape_activate_drive(&ft_drive_type),
		    fdc_disable();
		    fdc_release_irq_and_dma();
		    fdc_release_regions());
	TRACE_CATCH(ftape_get_drive_status(), ftape_detach_drive());
	if (ft_drive_type.vendor_id == UNKNOWN_VENDOR) {
		ftape_log_vendor_id();
	}
	if (ft_new_tape) {
		ftape_init_drive_needed = 1;
	}
	if (!ft_no_tape && ftape_init_drive_needed) {
		TRACE_CATCH(ftape_init_drive(), ftape_detach_drive());
	}
	ftape_munmap(); /* clear the mmap flag */
	clear_history();
	TRACE_EXIT 0;
}

/*   release routine called by the high level interface modules
 *   zftape or sftape.
 */
void ftape_disable(void)
{
	int i;
	TRACE_FUN(ft_t_any);

	for (i = 0; i < ft_nr_buffers; i++) {
		if (ft_buffer[i]->mmapped) {
			TRACE(ft_t_noise, "first byte of buffer %d: 0x%02x",
			      i, *ft_buffer[i]->address);
		}
	}
	if (sigtestsetmask(&current->pending.signal, _DONT_BLOCK) && 
	    !(sigtestsetmask(&current->pending.signal, _NEVER_BLOCK)) &&
	    ftape_tape_running) {
		TRACE(ft_t_warn,
		      "Interrupted by fatal signal and tape still running");
		ftape_dumb_stop();
		ftape_abort_operation(); /* it's annoying */
	} else {
		ftape_set_state(idle);
	}
	ftape_detach_drive();
	if (ft_history.used) {
		TRACE(ft_t_info, "== Non-fatal errors this run: ==");
		TRACE(ft_t_info, "fdc isr statistics:\n"
		      KERN_INFO " id_am_errors     : %3d\n"
		      KERN_INFO " id_crc_errors    : %3d\n"
		      KERN_INFO " data_am_errors   : %3d\n"
		      KERN_INFO " data_crc_errors  : %3d\n"
		      KERN_INFO " overrun_errors   : %3d\n"
		      KERN_INFO " no_data_errors   : %3d\n"
		      KERN_INFO " retries          : %3d",
		      ft_history.id_am_errors,   ft_history.id_crc_errors,
		      ft_history.data_am_errors, ft_history.data_crc_errors,
		      ft_history.overrun_errors, ft_history.no_data_errors,
		      ft_history.retries);
		if (ft_history.used & 1) {
			TRACE(ft_t_info, "ecc statistics:\n"
			      KERN_INFO " crc_errors       : %3d\n"
			      KERN_INFO " crc_failures     : %3d\n"
			      KERN_INFO " ecc_failures     : %3d\n"
			      KERN_INFO " sectors corrected: %3d",
			      ft_history.crc_errors,   ft_history.crc_failures,
			      ft_history.ecc_failures, ft_history.corrected);
		}
		if (ft_history.defects > 0) {
			TRACE(ft_t_warn, "Warning: %d media defects!",
			      ft_history.defects);
		}
		if (ft_history.rewinds > 0) {
			TRACE(ft_t_info, "tape motion statistics:\n"
			      KERN_INFO "repositions       : %3d",
			      ft_history.rewinds);
		}
	}
	ft_failure = 1;
	TRACE_EXIT;
}

static void ftape_init_driver(void)
{
	TRACE_FUN(ft_t_flow);

	ft_drive_type.vendor_id = UNKNOWN_VENDOR;
	ft_drive_type.speed     = 0;
	ft_drive_type.wake_up   = unknown_wake_up;
	ft_drive_type.name      = "Unknown";

	ftape_timeout.seek      = 650 * FT_SECOND;
	ftape_timeout.reset     = 670 * FT_SECOND;
	ftape_timeout.rewind    = 650 * FT_SECOND;
	ftape_timeout.head_seek =  15 * FT_SECOND;
	ftape_timeout.stop      =   5 * FT_SECOND;
	ftape_timeout.pause     =  16 * FT_SECOND;

	ft_qic_std             = -1;
	ftape_tape_len         = 0;  /* unknown */
	ftape_current_command  = 0;
	ftape_current_cylinder = -1;

	ft_segments_per_track       = 102;
	ftape_segments_per_head     = 1020;
	ftape_segments_per_cylinder = 4;
	ft_tracks_per_tape          = 20;

	ft_failure = 1;

	ft_formatted       = 0;
	ft_no_tape         = 1;
	ft_write_protected = 1;
	ft_new_tape        = 1;

	ft_driver_state = idle;

	ft_data_rate = 
		ft_fdc_max_rate   = 500;
	ft_drive_max_rate = 0; /* triggers set_rate_test() */

	ftape_init_drive_needed = 1;

	ft_header_segment_1    = -1;
	ft_header_segment_2    = -1;
	ft_used_header_segment = -1;
	ft_first_data_segment  = -1;
	ft_last_data_segment   = -1;

	ft_location.track = -1;
	ft_location.known = 0;

	ftape_tape_running = 0;
	ftape_might_be_off_track = 1;

	ftape_new_cartridge();	/* init some tape related variables */
	ftape_init_bsm();
	TRACE_EXIT;
}
OpenPOWER on IntegriCloud