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#define IDETAPE_VERSION "1.19"
426
427#include <linux/module.h>
428#include <linux/types.h>
429#include <linux/string.h>
430#include <linux/kernel.h>
431#include <linux/delay.h>
432#include <linux/timer.h>
433#include <linux/mm.h>
434#include <linux/interrupt.h>
435#include <linux/jiffies.h>
436#include <linux/major.h>
437#include <linux/errno.h>
438#include <linux/genhd.h>
439#include <linux/slab.h>
440#include <linux/pci.h>
441#include <linux/ide.h>
442#include <linux/smp_lock.h>
443#include <linux/completion.h>
444#include <linux/bitops.h>
445#include <linux/mutex.h>
446
447#include <asm/byteorder.h>
448#include <asm/irq.h>
449#include <asm/uaccess.h>
450#include <asm/io.h>
451#include <asm/unaligned.h>
452
453
454
455
456typedef struct os_partition_s {
457 __u8 partition_num;
458 __u8 par_desc_ver;
459 __u16 wrt_pass_cntr;
460 __u32 first_frame_addr;
461 __u32 last_frame_addr;
462 __u32 eod_frame_addr;
463} os_partition_t;
464
465
466
467
468typedef struct os_dat_entry_s {
469 __u32 blk_sz;
470 __u16 blk_cnt;
471 __u8 flags;
472 __u8 reserved;
473} os_dat_entry_t;
474
475
476
477
478#define OS_DAT_FLAGS_DATA (0xc)
479#define OS_DAT_FLAGS_MARK (0x1)
480
481typedef struct os_dat_s {
482 __u8 dat_sz;
483 __u8 reserved1;
484 __u8 entry_cnt;
485 __u8 reserved3;
486 os_dat_entry_t dat_list[16];
487} os_dat_t;
488
489#include <linux/mtio.h>
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509#define IDETAPE_MIN_PIPELINE_STAGES 1
510#define IDETAPE_MAX_PIPELINE_STAGES 400
511#define IDETAPE_INCREASE_STAGES_RATE 20
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531#define IDETAPE_DEBUG_INFO 0
532#define IDETAPE_DEBUG_LOG 0
533#define IDETAPE_DEBUG_BUGS 1
534
535
536
537
538
539
540
541#define IDETAPE_MAX_PC_RETRIES 3
542
543
544
545
546
547
548#define IDETAPE_PC_BUFFER_SIZE 256
549
550
551
552
553
554
555#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
556
557
558
559
560
561
562#define IDETAPE_WAIT_CMD (900*HZ)
563
564
565
566
567
568
569
570
571#define IDETAPE_FIFO_THRESHOLD 2
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#define IDETAPE_DSC_RW_MIN 5*HZ/100
608#define IDETAPE_DSC_RW_MAX 40*HZ/100
609#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ
610#define IDETAPE_DSC_MA_FAST 2*HZ
611#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ
612#define IDETAPE_DSC_MA_SLOW 30*HZ
613#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ
614
615
616
617
618
619
620
621
622#define USE_IOTRACE 0
623#if USE_IOTRACE
624#define IO_IDETAPE_FIFO 500
625#endif
626
627
628
629
630#define SIMULATE_ERRORS 0
631
632
633
634
635typedef enum {
636 idetape_direction_none,
637 idetape_direction_read,
638 idetape_direction_write
639} idetape_chrdev_direction_t;
640
641struct idetape_bh {
642 u32 b_size;
643 atomic_t b_count;
644 struct idetape_bh *b_reqnext;
645 char *b_data;
646};
647
648
649
650
651typedef struct idetape_packet_command_s {
652 u8 c[12];
653 int retries;
654 int error;
655 int request_transfer;
656 int actually_transferred;
657 int buffer_size;
658 struct idetape_bh *bh;
659 char *b_data;
660 int b_count;
661 u8 *buffer;
662 u8 *current_position;
663 ide_startstop_t (*callback) (ide_drive_t *);
664 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE];
665 unsigned long flags;
666} idetape_pc_t;
667
668
669
670
671
672#define PC_ABORT 0
673
674#define PC_WAIT_FOR_DSC 1
675
676#define PC_DMA_RECOMMENDED 2
677
678#define PC_DMA_IN_PROGRESS 3
679
680#define PC_DMA_ERROR 4
681
682#define PC_WRITING 5
683
684
685
686
687typedef struct {
688 unsigned page_code :6;
689 __u8 reserved0_6 :1;
690 __u8 ps :1;
691 __u8 page_length;
692 __u8 reserved2, reserved3;
693 unsigned ro :1;
694 unsigned reserved4_1234 :4;
695 unsigned sprev :1;
696 unsigned reserved4_67 :2;
697 unsigned reserved5_012 :3;
698 unsigned efmt :1;
699 unsigned reserved5_4 :1;
700 unsigned qfa :1;
701 unsigned reserved5_67 :2;
702 unsigned lock :1;
703 unsigned locked :1;
704 unsigned prevent :1;
705 unsigned eject :1;
706 __u8 disconnect :1;
707 __u8 reserved6_5 :1;
708 unsigned ecc :1;
709 unsigned cmprs :1;
710 unsigned reserved7_0 :1;
711 unsigned blk512 :1;
712 unsigned blk1024 :1;
713 unsigned reserved7_3_6 :4;
714 unsigned blk32768 :1;
715
716
717 __u16 max_speed;
718 __u8 reserved10, reserved11;
719 __u16 ctl;
720 __u16 speed;
721 __u16 buffer_size;
722 __u8 reserved18, reserved19;
723} idetape_capabilities_page_t;
724
725
726
727
728typedef struct {
729 unsigned page_code :6;
730 unsigned reserved1_6 :1;
731 unsigned ps :1;
732 __u8 page_length;
733 __u8 reserved2;
734 unsigned play32 :1;
735 unsigned play32_5 :1;
736 unsigned reserved2_23 :2;
737 unsigned record32 :1;
738 unsigned record32_5 :1;
739 unsigned reserved2_6 :1;
740 unsigned one :1;
741} idetape_block_size_page_t;
742
743
744
745
746typedef struct idetape_stage_s {
747 struct request rq;
748 struct idetape_bh *bh;
749 struct idetape_stage_s *next;
750} idetape_stage_t;
751
752
753
754
755typedef struct {
756 unsigned error_code :7;
757 unsigned valid :1;
758 __u8 reserved1 :8;
759 unsigned sense_key :4;
760 unsigned reserved2_4 :1;
761 unsigned ili :1;
762 unsigned eom :1;
763 unsigned filemark :1;
764 __u32 information __attribute__ ((packed));
765 __u8 asl;
766 __u32 command_specific;
767 __u8 asc;
768 __u8 ascq;
769 __u8 replaceable_unit_code;
770 unsigned sk_specific1 :7;
771 unsigned sksv :1;
772 __u8 sk_specific2;
773 __u8 sk_specific3;
774 __u8 pad[2];
775} idetape_request_sense_result_t;
776
777
778
779
780
781
782
783typedef struct ide_tape_obj {
784 ide_drive_t *drive;
785 ide_driver_t *driver;
786 struct gendisk *disk;
787 struct kref kref;
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805 idetape_pc_t *pc;
806
807 idetape_pc_t *failed_pc;
808
809 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
810
811 int pc_stack_index;
812 struct request rq_stack[IDETAPE_PC_STACK];
813
814 int rq_stack_index;
815
816
817
818
819
820
821
822
823
824
825
826
827 struct request *postponed_rq;
828
829 unsigned long dsc_polling_start;
830
831 struct timer_list dsc_timer;
832
833 unsigned long best_dsc_rw_frequency;
834
835 unsigned long dsc_polling_frequency;
836
837 unsigned long dsc_timeout;
838
839
840
841
842 u8 partition;
843
844 unsigned int first_frame_position;
845 unsigned int last_frame_position;
846 unsigned int blocks_in_buffer;
847
848
849
850
851 u8 sense_key, asc, ascq;
852
853
854
855
856 unsigned int minor;
857
858 char name[4];
859
860 idetape_chrdev_direction_t chrdev_direction;
861
862
863
864
865
866 unsigned short tape_block_size;
867 int user_bs_factor;
868
869 idetape_capabilities_page_t capabilities;
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885 struct request *active_data_request;
886
887 int stage_size;
888 idetape_stage_t *merge_stage;
889 int merge_stage_size;
890 struct idetape_bh *bh;
891 char *b_data;
892 int b_count;
893
894
895
896
897
898
899
900
901 int nr_stages;
902
903 int nr_pending_stages;
904
905 int max_stages, min_pipeline, max_pipeline;
906
907 idetape_stage_t *first_stage;
908
909 idetape_stage_t *active_stage;
910
911 idetape_stage_t *next_stage;
912
913 idetape_stage_t *last_stage;
914
915 idetape_stage_t *cache_stage;
916 int pages_per_stage;
917
918 int excess_bh_size;
919
920
921 unsigned long flags;
922
923 spinlock_t spinlock;
924
925
926
927
928 unsigned long avg_time;
929 int avg_size;
930 int avg_speed;
931
932
933 idetape_request_sense_result_t sense;
934
935 char vendor_id[10];
936 char product_id[18];
937 char firmware_revision[6];
938 int firmware_revision_num;
939
940
941 int door_locked;
942
943 char drv_write_prot;
944
945 char write_prot;
946
947
948
949
950
951
952
953 int postpone_cnt;
954
955
956
957
958
959
960
961
962 int pipeline_head;
963 int buffer_head;
964 int tape_head;
965 int last_tape_head;
966
967
968
969
970 unsigned long insert_time;
971 int insert_size;
972 int insert_speed;
973 int max_insert_speed;
974 int measure_insert_time;
975
976
977
978
979 unsigned long tape_still_time_begin;
980 int tape_still_time;
981
982
983
984
985 int speed_control;
986 int pipeline_head_speed;
987 int controlled_pipeline_head_speed;
988 int uncontrolled_pipeline_head_speed;
989 int controlled_last_pipeline_head;
990 int uncontrolled_last_pipeline_head;
991 unsigned long uncontrolled_pipeline_head_time;
992 unsigned long controlled_pipeline_head_time;
993 int controlled_previous_pipeline_head;
994 int uncontrolled_previous_pipeline_head;
995 unsigned long controlled_previous_head_time;
996 unsigned long uncontrolled_previous_head_time;
997 int restart_speed_control_req;
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010 int debug_level;
1011} idetape_tape_t;
1012
1013static DEFINE_MUTEX(idetape_ref_mutex);
1014
1015static struct class *idetape_sysfs_class;
1016
1017#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
1018
1019#define ide_tape_g(disk) \
1020 container_of((disk)->private_data, struct ide_tape_obj, driver)
1021
1022static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
1023{
1024 struct ide_tape_obj *tape = NULL;
1025
1026 mutex_lock(&idetape_ref_mutex);
1027 tape = ide_tape_g(disk);
1028 if (tape)
1029 kref_get(&tape->kref);
1030 mutex_unlock(&idetape_ref_mutex);
1031 return tape;
1032}
1033
1034static void ide_tape_release(struct kref *);
1035
1036static void ide_tape_put(struct ide_tape_obj *tape)
1037{
1038 mutex_lock(&idetape_ref_mutex);
1039 kref_put(&tape->kref, ide_tape_release);
1040 mutex_unlock(&idetape_ref_mutex);
1041}
1042
1043
1044
1045
1046#define DOOR_UNLOCKED 0
1047#define DOOR_LOCKED 1
1048#define DOOR_EXPLICITLY_LOCKED 2
1049
1050
1051
1052
1053#define IDETAPE_IGNORE_DSC 0
1054#define IDETAPE_ADDRESS_VALID 1
1055#define IDETAPE_BUSY 2
1056#define IDETAPE_PIPELINE_ERROR 3
1057#define IDETAPE_DETECT_BS 4
1058#define IDETAPE_FILEMARK 5
1059#define IDETAPE_DRQ_INTERRUPT 6
1060#define IDETAPE_READ_ERROR 7
1061#define IDETAPE_PIPELINE_ACTIVE 8
1062
1063#define IDETAPE_MEDIUM_PRESENT 9
1064
1065
1066
1067
1068#define IDETAPE_TEST_UNIT_READY_CMD 0x00
1069#define IDETAPE_REWIND_CMD 0x01
1070#define IDETAPE_REQUEST_SENSE_CMD 0x03
1071#define IDETAPE_READ_CMD 0x08
1072#define IDETAPE_WRITE_CMD 0x0a
1073#define IDETAPE_WRITE_FILEMARK_CMD 0x10
1074#define IDETAPE_SPACE_CMD 0x11
1075#define IDETAPE_INQUIRY_CMD 0x12
1076#define IDETAPE_ERASE_CMD 0x19
1077#define IDETAPE_MODE_SENSE_CMD 0x1a
1078#define IDETAPE_MODE_SELECT_CMD 0x15
1079#define IDETAPE_LOAD_UNLOAD_CMD 0x1b
1080#define IDETAPE_PREVENT_CMD 0x1e
1081#define IDETAPE_LOCATE_CMD 0x2b
1082#define IDETAPE_READ_POSITION_CMD 0x34
1083#define IDETAPE_READ_BUFFER_CMD 0x3c
1084#define IDETAPE_SET_SPEED_CMD 0xbb
1085
1086
1087
1088
1089#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
1090
1091
1092
1093
1094#define IDETAPE_SPACE_OVER_FILEMARK 1
1095#define IDETAPE_SPACE_TO_EOD 3
1096
1097
1098
1099
1100#define IDETAPE_LU_LOAD_MASK 1
1101#define IDETAPE_LU_RETENSION_MASK 2
1102#define IDETAPE_LU_EOT_MASK 4
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112enum {
1113 REQ_IDETAPE_PC1 = (1 << 0),
1114 REQ_IDETAPE_PC2 = (1 << 1),
1115 REQ_IDETAPE_READ = (1 << 2),
1116 REQ_IDETAPE_WRITE = (1 << 3),
1117 REQ_IDETAPE_READ_BUFFER = (1 << 4),
1118};
1119
1120
1121
1122
1123
1124#define IDETAPE_ERROR_GENERAL 101
1125#define IDETAPE_ERROR_FILEMARK 102
1126#define IDETAPE_ERROR_EOD 103
1127
1128
1129
1130
1131
1132struct idetape_id_gcw {
1133 unsigned packet_size :2;
1134 unsigned reserved234 :3;
1135 unsigned drq_type :2;
1136 unsigned removable :1;
1137 unsigned device_type :5;
1138 unsigned reserved13 :1;
1139 unsigned protocol :2;
1140};
1141
1142
1143
1144
1145typedef struct {
1146 unsigned device_type :5;
1147 unsigned reserved0_765 :3;
1148 unsigned reserved1_6t0 :7;
1149 unsigned rmb :1;
1150 unsigned ansi_version :3;
1151 unsigned ecma_version :3;
1152 unsigned iso_version :2;
1153 unsigned response_format :4;
1154 unsigned reserved3_45 :2;
1155 unsigned reserved3_6 :1;
1156 unsigned reserved3_7 :1;
1157 __u8 additional_length;
1158 __u8 rsv5, rsv6, rsv7;
1159 __u8 vendor_id[8];
1160 __u8 product_id[16];
1161 __u8 revision_level[4];
1162 __u8 vendor_specific[20];
1163 __u8 reserved56t95[40];
1164
1165} idetape_inquiry_result_t;
1166
1167
1168
1169
1170typedef struct {
1171 unsigned reserved0_10 :2;
1172 unsigned bpu :1;
1173 unsigned reserved0_543 :3;
1174 unsigned eop :1;
1175 unsigned bop :1;
1176 u8 partition;
1177 u8 reserved2, reserved3;
1178 u32 first_block;
1179 u32 last_block;
1180 u8 reserved12;
1181 u8 blocks_in_buffer[3];
1182 u32 bytes_in_buffer;
1183} idetape_read_position_result_t;
1184
1185
1186
1187
1188
1189
1190#define IDETAPE_BLOCK_DESCRIPTOR 0
1191#define IDETAPE_CAPABILITIES_PAGE 0x2a
1192#define IDETAPE_PARAMTR_PAGE 0x2b
1193#define IDETAPE_BLOCK_SIZE_PAGE 0x30
1194#define IDETAPE_BUFFER_FILLING_PAGE 0x33
1195
1196
1197
1198
1199typedef struct {
1200 __u8 mode_data_length;
1201 __u8 medium_type;
1202 __u8 dsp;
1203 __u8 bdl;
1204#if 0
1205
1206 __u8 page_code :6;
1207 __u8 reserved0_6 :1;
1208 __u8 ps :1;
1209 __u8 page_length;
1210 __u8 reserved2;
1211 __u8 read32k :1;
1212 __u8 read32k5 :1;
1213 __u8 reserved3_23 :2;
1214 __u8 write32k :1;
1215 __u8 write32k5 :1;
1216 __u8 reserved3_6 :1;
1217 __u8 streaming :1;
1218#endif
1219} idetape_mode_parameter_header_t;
1220
1221
1222
1223
1224
1225
1226typedef struct {
1227 __u8 density_code;
1228 __u8 blocks[3];
1229 __u8 reserved4;
1230 __u8 length[3];
1231} idetape_parameter_block_descriptor_t;
1232
1233
1234
1235
1236typedef struct {
1237 unsigned page_code :6;
1238 unsigned reserved0 :1;
1239 unsigned ps :1;
1240 __u8 page_length;
1241 unsigned reserved2 :6;
1242 unsigned dcc :1;
1243 unsigned dce :1;
1244 unsigned reserved3 :5;
1245 unsigned red :2;
1246 unsigned dde :1;
1247 __u32 ca;
1248 __u32 da;
1249 __u8 reserved[4];
1250} idetape_data_compression_page_t;
1251
1252
1253
1254
1255typedef struct {
1256 unsigned page_code :6;
1257 unsigned reserved1_6 :1;
1258 unsigned ps :1;
1259 __u8 page_length;
1260 __u8 map;
1261 __u8 apd;
1262 unsigned reserved4_012 :3;
1263 unsigned psum :2;
1264 unsigned idp :1;
1265 unsigned sdp :1;
1266 unsigned fdp :1;
1267 __u8 mfr;
1268 __u8 reserved[2];
1269} idetape_medium_partition_page_t;
1270
1271
1272
1273
1274typedef struct {
1275 int dsc_rw_frequency;
1276 int dsc_media_access_frequency;
1277 int nr_stages;
1278} idetape_config_t;
1279
1280
1281
1282
1283
1284static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
1285
1286#define ide_tape_f(file) ((file)->private_data)
1287
1288static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
1289{
1290 struct ide_tape_obj *tape = NULL;
1291
1292 mutex_lock(&idetape_ref_mutex);
1293 tape = idetape_devs[i];
1294 if (tape)
1295 kref_get(&tape->kref);
1296 mutex_unlock(&idetape_ref_mutex);
1297 return tape;
1298}
1299
1300
1301
1302
1303
1304static int idetape_chrdev_release (struct inode *inode, struct file *filp);
1305static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
1306
1307
1308
1309
1310
1311static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
1312{
1313 while (bcount--)
1314 (void) HWIF(drive)->INB(IDE_DATA_REG);
1315}
1316
1317static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1318{
1319 struct idetape_bh *bh = pc->bh;
1320 int count;
1321
1322 while (bcount) {
1323#if IDETAPE_DEBUG_BUGS
1324 if (bh == NULL) {
1325 printk(KERN_ERR "ide-tape: bh == NULL in "
1326 "idetape_input_buffers\n");
1327 idetape_discard_data(drive, bcount);
1328 return;
1329 }
1330#endif
1331 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
1332 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
1333 bcount -= count;
1334 atomic_add(count, &bh->b_count);
1335 if (atomic_read(&bh->b_count) == bh->b_size) {
1336 bh = bh->b_reqnext;
1337 if (bh)
1338 atomic_set(&bh->b_count, 0);
1339 }
1340 }
1341 pc->bh = bh;
1342}
1343
1344static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1345{
1346 struct idetape_bh *bh = pc->bh;
1347 int count;
1348
1349 while (bcount) {
1350#if IDETAPE_DEBUG_BUGS
1351 if (bh == NULL) {
1352 printk(KERN_ERR "ide-tape: bh == NULL in "
1353 "idetape_output_buffers\n");
1354 return;
1355 }
1356#endif
1357 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
1358 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
1359 bcount -= count;
1360 pc->b_data += count;
1361 pc->b_count -= count;
1362 if (!pc->b_count) {
1363 pc->bh = bh = bh->b_reqnext;
1364 if (bh) {
1365 pc->b_data = bh->b_data;
1366 pc->b_count = atomic_read(&bh->b_count);
1367 }
1368 }
1369 }
1370}
1371
1372static void idetape_update_buffers (idetape_pc_t *pc)
1373{
1374 struct idetape_bh *bh = pc->bh;
1375 int count;
1376 unsigned int bcount = pc->actually_transferred;
1377
1378 if (test_bit(PC_WRITING, &pc->flags))
1379 return;
1380 while (bcount) {
1381#if IDETAPE_DEBUG_BUGS
1382 if (bh == NULL) {
1383 printk(KERN_ERR "ide-tape: bh == NULL in "
1384 "idetape_update_buffers\n");
1385 return;
1386 }
1387#endif
1388 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
1389 atomic_set(&bh->b_count, count);
1390 if (atomic_read(&bh->b_count) == bh->b_size)
1391 bh = bh->b_reqnext;
1392 bcount -= count;
1393 }
1394 pc->bh = bh;
1395}
1396
1397
1398
1399
1400
1401
1402
1403static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
1404{
1405 idetape_tape_t *tape = drive->driver_data;
1406
1407#if IDETAPE_DEBUG_LOG
1408 if (tape->debug_level >= 5)
1409 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
1410 tape->pc_stack_index);
1411#endif
1412 if (tape->pc_stack_index == IDETAPE_PC_STACK)
1413 tape->pc_stack_index=0;
1414 return (&tape->pc_stack[tape->pc_stack_index++]);
1415}
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430static struct request *idetape_next_rq_storage (ide_drive_t *drive)
1431{
1432 idetape_tape_t *tape = drive->driver_data;
1433
1434#if IDETAPE_DEBUG_LOG
1435 if (tape->debug_level >= 5)
1436 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
1437 tape->rq_stack_index);
1438#endif
1439 if (tape->rq_stack_index == IDETAPE_PC_STACK)
1440 tape->rq_stack_index=0;
1441 return (&tape->rq_stack[tape->rq_stack_index++]);
1442}
1443
1444
1445
1446
1447static void idetape_init_pc (idetape_pc_t *pc)
1448{
1449 memset(pc->c, 0, 12);
1450 pc->retries = 0;
1451 pc->flags = 0;
1452 pc->request_transfer = 0;
1453 pc->buffer = pc->pc_buffer;
1454 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
1455 pc->bh = NULL;
1456 pc->b_data = NULL;
1457}
1458
1459
1460
1461
1462
1463
1464static void idetape_analyze_error (ide_drive_t *drive, idetape_request_sense_result_t *result)
1465{
1466 idetape_tape_t *tape = drive->driver_data;
1467 idetape_pc_t *pc = tape->failed_pc;
1468
1469 tape->sense = *result;
1470 tape->sense_key = result->sense_key;
1471 tape->asc = result->asc;
1472 tape->ascq = result->ascq;
1473#if IDETAPE_DEBUG_LOG
1474
1475
1476
1477
1478 if (tape->debug_level >= 1)
1479 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
1480 "asc = %x, ascq = %x\n",
1481 pc->c[0], result->sense_key,
1482 result->asc, result->ascq);
1483#endif
1484
1485
1486
1487
1488 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
1489 pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl(get_unaligned(&result->information));
1490 idetape_update_buffers(pc);
1491 }
1492
1493
1494
1495
1496
1497
1498 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
1499 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
1500 if (result->sense_key == 5) {
1501
1502 pc->error = 0;
1503
1504 set_bit(PC_ABORT, &pc->flags);
1505 }
1506 }
1507 if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
1508 pc->error = IDETAPE_ERROR_FILEMARK;
1509 set_bit(PC_ABORT, &pc->flags);
1510 }
1511 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1512 if (result->eom ||
1513 (result->sense_key == 0xd && result->asc == 0x0 &&
1514 result->ascq == 0x2)) {
1515 pc->error = IDETAPE_ERROR_EOD;
1516 set_bit(PC_ABORT, &pc->flags);
1517 }
1518 }
1519 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1520 if (result->sense_key == 8) {
1521 pc->error = IDETAPE_ERROR_EOD;
1522 set_bit(PC_ABORT, &pc->flags);
1523 }
1524 if (!test_bit(PC_ABORT, &pc->flags) &&
1525 pc->actually_transferred)
1526 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1527 }
1528}
1529
1530
1531
1532
1533static void idetape_active_next_stage (ide_drive_t *drive)
1534{
1535 idetape_tape_t *tape = drive->driver_data;
1536 idetape_stage_t *stage = tape->next_stage;
1537 struct request *rq = &stage->rq;
1538
1539#if IDETAPE_DEBUG_LOG
1540 if (tape->debug_level >= 4)
1541 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1542#endif
1543#if IDETAPE_DEBUG_BUGS
1544 if (stage == NULL) {
1545 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1546 return;
1547 }
1548#endif
1549
1550 rq->rq_disk = tape->disk;
1551 rq->buffer = NULL;
1552 rq->special = (void *)stage->bh;
1553 tape->active_data_request = rq;
1554 tape->active_stage = stage;
1555 tape->next_stage = stage->next;
1556}
1557
1558
1559
1560
1561
1562
1563
1564
1565static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1566{
1567 idetape_tape_t *tape = drive->driver_data;
1568 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1569
1570#if IDETAPE_DEBUG_LOG
1571 if (tape->debug_level >= 4)
1572 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1573#endif
1574
1575 tape->max_stages += max(increase, 1);
1576 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
1577 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
1578}
1579
1580
1581
1582
1583
1584static void __idetape_kfree_stage (idetape_stage_t *stage)
1585{
1586 struct idetape_bh *prev_bh, *bh = stage->bh;
1587 int size;
1588
1589 while (bh != NULL) {
1590 if (bh->b_data != NULL) {
1591 size = (int) bh->b_size;
1592 while (size > 0) {
1593 free_page((unsigned long) bh->b_data);
1594 size -= PAGE_SIZE;
1595 bh->b_data += PAGE_SIZE;
1596 }
1597 }
1598 prev_bh = bh;
1599 bh = bh->b_reqnext;
1600 kfree(prev_bh);
1601 }
1602 kfree(stage);
1603}
1604
1605static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1606{
1607 __idetape_kfree_stage(stage);
1608}
1609
1610
1611
1612
1613
1614static void idetape_remove_stage_head (ide_drive_t *drive)
1615{
1616 idetape_tape_t *tape = drive->driver_data;
1617 idetape_stage_t *stage;
1618
1619#if IDETAPE_DEBUG_LOG
1620 if (tape->debug_level >= 4)
1621 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1622#endif
1623#if IDETAPE_DEBUG_BUGS
1624 if (tape->first_stage == NULL) {
1625 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1626 return;
1627 }
1628 if (tape->active_stage == tape->first_stage) {
1629 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1630 return;
1631 }
1632#endif
1633 stage = tape->first_stage;
1634 tape->first_stage = stage->next;
1635 idetape_kfree_stage(tape, stage);
1636 tape->nr_stages--;
1637 if (tape->first_stage == NULL) {
1638 tape->last_stage = NULL;
1639#if IDETAPE_DEBUG_BUGS
1640 if (tape->next_stage != NULL)
1641 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1642 if (tape->nr_stages)
1643 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1644#endif
1645 }
1646}
1647
1648
1649
1650
1651
1652static void idetape_abort_pipeline(ide_drive_t *drive,
1653 idetape_stage_t *new_last_stage)
1654{
1655 idetape_tape_t *tape = drive->driver_data;
1656 idetape_stage_t *stage = new_last_stage->next;
1657 idetape_stage_t *nstage;
1658
1659#if IDETAPE_DEBUG_LOG
1660 if (tape->debug_level >= 4)
1661 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1662#endif
1663 while (stage) {
1664 nstage = stage->next;
1665 idetape_kfree_stage(tape, stage);
1666 --tape->nr_stages;
1667 --tape->nr_pending_stages;
1668 stage = nstage;
1669 }
1670 if (new_last_stage)
1671 new_last_stage->next = NULL;
1672 tape->last_stage = new_last_stage;
1673 tape->next_stage = NULL;
1674}
1675
1676
1677
1678
1679
1680static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1681{
1682 struct request *rq = HWGROUP(drive)->rq;
1683 idetape_tape_t *tape = drive->driver_data;
1684 unsigned long flags;
1685 int error;
1686 int remove_stage = 0;
1687 idetape_stage_t *active_stage;
1688
1689#if IDETAPE_DEBUG_LOG
1690 if (tape->debug_level >= 4)
1691 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1692#endif
1693
1694 switch (uptodate) {
1695 case 0: error = IDETAPE_ERROR_GENERAL; break;
1696 case 1: error = 0; break;
1697 default: error = uptodate;
1698 }
1699 rq->errors = error;
1700 if (error)
1701 tape->failed_pc = NULL;
1702
1703 spin_lock_irqsave(&tape->spinlock, flags);
1704
1705
1706 if (tape->active_data_request == rq) {
1707 active_stage = tape->active_stage;
1708 tape->active_stage = NULL;
1709 tape->active_data_request = NULL;
1710 tape->nr_pending_stages--;
1711 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1712 remove_stage = 1;
1713 if (error) {
1714 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1715 if (error == IDETAPE_ERROR_EOD)
1716 idetape_abort_pipeline(drive, active_stage);
1717 }
1718 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1719 if (error == IDETAPE_ERROR_EOD) {
1720 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1721 idetape_abort_pipeline(drive, active_stage);
1722 }
1723 }
1724 if (tape->next_stage != NULL) {
1725 idetape_active_next_stage(drive);
1726
1727
1728
1729
1730 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1731 } else if (!error) {
1732 idetape_increase_max_pipeline_stages(drive);
1733 }
1734 }
1735 ide_end_drive_cmd(drive, 0, 0);
1736
1737
1738
1739
1740 if (remove_stage)
1741 idetape_remove_stage_head(drive);
1742 if (tape->active_data_request == NULL)
1743 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1744 spin_unlock_irqrestore(&tape->spinlock, flags);
1745 return 0;
1746}
1747
1748static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1749{
1750 idetape_tape_t *tape = drive->driver_data;
1751
1752#if IDETAPE_DEBUG_LOG
1753 if (tape->debug_level >= 4)
1754 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1755#endif
1756 if (!tape->pc->error) {
1757 idetape_analyze_error(drive, (idetape_request_sense_result_t *) tape->pc->buffer);
1758 idetape_end_request(drive, 1, 0);
1759 } else {
1760 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1761 idetape_end_request(drive, 0, 0);
1762 }
1763 return ide_stopped;
1764}
1765
1766static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1767{
1768 idetape_init_pc(pc);
1769 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1770 pc->c[4] = 20;
1771 pc->request_transfer = 20;
1772 pc->callback = &idetape_request_sense_callback;
1773}
1774
1775static void idetape_init_rq(struct request *rq, u8 cmd)
1776{
1777 memset(rq, 0, sizeof(*rq));
1778 rq->cmd_type = REQ_TYPE_SPECIAL;
1779 rq->cmd[0] = cmd;
1780}
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1802{
1803 struct ide_tape_obj *tape = drive->driver_data;
1804
1805 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1806 rq->buffer = (char *) pc;
1807 rq->rq_disk = tape->disk;
1808 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1809}
1810
1811
1812
1813
1814
1815
1816static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1817{
1818 idetape_tape_t *tape = drive->driver_data;
1819 idetape_pc_t *pc;
1820 struct request *rq;
1821 atapi_error_t error;
1822
1823 error.all = HWIF(drive)->INB(IDE_ERROR_REG);
1824 pc = idetape_next_pc_storage(drive);
1825 rq = idetape_next_rq_storage(drive);
1826 idetape_create_request_sense_cmd(pc);
1827 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1828 idetape_queue_pc_head(drive, pc, rq);
1829 return ide_stopped;
1830}
1831
1832
1833
1834
1835
1836
1837static void idetape_postpone_request (ide_drive_t *drive)
1838{
1839 idetape_tape_t *tape = drive->driver_data;
1840
1841#if IDETAPE_DEBUG_LOG
1842 if (tape->debug_level >= 4)
1843 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1844#endif
1845 tape->postponed_rq = HWGROUP(drive)->rq;
1846 ide_stall_queue(drive, tape->dsc_polling_frequency);
1847}
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1858{
1859 ide_hwif_t *hwif = drive->hwif;
1860 idetape_tape_t *tape = drive->driver_data;
1861 atapi_status_t status;
1862 atapi_bcount_t bcount;
1863 atapi_ireason_t ireason;
1864 idetape_pc_t *pc = tape->pc;
1865
1866 unsigned int temp;
1867#if SIMULATE_ERRORS
1868 static int error_sim_count = 0;
1869#endif
1870
1871#if IDETAPE_DEBUG_LOG
1872 if (tape->debug_level >= 4)
1873 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1874 "interrupt handler\n");
1875#endif
1876
1877
1878 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
1879
1880 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1881 if (HWIF(drive)->ide_dma_end(drive) || status.b.check) {
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903 set_bit(PC_DMA_ERROR, &pc->flags);
1904 } else {
1905 pc->actually_transferred = pc->request_transfer;
1906 idetape_update_buffers(pc);
1907 }
1908#if IDETAPE_DEBUG_LOG
1909 if (tape->debug_level >= 4)
1910 printk(KERN_INFO "ide-tape: DMA finished\n");
1911#endif
1912 }
1913
1914
1915 if (!status.b.drq) {
1916#if IDETAPE_DEBUG_LOG
1917 if (tape->debug_level >= 2)
1918 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1919#endif
1920 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1921
1922 local_irq_enable();
1923
1924#if SIMULATE_ERRORS
1925 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1926 pc->c[0] == IDETAPE_READ_CMD) &&
1927 (++error_sim_count % 100) == 0) {
1928 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1929 tape->name);
1930 status.b.check = 1;
1931 }
1932#endif
1933 if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1934 status.b.check = 0;
1935 if (status.b.check || test_bit(PC_DMA_ERROR, &pc->flags)) {
1936#if IDETAPE_DEBUG_LOG
1937 if (tape->debug_level >= 1)
1938 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1939 tape->name);
1940#endif
1941 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1942 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1943 return ide_do_reset(drive);
1944 }
1945#if IDETAPE_DEBUG_LOG
1946 if (tape->debug_level >= 1)
1947 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1948#endif
1949
1950 return idetape_retry_pc(drive);
1951 }
1952 pc->error = 0;
1953 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
1954 !status.b.dsc) {
1955
1956 tape->dsc_polling_start = jiffies;
1957 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1958 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1959
1960 idetape_postpone_request(drive);
1961 return ide_stopped;
1962 }
1963 if (tape->failed_pc == pc)
1964 tape->failed_pc = NULL;
1965
1966 return pc->callback(drive);
1967 }
1968 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1969 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1970 "interrupts in DMA mode\n");
1971 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
1972 ide_dma_off(drive);
1973 return ide_do_reset(drive);
1974 }
1975
1976 bcount.b.high = hwif->INB(IDE_BCOUNTH_REG);
1977 bcount.b.low = hwif->INB(IDE_BCOUNTL_REG);
1978
1979 ireason.all = hwif->INB(IDE_IREASON_REG);
1980
1981 if (ireason.b.cod) {
1982 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1983 return ide_do_reset(drive);
1984 }
1985 if (ireason.b.io == test_bit(PC_WRITING, &pc->flags)) {
1986
1987 printk(KERN_ERR "ide-tape: We wanted to %s, ",
1988 ireason.b.io ? "Write":"Read");
1989 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
1990 ireason.b.io ? "Read":"Write");
1991 return ide_do_reset(drive);
1992 }
1993 if (!test_bit(PC_WRITING, &pc->flags)) {
1994
1995 temp = pc->actually_transferred + bcount.all;
1996 if (temp > pc->request_transfer) {
1997 if (temp > pc->buffer_size) {
1998 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
1999 idetape_discard_data(drive, bcount.all);
2000 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2001 return ide_started;
2002 }
2003#if IDETAPE_DEBUG_LOG
2004 if (tape->debug_level >= 2)
2005 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
2006#endif
2007 }
2008 }
2009 if (test_bit(PC_WRITING, &pc->flags)) {
2010 if (pc->bh != NULL)
2011 idetape_output_buffers(drive, pc, bcount.all);
2012 else
2013
2014 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
2015 } else {
2016 if (pc->bh != NULL)
2017 idetape_input_buffers(drive, pc, bcount.all);
2018 else
2019
2020 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
2021 }
2022
2023 pc->actually_transferred += bcount.all;
2024 pc->current_position += bcount.all;
2025#if IDETAPE_DEBUG_LOG
2026 if (tape->debug_level >= 2)
2027 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes on that interrupt\n", pc->c[0], bcount.all);
2028#endif
2029
2030 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2031 return ide_started;
2032}
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
2077{
2078 ide_hwif_t *hwif = drive->hwif;
2079 idetape_tape_t *tape = drive->driver_data;
2080 idetape_pc_t *pc = tape->pc;
2081 atapi_ireason_t ireason;
2082 int retries = 100;
2083 ide_startstop_t startstop;
2084
2085 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
2086 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
2087 return startstop;
2088 }
2089 ireason.all = hwif->INB(IDE_IREASON_REG);
2090 while (retries-- && (!ireason.b.cod || ireason.b.io)) {
2091 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
2092 "a packet command, retrying\n");
2093 udelay(100);
2094 ireason.all = hwif->INB(IDE_IREASON_REG);
2095 if (retries == 0) {
2096 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
2097 "issuing a packet command, ignoring\n");
2098 ireason.b.cod = 1;
2099 ireason.b.io = 0;
2100 }
2101 }
2102 if (!ireason.b.cod || ireason.b.io) {
2103 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
2104 "a packet command\n");
2105 return ide_do_reset(drive);
2106 }
2107
2108 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2109#ifdef CONFIG_BLK_DEV_IDEDMA
2110
2111 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
2112 hwif->dma_start(drive);
2113#endif
2114
2115 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
2116 return ide_started;
2117}
2118
2119static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
2120{
2121 ide_hwif_t *hwif = drive->hwif;
2122 idetape_tape_t *tape = drive->driver_data;
2123 atapi_bcount_t bcount;
2124 int dma_ok = 0;
2125
2126#if IDETAPE_DEBUG_BUGS
2127 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
2128 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2129 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
2130 "Two request sense in serial were issued\n");
2131 }
2132#endif
2133
2134 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
2135 tape->failed_pc = pc;
2136
2137 tape->pc = pc;
2138
2139 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
2140 test_bit(PC_ABORT, &pc->flags)) {
2141
2142
2143
2144
2145
2146 if (!test_bit(PC_ABORT, &pc->flags)) {
2147 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
2148 tape->sense_key == 2 && tape->asc == 4 &&
2149 (tape->ascq == 1 || tape->ascq == 8))) {
2150 printk(KERN_ERR "ide-tape: %s: I/O error, "
2151 "pc = %2x, key = %2x, "
2152 "asc = %2x, ascq = %2x\n",
2153 tape->name, pc->c[0],
2154 tape->sense_key, tape->asc,
2155 tape->ascq);
2156 }
2157
2158 pc->error = IDETAPE_ERROR_GENERAL;
2159 }
2160 tape->failed_pc = NULL;
2161 return pc->callback(drive);
2162 }
2163#if IDETAPE_DEBUG_LOG
2164 if (tape->debug_level >= 2)
2165 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
2166#endif
2167
2168 pc->retries++;
2169
2170 pc->actually_transferred = 0;
2171 pc->current_position = pc->buffer;
2172
2173 bcount.all = pc->request_transfer;
2174
2175 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
2176 printk(KERN_WARNING "ide-tape: DMA disabled, "
2177 "reverting to PIO\n");
2178 ide_dma_off(drive);
2179 }
2180 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
2181 dma_ok = !hwif->dma_setup(drive);
2182
2183 if (IDE_CONTROL_REG)
2184 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
2185 hwif->OUTB(dma_ok ? 1 : 0, IDE_FEATURE_REG);
2186 hwif->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
2187 hwif->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
2188 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
2189 if (dma_ok)
2190 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
2191 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
2192 ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL);
2193 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
2194 return ide_started;
2195 } else {
2196 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
2197 return idetape_transfer_pc(drive);
2198 }
2199}
2200
2201
2202
2203
2204static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
2205{
2206 idetape_tape_t *tape = drive->driver_data;
2207
2208#if IDETAPE_DEBUG_LOG
2209 if (tape->debug_level >= 4)
2210 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
2211#endif
2212
2213 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
2214 return ide_stopped;
2215}
2216
2217
2218
2219
2220static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
2221{
2222 idetape_init_pc(pc);
2223 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
2224 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
2225 pc->c[1] = 8;
2226 pc->c[2] = page_code;
2227
2228
2229
2230
2231
2232
2233
2234 pc->c[3] = 0;
2235 pc->c[4] = 255;
2236 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
2237 pc->request_transfer = 12;
2238 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
2239 pc->request_transfer = 24;
2240 else
2241 pc->request_transfer = 50;
2242 pc->callback = &idetape_pc_callback;
2243}
2244
2245static void calculate_speeds(ide_drive_t *drive)
2246{
2247 idetape_tape_t *tape = drive->driver_data;
2248 int full = 125, empty = 75;
2249
2250 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
2251 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
2252 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
2253 tape->controlled_last_pipeline_head = tape->pipeline_head;
2254 tape->controlled_pipeline_head_time = jiffies;
2255 }
2256 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
2257 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
2258 else if (time_after(jiffies, tape->controlled_previous_head_time))
2259 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
2260
2261 if (tape->nr_pending_stages < tape->max_stages ) {
2262
2263 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
2264 tape->uncontrolled_pipeline_head_time = jiffies;
2265 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
2266 }
2267 } else {
2268 tape->uncontrolled_previous_head_time = jiffies;
2269 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
2270 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
2271 tape->uncontrolled_pipeline_head_time = jiffies;
2272 }
2273 }
2274 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
2275 if (tape->speed_control == 0) {
2276 tape->max_insert_speed = 5000;
2277 } else if (tape->speed_control == 1) {
2278 if (tape->nr_pending_stages >= tape->max_stages / 2)
2279 tape->max_insert_speed = tape->pipeline_head_speed +
2280 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
2281 else
2282 tape->max_insert_speed = 500 +
2283 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
2284 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
2285 tape->max_insert_speed = 5000;
2286 } else if (tape->speed_control == 2) {
2287 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
2288 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
2289 } else
2290 tape->max_insert_speed = tape->speed_control;
2291 tape->max_insert_speed = max(tape->max_insert_speed, 500);
2292}
2293
2294static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
2295{
2296 idetape_tape_t *tape = drive->driver_data;
2297 idetape_pc_t *pc = tape->pc;
2298 atapi_status_t status;
2299
2300 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
2301 if (status.b.dsc) {
2302 if (status.b.check) {
2303
2304 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
2305 printk(KERN_ERR "ide-tape: %s: I/O error, ",
2306 tape->name);
2307
2308 return idetape_retry_pc(drive);
2309 }
2310 pc->error = 0;
2311 if (tape->failed_pc == pc)
2312 tape->failed_pc = NULL;
2313 } else {
2314 pc->error = IDETAPE_ERROR_GENERAL;
2315 tape->failed_pc = NULL;
2316 }
2317 return pc->callback(drive);
2318}
2319
2320static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
2321{
2322 idetape_tape_t *tape = drive->driver_data;
2323 struct request *rq = HWGROUP(drive)->rq;
2324 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
2325
2326 tape->avg_size += blocks * tape->tape_block_size;
2327 tape->insert_size += blocks * tape->tape_block_size;
2328 if (tape->insert_size > 1024 * 1024)
2329 tape->measure_insert_time = 1;
2330 if (tape->measure_insert_time) {
2331 tape->measure_insert_time = 0;
2332 tape->insert_time = jiffies;
2333 tape->insert_size = 0;
2334 }
2335 if (time_after(jiffies, tape->insert_time))
2336 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2337 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
2338 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
2339 tape->avg_size = 0;
2340 tape->avg_time = jiffies;
2341 }
2342
2343#if IDETAPE_DEBUG_LOG
2344 if (tape->debug_level >= 4)
2345 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
2346#endif
2347
2348 tape->first_frame_position += blocks;
2349 rq->current_nr_sectors -= blocks;
2350
2351 if (!tape->pc->error)
2352 idetape_end_request(drive, 1, 0);
2353 else
2354 idetape_end_request(drive, tape->pc->error, 0);
2355 return ide_stopped;
2356}
2357
2358static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
2359{
2360 idetape_init_pc(pc);
2361 pc->c[0] = IDETAPE_READ_CMD;
2362 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
2363 pc->c[1] = 1;
2364 pc->callback = &idetape_rw_callback;
2365 pc->bh = bh;
2366 atomic_set(&bh->b_count, 0);
2367 pc->buffer = NULL;
2368 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2369 if (pc->request_transfer == tape->stage_size)
2370 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2371}
2372
2373static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
2374{
2375 int size = 32768;
2376 struct idetape_bh *p = bh;
2377
2378 idetape_init_pc(pc);
2379 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
2380 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
2381 pc->c[7] = size >> 8;
2382 pc->c[8] = size & 0xff;
2383 pc->callback = &idetape_pc_callback;
2384 pc->bh = bh;
2385 atomic_set(&bh->b_count, 0);
2386 pc->buffer = NULL;
2387 while (p) {
2388 atomic_set(&p->b_count, 0);
2389 p = p->b_reqnext;
2390 }
2391 pc->request_transfer = pc->buffer_size = size;
2392}
2393
2394static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
2395{
2396 idetape_init_pc(pc);
2397 pc->c[0] = IDETAPE_WRITE_CMD;
2398 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
2399 pc->c[1] = 1;
2400 pc->callback = &idetape_rw_callback;
2401 set_bit(PC_WRITING, &pc->flags);
2402 pc->bh = bh;
2403 pc->b_data = bh->b_data;
2404 pc->b_count = atomic_read(&bh->b_count);
2405 pc->buffer = NULL;
2406 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2407 if (pc->request_transfer == tape->stage_size)
2408 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2409}
2410
2411
2412
2413
2414static ide_startstop_t idetape_do_request(ide_drive_t *drive,
2415 struct request *rq, sector_t block)
2416{
2417 idetape_tape_t *tape = drive->driver_data;
2418 idetape_pc_t *pc = NULL;
2419 struct request *postponed_rq = tape->postponed_rq;
2420 atapi_status_t status;
2421
2422#if IDETAPE_DEBUG_LOG
2423#if 0
2424 if (tape->debug_level >= 5)
2425 printk(KERN_INFO "ide-tape: %d, "
2426 "dev: %s, cmd: %ld, errors: %d\n",
2427 rq->rq_disk->disk_name, rq->cmd[0], rq->errors);
2428#endif
2429 if (tape->debug_level >= 2)
2430 printk(KERN_INFO "ide-tape: sector: %ld, "
2431 "nr_sectors: %ld, current_nr_sectors: %d\n",
2432 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
2433#endif
2434
2435 if (!blk_special_request(rq)) {
2436
2437
2438
2439 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
2440 "request queue (%d)\n", drive->name, rq->cmd_type);
2441 ide_end_request(drive, 0, 0);
2442 return ide_stopped;
2443 }
2444
2445
2446
2447
2448 if (tape->failed_pc != NULL &&
2449 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2450 return idetape_issue_packet_command(drive, tape->failed_pc);
2451 }
2452#if IDETAPE_DEBUG_BUGS
2453 if (postponed_rq != NULL)
2454 if (rq != postponed_rq) {
2455 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
2456 "Two DSC requests were queued\n");
2457 idetape_end_request(drive, 0, 0);
2458 return ide_stopped;
2459 }
2460#endif
2461
2462 tape->postponed_rq = NULL;
2463
2464
2465
2466
2467
2468 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
2469
2470 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
2471 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2472
2473 if (drive->post_reset == 1) {
2474 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2475 drive->post_reset = 0;
2476 }
2477
2478 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2479 tape->measure_insert_time = 1;
2480 if (time_after(jiffies, tape->insert_time))
2481 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2482 calculate_speeds(drive);
2483 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
2484 !status.b.dsc) {
2485 if (postponed_rq == NULL) {
2486 tape->dsc_polling_start = jiffies;
2487 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2488 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2489 } else if (time_after(jiffies, tape->dsc_timeout)) {
2490 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
2491 tape->name);
2492 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2493 idetape_media_access_finished(drive);
2494 return ide_stopped;
2495 } else {
2496 return ide_do_reset(drive);
2497 }
2498 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
2499 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2500 idetape_postpone_request(drive);
2501 return ide_stopped;
2502 }
2503 if (rq->cmd[0] & REQ_IDETAPE_READ) {
2504 tape->buffer_head++;
2505#if USE_IOTRACE
2506 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2507#endif
2508 tape->postpone_cnt = 0;
2509 pc = idetape_next_pc_storage(drive);
2510 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2511 goto out;
2512 }
2513 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
2514 tape->buffer_head++;
2515#if USE_IOTRACE
2516 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
2517#endif
2518 tape->postpone_cnt = 0;
2519 pc = idetape_next_pc_storage(drive);
2520 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2521 goto out;
2522 }
2523 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
2524 tape->postpone_cnt = 0;
2525 pc = idetape_next_pc_storage(drive);
2526 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2527 goto out;
2528 }
2529 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
2530 pc = (idetape_pc_t *) rq->buffer;
2531 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
2532 rq->cmd[0] |= REQ_IDETAPE_PC2;
2533 goto out;
2534 }
2535 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2536 idetape_media_access_finished(drive);
2537 return ide_stopped;
2538 }
2539 BUG();
2540out:
2541 return idetape_issue_packet_command(drive, pc);
2542}
2543
2544
2545
2546
2547static inline int idetape_pipeline_active (idetape_tape_t *tape)
2548{
2549 int rc1, rc2;
2550
2551 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2552 rc2 = (tape->active_data_request != NULL);
2553 return rc1;
2554}
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2569{
2570 idetape_stage_t *stage;
2571 struct idetape_bh *prev_bh, *bh;
2572 int pages = tape->pages_per_stage;
2573 char *b_data = NULL;
2574
2575 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
2576 return NULL;
2577 stage->next = NULL;
2578
2579 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
2580 if (bh == NULL)
2581 goto abort;
2582 bh->b_reqnext = NULL;
2583 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2584 goto abort;
2585 if (clear)
2586 memset(bh->b_data, 0, PAGE_SIZE);
2587 bh->b_size = PAGE_SIZE;
2588 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2589
2590 while (--pages) {
2591 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2592 goto abort;
2593 if (clear)
2594 memset(b_data, 0, PAGE_SIZE);
2595 if (bh->b_data == b_data + PAGE_SIZE) {
2596 bh->b_size += PAGE_SIZE;
2597 bh->b_data -= PAGE_SIZE;
2598 if (full)
2599 atomic_add(PAGE_SIZE, &bh->b_count);
2600 continue;
2601 }
2602 if (b_data == bh->b_data + bh->b_size) {
2603 bh->b_size += PAGE_SIZE;
2604 if (full)
2605 atomic_add(PAGE_SIZE, &bh->b_count);
2606 continue;
2607 }
2608 prev_bh = bh;
2609 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
2610 free_page((unsigned long) b_data);
2611 goto abort;
2612 }
2613 bh->b_reqnext = NULL;
2614 bh->b_data = b_data;
2615 bh->b_size = PAGE_SIZE;
2616 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2617 prev_bh->b_reqnext = bh;
2618 }
2619 bh->b_size -= tape->excess_bh_size;
2620 if (full)
2621 atomic_sub(tape->excess_bh_size, &bh->b_count);
2622 return stage;
2623abort:
2624 __idetape_kfree_stage(stage);
2625 return NULL;
2626}
2627
2628static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2629{
2630 idetape_stage_t *cache_stage = tape->cache_stage;
2631
2632#if IDETAPE_DEBUG_LOG
2633 if (tape->debug_level >= 4)
2634 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2635#endif
2636
2637 if (tape->nr_stages >= tape->max_stages)
2638 return NULL;
2639 if (cache_stage != NULL) {
2640 tape->cache_stage = NULL;
2641 return cache_stage;
2642 }
2643 return __idetape_kmalloc_stage(tape, 0, 0);
2644}
2645
2646static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
2647{
2648 struct idetape_bh *bh = tape->bh;
2649 int count;
2650 int ret = 0;
2651
2652 while (n) {
2653#if IDETAPE_DEBUG_BUGS
2654 if (bh == NULL) {
2655 printk(KERN_ERR "ide-tape: bh == NULL in "
2656 "idetape_copy_stage_from_user\n");
2657 return 1;
2658 }
2659#endif
2660 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
2661 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2662 ret = 1;
2663 n -= count;
2664 atomic_add(count, &bh->b_count);
2665 buf += count;
2666 if (atomic_read(&bh->b_count) == bh->b_size) {
2667 bh = bh->b_reqnext;
2668 if (bh)
2669 atomic_set(&bh->b_count, 0);
2670 }
2671 }
2672 tape->bh = bh;
2673 return ret;
2674}
2675
2676static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
2677{
2678 struct idetape_bh *bh = tape->bh;
2679 int count;
2680 int ret = 0;
2681
2682 while (n) {
2683#if IDETAPE_DEBUG_BUGS
2684 if (bh == NULL) {
2685 printk(KERN_ERR "ide-tape: bh == NULL in "
2686 "idetape_copy_stage_to_user\n");
2687 return 1;
2688 }
2689#endif
2690 count = min(tape->b_count, n);
2691 if (copy_to_user(buf, tape->b_data, count))
2692 ret = 1;
2693 n -= count;
2694 tape->b_data += count;
2695 tape->b_count -= count;
2696 buf += count;
2697 if (!tape->b_count) {
2698 tape->bh = bh = bh->b_reqnext;
2699 if (bh) {
2700 tape->b_data = bh->b_data;
2701 tape->b_count = atomic_read(&bh->b_count);
2702 }
2703 }
2704 }
2705 return ret;
2706}
2707
2708static void idetape_init_merge_stage (idetape_tape_t *tape)
2709{
2710 struct idetape_bh *bh = tape->merge_stage->bh;
2711
2712 tape->bh = bh;
2713 if (tape->chrdev_direction == idetape_direction_write)
2714 atomic_set(&bh->b_count, 0);
2715 else {
2716 tape->b_data = bh->b_data;
2717 tape->b_count = atomic_read(&bh->b_count);
2718 }
2719}
2720
2721static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2722{
2723 struct idetape_bh *tmp;
2724
2725 tmp = stage->bh;
2726 stage->bh = tape->merge_stage->bh;
2727 tape->merge_stage->bh = tmp;
2728 idetape_init_merge_stage(tape);
2729}
2730
2731
2732
2733
2734static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2735{
2736 idetape_tape_t *tape = drive->driver_data;
2737 unsigned long flags;
2738
2739#if IDETAPE_DEBUG_LOG
2740 if (tape->debug_level >= 4)
2741 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2742#endif
2743 spin_lock_irqsave(&tape->spinlock, flags);
2744 stage->next = NULL;
2745 if (tape->last_stage != NULL)
2746 tape->last_stage->next=stage;
2747 else
2748 tape->first_stage = tape->next_stage=stage;
2749 tape->last_stage = stage;
2750 if (tape->next_stage == NULL)
2751 tape->next_stage = tape->last_stage;
2752 tape->nr_stages++;
2753 tape->nr_pending_stages++;
2754 spin_unlock_irqrestore(&tape->spinlock, flags);
2755}
2756
2757
2758
2759
2760
2761
2762
2763
2764static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2765{
2766 DECLARE_COMPLETION_ONSTACK(wait);
2767 idetape_tape_t *tape = drive->driver_data;
2768
2769#if IDETAPE_DEBUG_BUGS
2770 if (rq == NULL || !blk_special_request(rq)) {
2771 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2772 return;
2773 }
2774#endif
2775 rq->end_io_data = &wait;
2776 rq->end_io = blk_end_sync_rq;
2777 spin_unlock_irq(&tape->spinlock);
2778 wait_for_completion(&wait);
2779
2780 spin_lock_irq(&tape->spinlock);
2781}
2782
2783static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2784{
2785 idetape_tape_t *tape = drive->driver_data;
2786 idetape_read_position_result_t *result;
2787
2788#if IDETAPE_DEBUG_LOG
2789 if (tape->debug_level >= 4)
2790 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2791#endif
2792
2793 if (!tape->pc->error) {
2794 result = (idetape_read_position_result_t *) tape->pc->buffer;
2795#if IDETAPE_DEBUG_LOG
2796 if (tape->debug_level >= 2)
2797 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2798 if (tape->debug_level >= 2)
2799 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2800#endif
2801 if (result->bpu) {
2802 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2803 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2804 idetape_end_request(drive, 0, 0);
2805 } else {
2806#if IDETAPE_DEBUG_LOG
2807 if (tape->debug_level >= 2)
2808 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2809#endif
2810 tape->partition = result->partition;
2811 tape->first_frame_position = ntohl(result->first_block);
2812 tape->last_frame_position = ntohl(result->last_block);
2813 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2814 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2815 idetape_end_request(drive, 1, 0);
2816 }
2817 } else {
2818 idetape_end_request(drive, 0, 0);
2819 }
2820 return ide_stopped;
2821}
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2832{
2833 idetape_init_pc(pc);
2834 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2835 pc->c[4] = write_filemark;
2836 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2837 pc->callback = &idetape_pc_callback;
2838}
2839
2840static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2841{
2842 idetape_init_pc(pc);
2843 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2844 pc->callback = &idetape_pc_callback;
2845}
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2868{
2869 struct ide_tape_obj *tape = drive->driver_data;
2870 struct request rq;
2871
2872 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2873 rq.buffer = (char *) pc;
2874 rq.rq_disk = tape->disk;
2875 return ide_do_drive_cmd(drive, &rq, ide_wait);
2876}
2877
2878static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2879{
2880 idetape_init_pc(pc);
2881 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2882 pc->c[4] = cmd;
2883 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2884 pc->callback = &idetape_pc_callback;
2885}
2886
2887static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2888{
2889 idetape_tape_t *tape = drive->driver_data;
2890 idetape_pc_t pc;
2891 int load_attempted = 0;
2892
2893
2894
2895
2896 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2897 timeout += jiffies;
2898 while (time_before(jiffies, timeout)) {
2899 idetape_create_test_unit_ready_cmd(&pc);
2900 if (!__idetape_queue_pc_tail(drive, &pc))
2901 return 0;
2902 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2903 || (tape->asc == 0x3A)) {
2904 if (load_attempted)
2905 return -ENOMEDIUM;
2906 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2907 __idetape_queue_pc_tail(drive, &pc);
2908 load_attempted = 1;
2909
2910 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2911 (tape->ascq == 1 || tape->ascq == 8)))
2912 return -EIO;
2913 msleep(100);
2914 }
2915 return -EIO;
2916}
2917
2918static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2919{
2920 return __idetape_queue_pc_tail(drive, pc);
2921}
2922
2923static int idetape_flush_tape_buffers (ide_drive_t *drive)
2924{
2925 idetape_pc_t pc;
2926 int rc;
2927
2928 idetape_create_write_filemark_cmd(drive, &pc, 0);
2929 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2930 return rc;
2931 idetape_wait_ready(drive, 60 * 5 * HZ);
2932 return 0;
2933}
2934
2935static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2936{
2937 idetape_init_pc(pc);
2938 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2939 pc->request_transfer = 20;
2940 pc->callback = &idetape_read_position_callback;
2941}
2942
2943static int idetape_read_position (ide_drive_t *drive)
2944{
2945 idetape_tape_t *tape = drive->driver_data;
2946 idetape_pc_t pc;
2947 int position;
2948
2949#if IDETAPE_DEBUG_LOG
2950 if (tape->debug_level >= 4)
2951 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2952#endif
2953
2954 idetape_create_read_position_cmd(&pc);
2955 if (idetape_queue_pc_tail(drive, &pc))
2956 return -1;
2957 position = tape->first_frame_position;
2958 return position;
2959}
2960
2961static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2962{
2963 idetape_init_pc(pc);
2964 pc->c[0] = IDETAPE_LOCATE_CMD;
2965 pc->c[1] = 2;
2966 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2967 pc->c[8] = partition;
2968 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2969 pc->callback = &idetape_pc_callback;
2970}
2971
2972static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2973{
2974 idetape_tape_t *tape = drive->driver_data;
2975
2976 if (!tape->capabilities.lock)
2977 return 0;
2978
2979 idetape_init_pc(pc);
2980 pc->c[0] = IDETAPE_PREVENT_CMD;
2981 pc->c[4] = prevent;
2982 pc->callback = &idetape_pc_callback;
2983 return 1;
2984}
2985
2986static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2987{
2988 idetape_tape_t *tape = drive->driver_data;
2989 unsigned long flags;
2990 int cnt;
2991
2992 if (tape->chrdev_direction != idetape_direction_read)
2993 return 0;
2994
2995
2996 cnt = tape->merge_stage_size / tape->tape_block_size;
2997 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2998 ++cnt;
2999 tape->merge_stage_size = 0;
3000 if (tape->merge_stage != NULL) {
3001 __idetape_kfree_stage(tape->merge_stage);
3002 tape->merge_stage = NULL;
3003 }
3004
3005
3006 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3007 tape->chrdev_direction = idetape_direction_none;
3008
3009
3010 if (tape->first_stage == NULL)
3011 return 0;
3012
3013 spin_lock_irqsave(&tape->spinlock, flags);
3014 tape->next_stage = NULL;
3015 if (idetape_pipeline_active(tape))
3016 idetape_wait_for_request(drive, tape->active_data_request);
3017 spin_unlock_irqrestore(&tape->spinlock, flags);
3018
3019 while (tape->first_stage != NULL) {
3020 struct request *rq_ptr = &tape->first_stage->rq;
3021
3022 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
3023 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3024 ++cnt;
3025 idetape_remove_stage_head(drive);
3026 }
3027 tape->nr_pending_stages = 0;
3028 tape->max_stages = tape->min_pipeline;
3029 return cnt;
3030}
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
3042{
3043 idetape_tape_t *tape = drive->driver_data;
3044 int retval;
3045 idetape_pc_t pc;
3046
3047 if (tape->chrdev_direction == idetape_direction_read)
3048 __idetape_discard_read_pipeline(drive);
3049 idetape_wait_ready(drive, 60 * 5 * HZ);
3050 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
3051 retval = idetape_queue_pc_tail(drive, &pc);
3052 if (retval)
3053 return (retval);
3054
3055 idetape_create_read_position_cmd(&pc);
3056 return (idetape_queue_pc_tail(drive, &pc));
3057}
3058
3059static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
3060{
3061 idetape_tape_t *tape = drive->driver_data;
3062 int cnt;
3063 int seek, position;
3064
3065 cnt = __idetape_discard_read_pipeline(drive);
3066 if (restore_position) {
3067 position = idetape_read_position(drive);
3068 seek = position > cnt ? position - cnt : 0;
3069 if (idetape_position_tape(drive, seek, 0, 0)) {
3070 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
3071 return;
3072 }
3073 }
3074}
3075
3076
3077
3078
3079
3080static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
3081{
3082 idetape_tape_t *tape = drive->driver_data;
3083 struct request rq;
3084
3085#if IDETAPE_DEBUG_LOG
3086 if (tape->debug_level >= 2)
3087 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
3088#endif
3089#if IDETAPE_DEBUG_BUGS
3090 if (idetape_pipeline_active(tape)) {
3091 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
3092 return (0);
3093 }
3094#endif
3095
3096 idetape_init_rq(&rq, cmd);
3097 rq.rq_disk = tape->disk;
3098 rq.special = (void *)bh;
3099 rq.sector = tape->first_frame_position;
3100 rq.nr_sectors = rq.current_nr_sectors = blocks;
3101 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
3102
3103 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
3104 return 0;
3105
3106 if (tape->merge_stage)
3107 idetape_init_merge_stage(tape);
3108 if (rq.errors == IDETAPE_ERROR_GENERAL)
3109 return -EIO;
3110 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
3111}
3112
3113
3114
3115
3116
3117static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
3118{
3119 idetape_tape_t *tape = drive->driver_data;
3120
3121 if (tape->next_stage == NULL)
3122 return;
3123 if (!idetape_pipeline_active(tape)) {
3124 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
3125 idetape_active_next_stage(drive);
3126 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
3127 }
3128}
3129
3130static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
3131{
3132 idetape_init_pc(pc);
3133 pc->c[0] = IDETAPE_INQUIRY_CMD;
3134 pc->c[4] = pc->request_transfer = 254;
3135 pc->callback = &idetape_pc_callback;
3136}
3137
3138static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
3139{
3140 idetape_init_pc(pc);
3141 pc->c[0] = IDETAPE_REWIND_CMD;
3142 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3143 pc->callback = &idetape_pc_callback;
3144}
3145
3146#if 0
3147static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
3148{
3149 idetape_init_pc(pc);
3150 set_bit(PC_WRITING, &pc->flags);
3151 pc->c[0] = IDETAPE_MODE_SELECT_CMD;
3152 pc->c[1] = 0x10;
3153 put_unaligned(htons(length), (unsigned short *) &pc->c[3]);
3154 pc->request_transfer = 255;
3155 pc->callback = &idetape_pc_callback;
3156}
3157#endif
3158
3159static void idetape_create_erase_cmd (idetape_pc_t *pc)
3160{
3161 idetape_init_pc(pc);
3162 pc->c[0] = IDETAPE_ERASE_CMD;
3163 pc->c[1] = 1;
3164 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3165 pc->callback = &idetape_pc_callback;
3166}
3167
3168static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
3169{
3170 idetape_init_pc(pc);
3171 pc->c[0] = IDETAPE_SPACE_CMD;
3172 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
3173 pc->c[1] = cmd;
3174 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3175 pc->callback = &idetape_pc_callback;
3176}
3177
3178static void idetape_wait_first_stage (ide_drive_t *drive)
3179{
3180 idetape_tape_t *tape = drive->driver_data;
3181 unsigned long flags;
3182
3183 if (tape->first_stage == NULL)
3184 return;
3185 spin_lock_irqsave(&tape->spinlock, flags);
3186 if (tape->active_stage == tape->first_stage)
3187 idetape_wait_for_request(drive, tape->active_data_request);
3188 spin_unlock_irqrestore(&tape->spinlock, flags);
3189}
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
3203{
3204 idetape_tape_t *tape = drive->driver_data;
3205 idetape_stage_t *new_stage;
3206 unsigned long flags;
3207 struct request *rq;
3208
3209#if IDETAPE_DEBUG_LOG
3210 if (tape->debug_level >= 3)
3211 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
3212#endif
3213
3214
3215
3216
3217
3218 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
3219 spin_lock_irqsave(&tape->spinlock, flags);
3220 if (idetape_pipeline_active(tape)) {
3221 idetape_wait_for_request(drive, tape->active_data_request);
3222 spin_unlock_irqrestore(&tape->spinlock, flags);
3223 } else {
3224 spin_unlock_irqrestore(&tape->spinlock, flags);
3225 idetape_insert_pipeline_into_queue(drive);
3226 if (idetape_pipeline_active(tape))
3227 continue;
3228
3229
3230
3231
3232 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3233 }
3234 }
3235 rq = &new_stage->rq;
3236 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
3237
3238 rq->sector = tape->first_frame_position;
3239 rq->nr_sectors = rq->current_nr_sectors = blocks;
3240
3241 idetape_switch_buffers(tape, new_stage);
3242 idetape_add_stage_tail(drive, new_stage);
3243 tape->pipeline_head++;
3244#if USE_IOTRACE
3245 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
3246#endif
3247 calculate_speeds(drive);
3248
3249
3250
3251
3252
3253
3254
3255
3256 if (!idetape_pipeline_active(tape)) {
3257 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
3258 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
3259 tape->measure_insert_time = 1;
3260 tape->insert_time = jiffies;
3261 tape->insert_size = 0;
3262 tape->insert_speed = 0;
3263 idetape_insert_pipeline_into_queue(drive);
3264 }
3265 }
3266 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
3267
3268 return -EIO;
3269 return blocks;
3270}
3271
3272
3273
3274
3275
3276static void idetape_wait_for_pipeline (ide_drive_t *drive)
3277{
3278 idetape_tape_t *tape = drive->driver_data;
3279 unsigned long flags;
3280
3281 while (tape->next_stage || idetape_pipeline_active(tape)) {
3282 idetape_insert_pipeline_into_queue(drive);
3283 spin_lock_irqsave(&tape->spinlock, flags);
3284 if (idetape_pipeline_active(tape))
3285 idetape_wait_for_request(drive, tape->active_data_request);
3286 spin_unlock_irqrestore(&tape->spinlock, flags);
3287 }
3288}
3289
3290static void idetape_empty_write_pipeline (ide_drive_t *drive)
3291{
3292 idetape_tape_t *tape = drive->driver_data;
3293 int blocks, min;
3294 struct idetape_bh *bh;
3295
3296#if IDETAPE_DEBUG_BUGS
3297 if (tape->chrdev_direction != idetape_direction_write) {
3298 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
3299 return;
3300 }
3301 if (tape->merge_stage_size > tape->stage_size) {
3302 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
3303 tape->merge_stage_size = tape->stage_size;
3304 }
3305#endif
3306 if (tape->merge_stage_size) {
3307 blocks = tape->merge_stage_size / tape->tape_block_size;
3308 if (tape->merge_stage_size % tape->tape_block_size) {
3309 unsigned int i;
3310
3311 blocks++;
3312 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
3313 bh = tape->bh->b_reqnext;
3314 while (bh) {
3315 atomic_set(&bh->b_count, 0);
3316 bh = bh->b_reqnext;
3317 }
3318 bh = tape->bh;
3319 while (i) {
3320 if (bh == NULL) {
3321
3322 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
3323 break;
3324 }
3325 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
3326 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
3327 atomic_add(min, &bh->b_count);
3328 i -= min;
3329 bh = bh->b_reqnext;
3330 }
3331 }
3332 (void) idetape_add_chrdev_write_request(drive, blocks);
3333 tape->merge_stage_size = 0;
3334 }
3335 idetape_wait_for_pipeline(drive);
3336 if (tape->merge_stage != NULL) {
3337 __idetape_kfree_stage(tape->merge_stage);
3338 tape->merge_stage = NULL;
3339 }
3340 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3341 tape->chrdev_direction = idetape_direction_none;
3342
3343
3344
3345
3346
3347
3348
3349 tape->max_stages = tape->min_pipeline;
3350#if IDETAPE_DEBUG_BUGS
3351 if (tape->first_stage != NULL ||
3352 tape->next_stage != NULL ||
3353 tape->last_stage != NULL ||
3354 tape->nr_stages != 0) {
3355 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
3356 "first_stage %p, next_stage %p, "
3357 "last_stage %p, nr_stages %d\n",
3358 tape->first_stage, tape->next_stage,
3359 tape->last_stage, tape->nr_stages);
3360 }
3361#endif
3362}
3363
3364static void idetape_restart_speed_control (ide_drive_t *drive)
3365{
3366 idetape_tape_t *tape = drive->driver_data;
3367
3368 tape->restart_speed_control_req = 0;
3369 tape->pipeline_head = 0;
3370 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
3371 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
3372 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
3373 tape->uncontrolled_pipeline_head_speed = 0;
3374 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
3375 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
3376}
3377
3378static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
3379{
3380 idetape_tape_t *tape = drive->driver_data;
3381 idetape_stage_t *new_stage;
3382 struct request rq;
3383 int bytes_read;
3384 int blocks = tape->capabilities.ctl;
3385
3386
3387 if (tape->chrdev_direction != idetape_direction_read) {
3388 if (tape->chrdev_direction == idetape_direction_write) {
3389 idetape_empty_write_pipeline(drive);
3390 idetape_flush_tape_buffers(drive);
3391 }
3392#if IDETAPE_DEBUG_BUGS
3393 if (tape->merge_stage || tape->merge_stage_size) {
3394 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
3395 tape->merge_stage_size = 0;
3396 }
3397#endif
3398 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3399 return -ENOMEM;
3400 tape->chrdev_direction = idetape_direction_read;
3401
3402
3403
3404
3405
3406
3407
3408
3409 if (drive->dsc_overlap) {
3410 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
3411 if (bytes_read < 0) {
3412 __idetape_kfree_stage(tape->merge_stage);
3413 tape->merge_stage = NULL;
3414 tape->chrdev_direction = idetape_direction_none;
3415 return bytes_read;
3416 }
3417 }
3418 }
3419 if (tape->restart_speed_control_req)
3420 idetape_restart_speed_control(drive);
3421 idetape_init_rq(&rq, REQ_IDETAPE_READ);
3422 rq.sector = tape->first_frame_position;
3423 rq.nr_sectors = rq.current_nr_sectors = blocks;
3424 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
3425 tape->nr_stages < max_stages) {
3426 new_stage = idetape_kmalloc_stage(tape);
3427 while (new_stage != NULL) {
3428 new_stage->rq = rq;
3429 idetape_add_stage_tail(drive, new_stage);
3430 if (tape->nr_stages >= max_stages)
3431 break;
3432 new_stage = idetape_kmalloc_stage(tape);
3433 }
3434 }
3435 if (!idetape_pipeline_active(tape)) {
3436 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
3437 tape->measure_insert_time = 1;
3438 tape->insert_time = jiffies;
3439 tape->insert_size = 0;
3440 tape->insert_speed = 0;
3441 idetape_insert_pipeline_into_queue(drive);
3442 }
3443 }
3444 return 0;
3445}
3446
3447
3448
3449
3450
3451
3452static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
3453{
3454 idetape_tape_t *tape = drive->driver_data;
3455 unsigned long flags;
3456 struct request *rq_ptr;
3457 int bytes_read;
3458
3459#if IDETAPE_DEBUG_LOG
3460 if (tape->debug_level >= 4)
3461 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
3462#endif
3463
3464
3465
3466
3467 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
3468 return 0;
3469
3470
3471
3472
3473
3474 idetape_initiate_read(drive, tape->max_stages);
3475 if (tape->first_stage == NULL) {
3476 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
3477 return 0;
3478 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
3479 }
3480 idetape_wait_first_stage(drive);
3481 rq_ptr = &tape->first_stage->rq;
3482 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
3483 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
3484
3485
3486 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
3487 return 0;
3488 else {
3489 idetape_switch_buffers(tape, tape->first_stage);
3490 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3491 set_bit(IDETAPE_FILEMARK, &tape->flags);
3492 spin_lock_irqsave(&tape->spinlock, flags);
3493 idetape_remove_stage_head(drive);
3494 spin_unlock_irqrestore(&tape->spinlock, flags);
3495 tape->pipeline_head++;
3496#if USE_IOTRACE
3497 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
3498#endif
3499 calculate_speeds(drive);
3500 }
3501#if IDETAPE_DEBUG_BUGS
3502 if (bytes_read > blocks * tape->tape_block_size) {
3503 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
3504 bytes_read = blocks * tape->tape_block_size;
3505 }
3506#endif
3507 return (bytes_read);
3508}
3509
3510static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
3511{
3512 idetape_tape_t *tape = drive->driver_data;
3513 struct idetape_bh *bh;
3514 int blocks;
3515
3516 while (bcount) {
3517 unsigned int count;
3518
3519 bh = tape->merge_stage->bh;
3520 count = min(tape->stage_size, bcount);
3521 bcount -= count;
3522 blocks = count / tape->tape_block_size;
3523 while (count) {
3524 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
3525 memset(bh->b_data, 0, atomic_read(&bh->b_count));
3526 count -= atomic_read(&bh->b_count);
3527 bh = bh->b_reqnext;
3528 }
3529 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3530 }
3531}
3532
3533static int idetape_pipeline_size (ide_drive_t *drive)
3534{
3535 idetape_tape_t *tape = drive->driver_data;
3536 idetape_stage_t *stage;
3537 struct request *rq;
3538 int size = 0;
3539
3540 idetape_wait_for_pipeline(drive);
3541 stage = tape->first_stage;
3542 while (stage != NULL) {
3543 rq = &stage->rq;
3544 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
3545 if (rq->errors == IDETAPE_ERROR_FILEMARK)
3546 size += tape->tape_block_size;
3547 stage = stage->next;
3548 }
3549 size += tape->merge_stage_size;
3550 return size;
3551}
3552
3553
3554
3555
3556
3557
3558static int idetape_rewind_tape (ide_drive_t *drive)
3559{
3560 int retval;
3561 idetape_pc_t pc;
3562#if IDETAPE_DEBUG_LOG
3563 idetape_tape_t *tape = drive->driver_data;
3564 if (tape->debug_level >= 2)
3565 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
3566#endif
3567
3568 idetape_create_rewind_cmd(drive, &pc);
3569 retval = idetape_queue_pc_tail(drive, &pc);
3570 if (retval)
3571 return retval;
3572
3573 idetape_create_read_position_cmd(&pc);
3574 retval = idetape_queue_pc_tail(drive, &pc);
3575 if (retval)
3576 return retval;
3577 return 0;
3578}
3579
3580
3581
3582
3583
3584
3585
3586
3587static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
3588{
3589 idetape_tape_t *tape = drive->driver_data;
3590 idetape_config_t config;
3591 void __user *argp = (void __user *)arg;
3592
3593#if IDETAPE_DEBUG_LOG
3594 if (tape->debug_level >= 4)
3595 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
3596#endif
3597 switch (cmd) {
3598 case 0x0340:
3599 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
3600 return -EFAULT;
3601 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
3602 tape->max_stages = config.nr_stages;
3603 break;
3604 case 0x0350:
3605 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
3606 config.nr_stages = tape->max_stages;
3607 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
3608 return -EFAULT;
3609 break;
3610 default:
3611 return -EIO;
3612 }
3613 return 0;
3614}
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
3626{
3627 idetape_tape_t *tape = drive->driver_data;
3628 idetape_pc_t pc;
3629 unsigned long flags;
3630 int retval,count=0;
3631
3632 if (mt_count == 0)
3633 return 0;
3634 if (MTBSF == mt_op || MTBSFM == mt_op) {
3635 if (!tape->capabilities.sprev)
3636 return -EIO;
3637 mt_count = - mt_count;
3638 }
3639
3640 if (tape->chrdev_direction == idetape_direction_read) {
3641
3642
3643
3644
3645 tape->merge_stage_size = 0;
3646 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3647 ++count;
3648 while (tape->first_stage != NULL) {
3649 if (count == mt_count) {
3650 if (mt_op == MTFSFM)
3651 set_bit(IDETAPE_FILEMARK, &tape->flags);
3652 return 0;
3653 }
3654 spin_lock_irqsave(&tape->spinlock, flags);
3655 if (tape->first_stage == tape->active_stage) {
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665 tape->next_stage = NULL;
3666 spin_unlock_irqrestore(&tape->spinlock, flags);
3667 idetape_wait_first_stage(drive);
3668 tape->next_stage = tape->first_stage->next;
3669 } else
3670 spin_unlock_irqrestore(&tape->spinlock, flags);
3671 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3672 ++count;
3673 idetape_remove_stage_head(drive);
3674 }
3675 idetape_discard_read_pipeline(drive, 0);
3676 }
3677
3678
3679
3680
3681
3682 switch (mt_op) {
3683 case MTFSF:
3684 case MTBSF:
3685 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3686 return (idetape_queue_pc_tail(drive, &pc));
3687 case MTFSFM:
3688 case MTBSFM:
3689 if (!tape->capabilities.sprev)
3690 return (-EIO);
3691 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3692 if (retval) return (retval);
3693 count = (MTBSFM == mt_op ? 1 : -1);
3694 return (idetape_space_over_filemarks(drive, MTFSF, count));
3695 default:
3696 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3697 return (-EIO);
3698 }
3699}
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3720 size_t count, loff_t *ppos)
3721{
3722 struct ide_tape_obj *tape = ide_tape_f(file);
3723 ide_drive_t *drive = tape->drive;
3724 ssize_t bytes_read,temp, actually_read = 0, rc;
3725 ssize_t ret = 0;
3726
3727#if IDETAPE_DEBUG_LOG
3728 if (tape->debug_level >= 3)
3729 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3730#endif
3731
3732 if (tape->chrdev_direction != idetape_direction_read) {
3733 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3734 if (count > tape->tape_block_size &&
3735 (count % tape->tape_block_size) == 0)
3736 tape->user_bs_factor = count / tape->tape_block_size;
3737 }
3738 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3739 return rc;
3740 if (count == 0)
3741 return (0);
3742 if (tape->merge_stage_size) {
3743 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
3744 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
3745 ret = -EFAULT;
3746 buf += actually_read;
3747 tape->merge_stage_size -= actually_read;
3748 count -= actually_read;
3749 }
3750 while (count >= tape->stage_size) {
3751 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3752 if (bytes_read <= 0)
3753 goto finish;
3754 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
3755 ret = -EFAULT;
3756 buf += bytes_read;
3757 count -= bytes_read;
3758 actually_read += bytes_read;
3759 }
3760 if (count) {
3761 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3762 if (bytes_read <= 0)
3763 goto finish;
3764 temp = min((unsigned long)count, (unsigned long)bytes_read);
3765 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
3766 ret = -EFAULT;
3767 actually_read += temp;
3768 tape->merge_stage_size = bytes_read-temp;
3769 }
3770finish:
3771 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3772#if IDETAPE_DEBUG_LOG
3773 if (tape->debug_level >= 2)
3774 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3775#endif
3776 idetape_space_over_filemarks(drive, MTFSF, 1);
3777 return 0;
3778 }
3779
3780 return (ret) ? ret : actually_read;
3781}
3782
3783static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3784 size_t count, loff_t *ppos)
3785{
3786 struct ide_tape_obj *tape = ide_tape_f(file);
3787 ide_drive_t *drive = tape->drive;
3788 ssize_t actually_written = 0;
3789 ssize_t ret = 0;
3790
3791
3792 if (tape->write_prot)
3793 return -EACCES;
3794
3795#if IDETAPE_DEBUG_LOG
3796 if (tape->debug_level >= 3)
3797 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3798 "count %Zd\n", count);
3799#endif
3800
3801
3802 if (tape->chrdev_direction != idetape_direction_write) {
3803 if (tape->chrdev_direction == idetape_direction_read)
3804 idetape_discard_read_pipeline(drive, 1);
3805#if IDETAPE_DEBUG_BUGS
3806 if (tape->merge_stage || tape->merge_stage_size) {
3807 printk(KERN_ERR "ide-tape: merge_stage_size "
3808 "should be 0 now\n");
3809 tape->merge_stage_size = 0;
3810 }
3811#endif
3812 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3813 return -ENOMEM;
3814 tape->chrdev_direction = idetape_direction_write;
3815 idetape_init_merge_stage(tape);
3816
3817
3818
3819
3820
3821
3822
3823
3824 if (drive->dsc_overlap) {
3825 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
3826 if (retval < 0) {
3827 __idetape_kfree_stage(tape->merge_stage);
3828 tape->merge_stage = NULL;
3829 tape->chrdev_direction = idetape_direction_none;
3830 return retval;
3831 }
3832 }
3833 }
3834 if (count == 0)
3835 return (0);
3836 if (tape->restart_speed_control_req)
3837 idetape_restart_speed_control(drive);
3838 if (tape->merge_stage_size) {
3839#if IDETAPE_DEBUG_BUGS
3840 if (tape->merge_stage_size >= tape->stage_size) {
3841 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3842 tape->merge_stage_size = 0;
3843 }
3844#endif
3845 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
3846 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3847 ret = -EFAULT;
3848 buf += actually_written;
3849 tape->merge_stage_size += actually_written;
3850 count -= actually_written;
3851
3852 if (tape->merge_stage_size == tape->stage_size) {
3853 ssize_t retval;
3854 tape->merge_stage_size = 0;
3855 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3856 if (retval <= 0)
3857 return (retval);
3858 }
3859 }
3860 while (count >= tape->stage_size) {
3861 ssize_t retval;
3862 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3863 ret = -EFAULT;
3864 buf += tape->stage_size;
3865 count -= tape->stage_size;
3866 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3867 actually_written += tape->stage_size;
3868 if (retval <= 0)
3869 return (retval);
3870 }
3871 if (count) {
3872 actually_written += count;
3873 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3874 ret = -EFAULT;
3875 tape->merge_stage_size += count;
3876 }
3877 return (ret) ? ret : actually_written;
3878}
3879
3880static int idetape_write_filemark (ide_drive_t *drive)
3881{
3882 idetape_pc_t pc;
3883
3884
3885 idetape_create_write_filemark_cmd(drive, &pc, 1);
3886 if (idetape_queue_pc_tail(drive, &pc)) {
3887 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3888 return -EIO;
3889 }
3890 return 0;
3891}
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3957{
3958 idetape_tape_t *tape = drive->driver_data;
3959 idetape_pc_t pc;
3960 int i,retval;
3961
3962#if IDETAPE_DEBUG_LOG
3963 if (tape->debug_level >= 1)
3964 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3965 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3966#endif
3967
3968
3969
3970 switch (mt_op) {
3971 case MTFSF:
3972 case MTFSFM:
3973 case MTBSF:
3974 case MTBSFM:
3975 if (!mt_count)
3976 return (0);
3977 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3978 default:
3979 break;
3980 }
3981 switch (mt_op) {
3982 case MTWEOF:
3983 if (tape->write_prot)
3984 return -EACCES;
3985 idetape_discard_read_pipeline(drive, 1);
3986 for (i = 0; i < mt_count; i++) {
3987 retval = idetape_write_filemark(drive);
3988 if (retval)
3989 return retval;
3990 }
3991 return (0);
3992 case MTREW:
3993 idetape_discard_read_pipeline(drive, 0);
3994 if (idetape_rewind_tape(drive))
3995 return -EIO;
3996 return 0;
3997 case MTLOAD:
3998 idetape_discard_read_pipeline(drive, 0);
3999 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
4000 return (idetape_queue_pc_tail(drive, &pc));
4001 case MTUNLOAD:
4002 case MTOFFL:
4003
4004
4005
4006
4007 if (tape->door_locked) {
4008 if (idetape_create_prevent_cmd(drive, &pc, 0))
4009 if (!idetape_queue_pc_tail(drive, &pc))
4010 tape->door_locked = DOOR_UNLOCKED;
4011 }
4012 idetape_discard_read_pipeline(drive, 0);
4013 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
4014 retval = idetape_queue_pc_tail(drive, &pc);
4015 if (!retval)
4016 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
4017 return retval;
4018 case MTNOP:
4019 idetape_discard_read_pipeline(drive, 0);
4020 return (idetape_flush_tape_buffers(drive));
4021 case MTRETEN:
4022 idetape_discard_read_pipeline(drive, 0);
4023 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
4024 return (idetape_queue_pc_tail(drive, &pc));
4025 case MTEOM:
4026 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
4027 return (idetape_queue_pc_tail(drive, &pc));
4028 case MTERASE:
4029 (void) idetape_rewind_tape(drive);
4030 idetape_create_erase_cmd(&pc);
4031 return (idetape_queue_pc_tail(drive, &pc));
4032 case MTSETBLK:
4033 if (mt_count) {
4034 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
4035 return -EIO;
4036 tape->user_bs_factor = mt_count / tape->tape_block_size;
4037 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
4038 } else
4039 set_bit(IDETAPE_DETECT_BS, &tape->flags);
4040 return 0;
4041 case MTSEEK:
4042 idetape_discard_read_pipeline(drive, 0);
4043 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
4044 case MTSETPART:
4045 idetape_discard_read_pipeline(drive, 0);
4046 return (idetape_position_tape(drive, 0, mt_count, 0));
4047 case MTFSR:
4048 case MTBSR:
4049 case MTLOCK:
4050 if (!idetape_create_prevent_cmd(drive, &pc, 1))
4051 return 0;
4052 retval = idetape_queue_pc_tail(drive, &pc);
4053 if (retval) return retval;
4054 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
4055 return 0;
4056 case MTUNLOCK:
4057 if (!idetape_create_prevent_cmd(drive, &pc, 0))
4058 return 0;
4059 retval = idetape_queue_pc_tail(drive, &pc);
4060 if (retval) return retval;
4061 tape->door_locked = DOOR_UNLOCKED;
4062 return 0;
4063 default:
4064 printk(KERN_ERR "ide-tape: MTIO operation %d not "
4065 "supported\n", mt_op);
4066 return (-EIO);
4067 }
4068}
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
4094{
4095 struct ide_tape_obj *tape = ide_tape_f(file);
4096 ide_drive_t *drive = tape->drive;
4097 struct mtop mtop;
4098 struct mtget mtget;
4099 struct mtpos mtpos;
4100 int block_offset = 0, position = tape->first_frame_position;
4101 void __user *argp = (void __user *)arg;
4102
4103#if IDETAPE_DEBUG_LOG
4104 if (tape->debug_level >= 3)
4105 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
4106 "cmd=%u\n", cmd);
4107#endif
4108
4109 tape->restart_speed_control_req = 1;
4110 if (tape->chrdev_direction == idetape_direction_write) {
4111 idetape_empty_write_pipeline(drive);
4112 idetape_flush_tape_buffers(drive);
4113 }
4114 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
4115 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
4116 if ((position = idetape_read_position(drive)) < 0)
4117 return -EIO;
4118 }
4119 switch (cmd) {
4120 case MTIOCTOP:
4121 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
4122 return -EFAULT;
4123 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
4124 case MTIOCGET:
4125 memset(&mtget, 0, sizeof (struct mtget));
4126 mtget.mt_type = MT_ISSCSI2;
4127 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
4128 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
4129 if (tape->drv_write_prot) {
4130 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
4131 }
4132 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
4133 return -EFAULT;
4134 return 0;
4135 case MTIOCPOS:
4136 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
4137 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
4138 return -EFAULT;
4139 return 0;
4140 default:
4141 if (tape->chrdev_direction == idetape_direction_read)
4142 idetape_discard_read_pipeline(drive, 1);
4143 return idetape_blkdev_ioctl(drive, cmd, arg);
4144 }
4145}
4146
4147static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
4148
4149
4150
4151
4152static int idetape_chrdev_open (struct inode *inode, struct file *filp)
4153{
4154 unsigned int minor = iminor(inode), i = minor & ~0xc0;
4155 ide_drive_t *drive;
4156 idetape_tape_t *tape;
4157 idetape_pc_t pc;
4158 int retval;
4159
4160
4161
4162
4163
4164
4165 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
4166
4167#if IDETAPE_DEBUG_LOG
4168 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
4169#endif
4170
4171 if (i >= MAX_HWIFS * MAX_DRIVES)
4172 return -ENXIO;
4173
4174 if (!(tape = ide_tape_chrdev_get(i)))
4175 return -ENXIO;
4176
4177 drive = tape->drive;
4178
4179 filp->private_data = tape;
4180
4181 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
4182 retval = -EBUSY;
4183 goto out_put_tape;
4184 }
4185
4186 retval = idetape_wait_ready(drive, 60 * HZ);
4187 if (retval) {
4188 clear_bit(IDETAPE_BUSY, &tape->flags);
4189 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
4190 goto out_put_tape;
4191 }
4192
4193 idetape_read_position(drive);
4194 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
4195 (void)idetape_rewind_tape(drive);
4196
4197 if (tape->chrdev_direction != idetape_direction_read)
4198 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
4199
4200
4201 idetape_get_blocksize_from_block_descriptor(drive);
4202
4203
4204 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
4205 tape->write_prot = 1;
4206 else
4207 tape->write_prot = tape->drv_write_prot;
4208
4209
4210 if (tape->write_prot) {
4211 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
4212 (filp->f_flags & O_ACCMODE) == O_RDWR) {
4213 clear_bit(IDETAPE_BUSY, &tape->flags);
4214 retval = -EROFS;
4215 goto out_put_tape;
4216 }
4217 }
4218
4219
4220
4221
4222 if (tape->chrdev_direction == idetape_direction_none) {
4223 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
4224 if (!idetape_queue_pc_tail(drive, &pc)) {
4225 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
4226 tape->door_locked = DOOR_LOCKED;
4227 }
4228 }
4229 }
4230 idetape_restart_speed_control(drive);
4231 tape->restart_speed_control_req = 0;
4232 return 0;
4233
4234out_put_tape:
4235 ide_tape_put(tape);
4236 return retval;
4237}
4238
4239static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
4240{
4241 idetape_tape_t *tape = drive->driver_data;
4242
4243 idetape_empty_write_pipeline(drive);
4244 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
4245 if (tape->merge_stage != NULL) {
4246 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
4247 __idetape_kfree_stage(tape->merge_stage);
4248 tape->merge_stage = NULL;
4249 }
4250 idetape_write_filemark(drive);
4251 idetape_flush_tape_buffers(drive);
4252 idetape_flush_tape_buffers(drive);
4253}
4254
4255
4256
4257
4258static int idetape_chrdev_release (struct inode *inode, struct file *filp)
4259{
4260 struct ide_tape_obj *tape = ide_tape_f(filp);
4261 ide_drive_t *drive = tape->drive;
4262 idetape_pc_t pc;
4263 unsigned int minor = iminor(inode);
4264
4265 lock_kernel();
4266 tape = drive->driver_data;
4267#if IDETAPE_DEBUG_LOG
4268 if (tape->debug_level >= 3)
4269 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
4270#endif
4271
4272 if (tape->chrdev_direction == idetape_direction_write)
4273 idetape_write_release(drive, minor);
4274 if (tape->chrdev_direction == idetape_direction_read) {
4275 if (minor < 128)
4276 idetape_discard_read_pipeline(drive, 1);
4277 else
4278 idetape_wait_for_pipeline(drive);
4279 }
4280 if (tape->cache_stage != NULL) {
4281 __idetape_kfree_stage(tape->cache_stage);
4282 tape->cache_stage = NULL;
4283 }
4284 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
4285 (void) idetape_rewind_tape(drive);
4286 if (tape->chrdev_direction == idetape_direction_none) {
4287 if (tape->door_locked == DOOR_LOCKED) {
4288 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
4289 if (!idetape_queue_pc_tail(drive, &pc))
4290 tape->door_locked = DOOR_UNLOCKED;
4291 }
4292 }
4293 }
4294 clear_bit(IDETAPE_BUSY, &tape->flags);
4295 ide_tape_put(tape);
4296 unlock_kernel();
4297 return 0;
4298}
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309static int idetape_identify_device (ide_drive_t *drive)
4310{
4311 struct idetape_id_gcw gcw;
4312 struct hd_driveid *id = drive->id;
4313#if IDETAPE_DEBUG_INFO
4314 unsigned short mask,i;
4315#endif
4316
4317 if (drive->id_read == 0)
4318 return 1;
4319
4320 *((unsigned short *) &gcw) = id->config;
4321
4322#if IDETAPE_DEBUG_INFO
4323 printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
4324 printk(KERN_INFO "ide-tape: Protocol Type: ");
4325 switch (gcw.protocol) {
4326 case 0: case 1: printk("ATA\n");break;
4327 case 2: printk("ATAPI\n");break;
4328 case 3: printk("Reserved (Unknown to ide-tape)\n");break;
4329 }
4330 printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
4331 switch (gcw.device_type) {
4332 case 0: printk("Direct-access Device\n");break;
4333 case 1: printk("Streaming Tape Device\n");break;
4334 case 2: case 3: case 4: printk("Reserved\n");break;
4335 case 5: printk("CD-ROM Device\n");break;
4336 case 6: printk("Reserved\n");
4337 case 7: printk("Optical memory Device\n");break;
4338 case 0x1f: printk("Unknown or no Device type\n");break;
4339 default: printk("Reserved\n");
4340 }
4341 printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
4342 printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
4343 switch (gcw.drq_type) {
4344 case 0: printk("Microprocessor DRQ\n");break;
4345 case 1: printk("Interrupt DRQ\n");break;
4346 case 2: printk("Accelerated DRQ\n");break;
4347 case 3: printk("Reserved\n");break;
4348 }
4349 printk(KERN_INFO "ide-tape: Command Packet Size: ");
4350 switch (gcw.packet_size) {
4351 case 0: printk("12 bytes\n");break;
4352 case 1: printk("16 bytes\n");break;
4353 default: printk("Reserved\n");break;
4354 }
4355 printk(KERN_INFO "ide-tape: Model: %.40s\n",id->model);
4356 printk(KERN_INFO "ide-tape: Firmware Revision: %.8s\n",id->fw_rev);
4357 printk(KERN_INFO "ide-tape: Serial Number: %.20s\n",id->serial_no);
4358 printk(KERN_INFO "ide-tape: Write buffer size: %d bytes\n",id->buf_size*512);
4359 printk(KERN_INFO "ide-tape: DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
4360 printk(KERN_INFO "ide-tape: LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
4361 printk(KERN_INFO "ide-tape: IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
4362 printk(KERN_INFO "ide-tape: IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
4363 printk(KERN_INFO "ide-tape: ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n");
4364 printk(KERN_INFO "ide-tape: PIO Cycle Timing Category: %d\n",id->tPIO);
4365 printk(KERN_INFO "ide-tape: DMA Cycle Timing Category: %d\n",id->tDMA);
4366 printk(KERN_INFO "ide-tape: Single Word DMA supported modes: ");
4367 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
4368 if (id->dma_1word & mask)
4369 printk("%d ",i);
4370 if (id->dma_1word & (mask << 8))
4371 printk("(active) ");
4372 }
4373 printk("\n");
4374 printk(KERN_INFO "ide-tape: Multi Word DMA supported modes: ");
4375 for (i=0,mask=1;i<8;i++,mask=mask << 1) {
4376 if (id->dma_mword & mask)
4377 printk("%d ",i);
4378 if (id->dma_mword & (mask << 8))
4379 printk("(active) ");
4380 }
4381 printk("\n");
4382 if (id->field_valid & 0x0002) {
4383 printk(KERN_INFO "ide-tape: Enhanced PIO Modes: %s\n",
4384 id->eide_pio_modes & 1 ? "Mode 3":"None");
4385 printk(KERN_INFO "ide-tape: Minimum Multi-word DMA cycle per word: ");
4386 if (id->eide_dma_min == 0)
4387 printk("Not supported\n");
4388 else
4389 printk("%d ns\n",id->eide_dma_min);
4390
4391 printk(KERN_INFO "ide-tape: Manufacturer\'s Recommended Multi-word cycle: ");
4392 if (id->eide_dma_time == 0)
4393 printk("Not supported\n");
4394 else
4395 printk("%d ns\n",id->eide_dma_time);
4396
4397 printk(KERN_INFO "ide-tape: Minimum PIO cycle without IORDY: ");
4398 if (id->eide_pio == 0)
4399 printk("Not supported\n");
4400 else
4401 printk("%d ns\n",id->eide_pio);
4402
4403 printk(KERN_INFO "ide-tape: Minimum PIO cycle with IORDY: ");
4404 if (id->eide_pio_iordy == 0)
4405 printk("Not supported\n");
4406 else
4407 printk("%d ns\n",id->eide_pio_iordy);
4408
4409 } else
4410 printk(KERN_INFO "ide-tape: According to the device, fields 64-70 are not valid.\n");
4411#endif
4412
4413
4414
4415 if (gcw.protocol !=2 )
4416 printk(KERN_ERR "ide-tape: Protocol is not ATAPI\n");
4417 else if (gcw.device_type != 1)
4418 printk(KERN_ERR "ide-tape: Device type is not set to tape\n");
4419 else if (!gcw.removable)
4420 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
4421 else if (gcw.packet_size != 0) {
4422 printk(KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
4423 if (gcw.packet_size == 1)
4424 printk(KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
4425 } else
4426 return 1;
4427 return 0;
4428}
4429
4430
4431
4432
4433static void idetape_get_inquiry_results (ide_drive_t *drive)
4434{
4435 char *r;
4436 idetape_tape_t *tape = drive->driver_data;
4437 idetape_pc_t pc;
4438 idetape_inquiry_result_t *inquiry;
4439
4440 idetape_create_inquiry_cmd(&pc);
4441 if (idetape_queue_pc_tail(drive, &pc)) {
4442 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
4443 return;
4444 }
4445 inquiry = (idetape_inquiry_result_t *) pc.buffer;
4446 memcpy(tape->vendor_id, inquiry->vendor_id, 8);
4447 memcpy(tape->product_id, inquiry->product_id, 16);
4448 memcpy(tape->firmware_revision, inquiry->revision_level, 4);
4449 ide_fixstring(tape->vendor_id, 10, 0);
4450 ide_fixstring(tape->product_id, 18, 0);
4451 ide_fixstring(tape->firmware_revision, 6, 0);
4452 r = tape->firmware_revision;
4453 if (*(r + 1) == '.')
4454 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
4455 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n", drive->name, tape->name, tape->vendor_id, tape->product_id, tape->firmware_revision);
4456}
4457
4458
4459
4460
4461
4462
4463static void idetape_get_mode_sense_results (ide_drive_t *drive)
4464{
4465 idetape_tape_t *tape = drive->driver_data;
4466 idetape_pc_t pc;
4467 idetape_mode_parameter_header_t *header;
4468 idetape_capabilities_page_t *capabilities;
4469
4470 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
4471 if (idetape_queue_pc_tail(drive, &pc)) {
4472 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
4473 tape->tape_block_size = 512;
4474 tape->capabilities.ctl = 52;
4475 tape->capabilities.speed = 450;
4476 tape->capabilities.buffer_size = 6 * 52;
4477 return;
4478 }
4479 header = (idetape_mode_parameter_header_t *) pc.buffer;
4480 capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
4481
4482 capabilities->max_speed = ntohs(capabilities->max_speed);
4483 capabilities->ctl = ntohs(capabilities->ctl);
4484 capabilities->speed = ntohs(capabilities->speed);
4485 capabilities->buffer_size = ntohs(capabilities->buffer_size);
4486
4487 if (!capabilities->speed) {
4488 printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
4489 capabilities->speed = 650;
4490 }
4491 if (!capabilities->max_speed) {
4492 printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
4493 capabilities->max_speed = 650;
4494 }
4495
4496 tape->capabilities = *capabilities;
4497 if (capabilities->blk512)
4498 tape->tape_block_size = 512;
4499 else if (capabilities->blk1024)
4500 tape->tape_block_size = 1024;
4501
4502#if IDETAPE_DEBUG_INFO
4503 printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
4504 printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
4505 printk(KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length);
4506 printk(KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type);
4507 printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp);
4508 printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl);
4509
4510 printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
4511 printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
4512 printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
4513 printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
4514 printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
4515 printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
4516 printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
4517 printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
4518 printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
4519 printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
4520 printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
4521 printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
4522 printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
4523 printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
4524 printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
4525 printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
4526 printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
4527 printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
4528 printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
4529 printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
4530#endif
4531}
4532
4533
4534
4535
4536
4537static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
4538{
4539
4540 idetape_tape_t *tape = drive->driver_data;
4541 idetape_pc_t pc;
4542 idetape_mode_parameter_header_t *header;
4543 idetape_parameter_block_descriptor_t *block_descrp;
4544
4545 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
4546 if (idetape_queue_pc_tail(drive, &pc)) {
4547 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
4548 if (tape->tape_block_size == 0) {
4549 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
4550 tape->tape_block_size = 32768;
4551 }
4552 return;
4553 }
4554 header = (idetape_mode_parameter_header_t *) pc.buffer;
4555 block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t));
4556 tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
4557 tape->drv_write_prot = (header->dsp & 0x80) >> 7;
4558
4559#if IDETAPE_DEBUG_INFO
4560 printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
4561#endif
4562}
4563
4564#ifdef CONFIG_IDE_PROC_FS
4565static void idetape_add_settings (ide_drive_t *drive)
4566{
4567 idetape_tape_t *tape = drive->driver_data;
4568
4569
4570
4571
4572 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
4573 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
4574 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
4575 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
4576 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
4577 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
4578 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
4579 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
4580 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_frequency, NULL);
4581 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
4582 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
4583 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
4584 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
4585 ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
4586}
4587#else
4588static inline void idetape_add_settings(ide_drive_t *drive) { ; }
4589#endif
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
4604{
4605 unsigned long t1, tmid, tn, t;
4606 int speed;
4607 struct idetape_id_gcw gcw;
4608 int stage_size;
4609 struct sysinfo si;
4610
4611 spin_lock_init(&tape->spinlock);
4612 drive->dsc_overlap = 1;
4613#ifdef CONFIG_BLK_DEV_IDEPCI
4614 if (HWIF(drive)->pci_dev != NULL) {
4615
4616
4617
4618
4619 if ((HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) ||
4620 (HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_TTI_HPT343)) {
4621 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", tape->name);
4622 drive->dsc_overlap = 0;
4623 }
4624 }
4625#endif
4626
4627 if (strstr(drive->id->model, "Seagate STT3401"))
4628 drive->dsc_overlap = 0;
4629 tape->minor = minor;
4630 tape->name[0] = 'h';
4631 tape->name[1] = 't';
4632 tape->name[2] = '0' + minor;
4633 tape->chrdev_direction = idetape_direction_none;
4634 tape->pc = tape->pc_stack;
4635 tape->max_insert_speed = 10000;
4636 tape->speed_control = 1;
4637 *((unsigned short *) &gcw) = drive->id->config;
4638 if (gcw.drq_type == 1)
4639 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
4640
4641 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
4642
4643 idetape_get_inquiry_results(drive);
4644 idetape_get_mode_sense_results(drive);
4645 idetape_get_blocksize_from_block_descriptor(drive);
4646 tape->user_bs_factor = 1;
4647 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4648 while (tape->stage_size > 0xffff) {
4649 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
4650 tape->capabilities.ctl /= 2;
4651 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4652 }
4653 stage_size = tape->stage_size;
4654 tape->pages_per_stage = stage_size / PAGE_SIZE;
4655 if (stage_size % PAGE_SIZE) {
4656 tape->pages_per_stage++;
4657 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
4658 }
4659
4660
4661
4662
4663
4664 speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
4665
4666 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
4667
4668
4669
4670
4671 si_meminfo(&si);
4672 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
4673 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
4674 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
4675 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
4676 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
4677 if (tape->max_stages == 0)
4678 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
4679
4680 t1 = (tape->stage_size * HZ) / (speed * 1000);
4681 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
4682 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
4683
4684 if (tape->max_stages)
4685 t = tn;
4686 else
4687 t = t1;
4688
4689
4690
4691
4692
4693 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
4694 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
4695 "%dkB pipeline, %lums tDSC%s\n",
4696 drive->name, tape->name, tape->capabilities.speed,
4697 (tape->capabilities.buffer_size * 512) / tape->stage_size,
4698 tape->stage_size / 1024,
4699 tape->max_stages * tape->stage_size / 1024,
4700 tape->best_dsc_rw_frequency * 1000 / HZ,
4701 drive->using_dma ? ", DMA":"");
4702
4703 idetape_add_settings(drive);
4704}
4705
4706static void ide_tape_remove(ide_drive_t *drive)
4707{
4708 idetape_tape_t *tape = drive->driver_data;
4709
4710 ide_proc_unregister_driver(drive, tape->driver);
4711
4712 ide_unregister_region(tape->disk);
4713
4714 ide_tape_put(tape);
4715}
4716
4717static void ide_tape_release(struct kref *kref)
4718{
4719 struct ide_tape_obj *tape = to_ide_tape(kref);
4720 ide_drive_t *drive = tape->drive;
4721 struct gendisk *g = tape->disk;
4722
4723 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
4724
4725 drive->dsc_overlap = 0;
4726 drive->driver_data = NULL;
4727 class_device_destroy(idetape_sysfs_class,
4728 MKDEV(IDETAPE_MAJOR, tape->minor));
4729 class_device_destroy(idetape_sysfs_class,
4730 MKDEV(IDETAPE_MAJOR, tape->minor + 128));
4731 idetape_devs[tape->minor] = NULL;
4732 g->private_data = NULL;
4733 put_disk(g);
4734 kfree(tape);
4735}
4736
4737#ifdef CONFIG_IDE_PROC_FS
4738static int proc_idetape_read_name
4739 (char *page, char **start, off_t off, int count, int *eof, void *data)
4740{
4741 ide_drive_t *drive = (ide_drive_t *) data;
4742 idetape_tape_t *tape = drive->driver_data;
4743 char *out = page;
4744 int len;
4745
4746 len = sprintf(out, "%s\n", tape->name);
4747 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
4748}
4749
4750static ide_proc_entry_t idetape_proc[] = {
4751 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
4752 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
4753 { NULL, 0, NULL, NULL }
4754};
4755#endif
4756
4757static int ide_tape_probe(ide_drive_t *);
4758
4759static ide_driver_t idetape_driver = {
4760 .gen_driver = {
4761 .owner = THIS_MODULE,
4762 .name = "ide-tape",
4763 .bus = &ide_bus_type,
4764 },
4765 .probe = ide_tape_probe,
4766 .remove = ide_tape_remove,
4767 .version = IDETAPE_VERSION,
4768 .media = ide_tape,
4769 .supports_dsc_overlap = 1,
4770 .do_request = idetape_do_request,
4771 .end_request = idetape_end_request,
4772 .error = __ide_error,
4773 .abort = __ide_abort,
4774#ifdef CONFIG_IDE_PROC_FS
4775 .proc = idetape_proc,
4776#endif
4777};
4778
4779
4780
4781
4782static const struct file_operations idetape_fops = {
4783 .owner = THIS_MODULE,
4784 .read = idetape_chrdev_read,
4785 .write = idetape_chrdev_write,
4786 .ioctl = idetape_chrdev_ioctl,
4787 .open = idetape_chrdev_open,
4788 .release = idetape_chrdev_release,
4789};
4790
4791static int idetape_open(struct inode *inode, struct file *filp)
4792{
4793 struct gendisk *disk = inode->i_bdev->bd_disk;
4794 struct ide_tape_obj *tape;
4795
4796 if (!(tape = ide_tape_get(disk)))
4797 return -ENXIO;
4798
4799 return 0;
4800}
4801
4802static int idetape_release(struct inode *inode, struct file *filp)
4803{
4804 struct gendisk *disk = inode->i_bdev->bd_disk;
4805 struct ide_tape_obj *tape = ide_tape_g(disk);
4806
4807 ide_tape_put(tape);
4808
4809 return 0;
4810}
4811
4812static int idetape_ioctl(struct inode *inode, struct file *file,
4813 unsigned int cmd, unsigned long arg)
4814{
4815 struct block_device *bdev = inode->i_bdev;
4816 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4817 ide_drive_t *drive = tape->drive;
4818 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4819 if (err == -EINVAL)
4820 err = idetape_blkdev_ioctl(drive, cmd, arg);
4821 return err;
4822}
4823
4824static struct block_device_operations idetape_block_ops = {
4825 .owner = THIS_MODULE,
4826 .open = idetape_open,
4827 .release = idetape_release,
4828 .ioctl = idetape_ioctl,
4829};
4830
4831static int ide_tape_probe(ide_drive_t *drive)
4832{
4833 idetape_tape_t *tape;
4834 struct gendisk *g;
4835 int minor;
4836
4837 if (!strstr("ide-tape", drive->driver_req))
4838 goto failed;
4839 if (!drive->present)
4840 goto failed;
4841 if (drive->media != ide_tape)
4842 goto failed;
4843 if (!idetape_identify_device (drive)) {
4844 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4845 goto failed;
4846 }
4847 if (drive->scsi) {
4848 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4849 goto failed;
4850 }
4851 if (strstr(drive->id->model, "OnStream DI-")) {
4852 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4853 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4854 }
4855 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
4856 if (tape == NULL) {
4857 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4858 goto failed;
4859 }
4860
4861 g = alloc_disk(1 << PARTN_BITS);
4862 if (!g)
4863 goto out_free_tape;
4864
4865 ide_init_disk(g, drive);
4866
4867 ide_proc_register_driver(drive, &idetape_driver);
4868
4869 kref_init(&tape->kref);
4870
4871 tape->drive = drive;
4872 tape->driver = &idetape_driver;
4873 tape->disk = g;
4874
4875 g->private_data = &tape->driver;
4876
4877 drive->driver_data = tape;
4878
4879 mutex_lock(&idetape_ref_mutex);
4880 for (minor = 0; idetape_devs[minor]; minor++)
4881 ;
4882 idetape_devs[minor] = tape;
4883 mutex_unlock(&idetape_ref_mutex);
4884
4885 idetape_setup(drive, tape, minor);
4886
4887 class_device_create(idetape_sysfs_class, NULL,
4888 MKDEV(IDETAPE_MAJOR, minor), &drive->gendev, "%s", tape->name);
4889 class_device_create(idetape_sysfs_class, NULL,
4890 MKDEV(IDETAPE_MAJOR, minor + 128), &drive->gendev, "n%s", tape->name);
4891
4892 g->fops = &idetape_block_ops;
4893 ide_register_region(g);
4894
4895 return 0;
4896
4897out_free_tape:
4898 kfree(tape);
4899failed:
4900 return -ENODEV;
4901}
4902
4903MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4904MODULE_LICENSE("GPL");
4905
4906static void __exit idetape_exit (void)
4907{
4908 driver_unregister(&idetape_driver.gen_driver);
4909 class_destroy(idetape_sysfs_class);
4910 unregister_chrdev(IDETAPE_MAJOR, "ht");
4911}
4912
4913static int __init idetape_init(void)
4914{
4915 int error = 1;
4916 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4917 if (IS_ERR(idetape_sysfs_class)) {
4918 idetape_sysfs_class = NULL;
4919 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4920 error = -EBUSY;
4921 goto out;
4922 }
4923
4924 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4925 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
4926 error = -EBUSY;
4927 goto out_free_class;
4928 }
4929
4930 error = driver_register(&idetape_driver.gen_driver);
4931 if (error)
4932 goto out_free_driver;
4933
4934 return 0;
4935
4936out_free_driver:
4937 driver_unregister(&idetape_driver.gen_driver);
4938out_free_class:
4939 class_destroy(idetape_sysfs_class);
4940out:
4941 return error;
4942}
4943
4944MODULE_ALIAS("ide:*m-tape*");
4945module_init(idetape_init);
4946module_exit(idetape_exit);
4947MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
4948