1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
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
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
|
/*
SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
file README.st for more information.
History:
Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
Contribution and ideas from several people including (in alphabetical
order) Klaus Ehrenfried, Wolfgang Denk, Steve Hirsch, Andreas Koppenh"ofer,
Michael Leodolter, Eyal Lebedinsky, J"org Weule, and Eric Youngdale.
Copyright 1992 - 1998 Kai Makisara
email Kai.Makisara@metla.fi
Last modified: Sun Sep 6 09:34:49 1998 by root@home
Some small formal changes - aeb, 950809
*/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/mtio.h>
#include <linux/ioctl.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>
#include <asm/dma.h>
#include <asm/system.h>
#include <asm/spinlock.h>
/* The driver prints some debugging information on the console if DEBUG
is defined and non-zero. */
#define DEBUG 0
/* The message level for the debug messages is currently set to KERN_NOTICE
so that people can easily see the messages. Later when the debugging messages
in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
#define ST_DEB_MSG KERN_NOTICE
#define MAJOR_NR SCSI_TAPE_MAJOR
#include <linux/blk.h>
#include "scsi.h"
#include "hosts.h"
#include <scsi/scsi_ioctl.h>
#define ST_KILOBYTE 1024
#include "st_options.h"
#include "st.h"
#include "constants.h"
#ifdef MODULE
MODULE_PARM(buffer_kbs, "i");
MODULE_PARM(write_threshold_kbs, "i");
MODULE_PARM(max_buffers, "i");
MODULE_PARM(max_sg_segs, "i");
static int buffer_kbs = 0;
static int write_threshold_kbs = 0;
static int max_buffers = 0;
static int max_sg_segs = 0;
#endif
/* The default definitions have been moved to st_options.h */
#define ST_BUFFER_SIZE (ST_BUFFER_BLOCKS * ST_KILOBYTE)
#define ST_WRITE_THRESHOLD (ST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
/* The buffer size should fit into the 24 bits for length in the
6-byte SCSI read and write commands. */
#if ST_BUFFER_SIZE >= (2 << 24 - 1)
#error "Buffer size should not exceed (2 << 24 - 1) bytes!"
#endif
#if DEBUG
static int debugging = 1;
#endif
#define MAX_RETRIES 0
#define MAX_WRITE_RETRIES 0
#define MAX_READY_RETRIES 5
#define NO_TAPE NOT_READY
#define ST_TIMEOUT (900 * HZ)
#define ST_LONG_TIMEOUT (14000 * HZ)
#define TAPE_NR(x) (MINOR(x) & ~(128 | ST_MODE_MASK))
#define TAPE_MODE(x) ((MINOR(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
/* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
24 bits) */
#define SET_DENS_AND_BLK 0x10001
static int st_nbr_buffers;
static ST_buffer **st_buffers;
static int st_buffer_size = ST_BUFFER_SIZE;
static int st_write_threshold = ST_WRITE_THRESHOLD;
static int st_max_buffers = ST_MAX_BUFFERS;
static int st_max_sg_segs = ST_MAX_SG;
static Scsi_Tape * scsi_tapes = NULL;
static int modes_defined = FALSE;
static ST_buffer *new_tape_buffer(int, int);
static int enlarge_buffer(ST_buffer *, int, int);
static void normalize_buffer(ST_buffer *);
static int append_to_buffer(const char *, ST_buffer *, int);
static int from_buffer(ST_buffer *, char *, int);
static int st_init(void);
static int st_attach(Scsi_Device *);
static int st_detect(Scsi_Device *);
static void st_detach(Scsi_Device *);
struct Scsi_Device_Template st_template = {NULL, "tape", "st", NULL, TYPE_TAPE,
SCSI_TAPE_MAJOR, 0, 0, 0, 0,
st_detect, st_init,
NULL, st_attach, st_detach};
static int st_compression(Scsi_Tape *, int);
static int find_partition(struct inode *);
static int update_partition(struct inode *);
static int st_int_ioctl(struct inode * inode, unsigned int cmd_in,
unsigned long arg);
/* Convert the result to success code */
static int
st_chk_result(Scsi_Cmnd * SCpnt)
{
int dev = TAPE_NR(SCpnt->request.rq_dev);
int result = SCpnt->result;
unsigned char * sense = SCpnt->sense_buffer, scode;
#if DEBUG
const char *stp;
#endif
if (!result /* && SCpnt->sense_buffer[0] == 0 */ )
return 0;
if (driver_byte(result) & DRIVER_SENSE)
scode = sense[2] & 0x0f;
else {
sense[0] = 0; /* We don't have sense data if this byte is zero */
scode = 0;
}
#if DEBUG
if (debugging) {
printk(ST_DEB_MSG "st%d: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n",
dev, result,
SCpnt->data_cmnd[0], SCpnt->data_cmnd[1], SCpnt->data_cmnd[2],
SCpnt->data_cmnd[3], SCpnt->data_cmnd[4], SCpnt->data_cmnd[5],
SCpnt->request_bufflen);
if (driver_byte(result) & DRIVER_SENSE)
print_sense("st", SCpnt);
else
printk("\n");
}
else
#endif
if (!(driver_byte(result) & DRIVER_SENSE) ||
((sense[0] & 0x70) == 0x70 &&
scode != NO_SENSE &&
scode != RECOVERED_ERROR &&
/* scode != UNIT_ATTENTION && */
scode != BLANK_CHECK &&
scode != VOLUME_OVERFLOW &&
SCpnt->data_cmnd[0] != MODE_SENSE &&
SCpnt->data_cmnd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */
if (driver_byte(result) & DRIVER_SENSE) {
printk(KERN_WARNING "st%d: Error with sense data: ", dev);
print_sense("st", SCpnt);
}
else
printk(KERN_WARNING
"st%d: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n",
dev, result, suggestion(result), driver_byte(result),
host_byte(result));
}
if ((sense[0] & 0x70) == 0x70 &&
scode == RECOVERED_ERROR
#if ST_RECOVERED_WRITE_FATAL
&& SCpnt->data_cmnd[0] != WRITE_6
&& SCpnt->data_cmnd[0] != WRITE_FILEMARKS
#endif
) {
scsi_tapes[dev].recover_count++;
scsi_tapes[dev].mt_status->mt_erreg += (1 << MT_ST_SOFTERR_SHIFT);
#if DEBUG
if (debugging) {
if (SCpnt->data_cmnd[0] == READ_6)
stp = "read";
else if (SCpnt->data_cmnd[0] == WRITE_6)
stp = "write";
else
stp = "ioctl";
printk(ST_DEB_MSG "st%d: Recovered %s error (%d).\n", dev, stp,
scsi_tapes[dev].recover_count);
}
#endif
if ((sense[2] & 0xe0) == 0)
return 0;
}
return (-EIO);
}
/* Wakeup from interrupt */
static void
st_sleep_done (Scsi_Cmnd * SCpnt)
{
unsigned int st_nbr;
int remainder;
Scsi_Tape * STp;
if ((st_nbr = TAPE_NR(SCpnt->request.rq_dev)) < st_template.nr_dev) {
STp = &(scsi_tapes[st_nbr]);
if ((STp->buffer)->writing &&
(SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
(SCpnt->sense_buffer[2] & 0x40)) {
/* EOM at write-behind, has all been written? */
if ((SCpnt->sense_buffer[0] & 0x80) != 0)
remainder = (SCpnt->sense_buffer[3] << 24) |
(SCpnt->sense_buffer[4] << 16) |
(SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
else
remainder = 0;
if ((SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW ||
remainder > 0)
(STp->buffer)->last_result = SCpnt->result; /* Error */
else
(STp->buffer)->last_result = INT_MAX; /* OK */
}
else
(STp->buffer)->last_result = SCpnt->result;
SCpnt->request.rq_status = RQ_SCSI_DONE;
(STp->buffer)->last_SCpnt = SCpnt;
#if DEBUG
STp->write_pending = 0;
#endif
up(SCpnt->request.sem);
}
#if DEBUG
else if (debugging)
printk(KERN_ERR "st?: Illegal interrupt device %x\n", st_nbr);
#endif
}
/* Do the scsi command. Waits until command performed if do_wait is true.
Otherwise write_behind_check() is used to check that the command
has finished. */
static Scsi_Cmnd *
st_do_scsi(Scsi_Cmnd *SCpnt, Scsi_Tape *STp, unsigned char *cmd, int bytes,
int timeout, int retries, int do_wait)
{
unsigned long flags;
unsigned char *bp;
spin_lock_irqsave(&io_request_lock, flags);
if (SCpnt == NULL)
if ((SCpnt = scsi_allocate_device(NULL, STp->device, 1)) == NULL) {
printk(KERN_ERR "st%d: Can't get SCSI request.\n", TAPE_NR(STp->devt));
spin_unlock_irqrestore(&io_request_lock, flags);
return NULL;
}
cmd[1] |= (SCpnt->lun << 5) & 0xe0;
STp->sem = MUTEX_LOCKED;
SCpnt->use_sg = (bytes > (STp->buffer)->sg[0].length) ?
(STp->buffer)->use_sg : 0;
if (SCpnt->use_sg) {
bp = (char *)&((STp->buffer)->sg[0]);
if ((STp->buffer)->sg_segs < SCpnt->use_sg)
SCpnt->use_sg = (STp->buffer)->sg_segs;
}
else
bp = (STp->buffer)->b_data;
SCpnt->request.sem = &(STp->sem);
SCpnt->request.rq_status = RQ_SCSI_BUSY;
SCpnt->request.rq_dev = STp->devt;
scsi_do_cmd(SCpnt, (void *)cmd, bp, bytes,
st_sleep_done, timeout, retries);
spin_unlock_irqrestore(&io_request_lock, flags);
if (do_wait) {
down(SCpnt->request.sem);
(STp->buffer)->last_result_fatal = st_chk_result(SCpnt);
}
return SCpnt;
}
/* Handle the write-behind checking (downs the semaphore) */
static void
write_behind_check(Scsi_Tape *STp)
{
ST_buffer * STbuffer;
ST_partstat * STps;
STbuffer = STp->buffer;
#if DEBUG
if (STp->write_pending)
STp->nbr_waits++;
else
STp->nbr_finished++;
#endif
down(&(STp->sem));
(STp->buffer)->last_result_fatal = st_chk_result((STp->buffer)->last_SCpnt);
scsi_release_command((STp->buffer)->last_SCpnt);
if (STbuffer->writing < STbuffer->buffer_bytes)
#if 0
memcpy(STbuffer->b_data,
STbuffer->b_data + STbuffer->writing,
STbuffer->buffer_bytes - STbuffer->writing);
#else
printk(KERN_WARNING "st: write_behind_check: something left in buffer!\n");
#endif
STbuffer->buffer_bytes -= STbuffer->writing;
STps = &(STp->ps[STp->partition]);
if (STps->drv_block >= 0) {
if (STp->block_size == 0)
STps->drv_block++;
else
STps->drv_block += STbuffer->writing / STp->block_size;
}
STbuffer->writing = 0;
return;
}
/* Step over EOF if it has been inadvertently crossed (ioctl not used because
it messes up the block number). */
static int
cross_eof(Scsi_Tape *STp, int forward)
{
Scsi_Cmnd *SCpnt;
unsigned char cmd[10];
cmd[0] = SPACE;
cmd[1] = 0x01; /* Space FileMarks */
if (forward) {
cmd[2] = cmd[3] = 0;
cmd[4] = 1;
}
else
cmd[2] = cmd[3] = cmd[4] = 0xff; /* -1 filemarks */
cmd[5] = 0;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Stepping over filemark %s.\n",
TAPE_NR(STp->devt), forward ? "forward" : "backward");
#endif
SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->timeout, MAX_RETRIES, TRUE);
if (!SCpnt)
return (-EBUSY);
scsi_release_command(SCpnt);
SCpnt = NULL;
if ((STp->buffer)->last_result != 0)
printk(KERN_ERR "st%d: Stepping over filemark %s failed.\n",
TAPE_NR(STp->devt), forward ? "forward" : "backward");
return (STp->buffer)->last_result_fatal;
}
/* Flush the write buffer (never need to write if variable blocksize). */
static int
flush_write_buffer(Scsi_Tape *STp)
{
int offset, transfer, blks;
int result;
unsigned char cmd[10];
Scsi_Cmnd *SCpnt;
ST_partstat * STps;
if ((STp->buffer)->writing) {
write_behind_check(STp);
if ((STp->buffer)->last_result_fatal) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Async write error (flush) %x.\n",
TAPE_NR(STp->devt), (STp->buffer)->last_result);
#endif
if ((STp->buffer)->last_result == INT_MAX)
return (-ENOSPC);
return (-EIO);
}
}
if (STp->block_size == 0)
return 0;
result = 0;
if (STp->dirty == 1) {
offset = (STp->buffer)->buffer_bytes;
transfer = ((offset + STp->block_size - 1) /
STp->block_size) * STp->block_size;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Flushing %d bytes.\n", TAPE_NR(STp->devt), transfer);
#endif
memset((STp->buffer)->b_data + offset, 0, transfer - offset);
memset(cmd, 0, 10);
cmd[0] = WRITE_6;
cmd[1] = 1;
blks = transfer / STp->block_size;
cmd[2] = blks >> 16;
cmd[3] = blks >> 8;
cmd[4] = blks;
SCpnt = st_do_scsi(NULL, STp, cmd, transfer, STp->timeout, MAX_WRITE_RETRIES,
TRUE);
if (!SCpnt)
return (-EBUSY);
STps = &(STp->ps[STp->partition]);
if ((STp->buffer)->last_result_fatal != 0) {
if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
(SCpnt->sense_buffer[2] & 0x40) &&
(SCpnt->sense_buffer[2] & 0x0f) == NO_SENSE) {
STp->dirty = 0;
(STp->buffer)->buffer_bytes = 0;
result = (-ENOSPC);
}
else {
printk(KERN_ERR "st%d: Error on flush.\n", TAPE_NR(STp->devt));
result = (-EIO);
}
STps->drv_block = (-1);
}
else {
if (STps->drv_block >= 0)
STps->drv_block += blks;
STp->dirty = 0;
(STp->buffer)->buffer_bytes = 0;
}
scsi_release_command(SCpnt);
SCpnt = NULL;
}
return result;
}
/* Flush the tape buffer. The tape will be positioned correctly unless
seek_next is true. */
static int
flush_buffer(struct inode * inode, struct file * filp, int seek_next)
{
int backspace, result;
Scsi_Tape * STp;
ST_buffer * STbuffer;
ST_partstat * STps;
int dev = TAPE_NR(inode->i_rdev);
STp = &(scsi_tapes[dev]);
STbuffer = STp->buffer;
/*
* If there was a bus reset, block further access
* to this device.
*/
if( STp->device->was_reset )
return (-EIO);
if (STp->ready != ST_READY)
return 0;
STps = &(STp->ps[STp->partition]);
if (STps->rw == ST_WRITING) /* Writing */
return flush_write_buffer(STp);
if (STp->block_size == 0)
return 0;
backspace = ((STp->buffer)->buffer_bytes +
(STp->buffer)->read_pointer) / STp->block_size -
((STp->buffer)->read_pointer + STp->block_size - 1) /
STp->block_size;
(STp->buffer)->buffer_bytes = 0;
(STp->buffer)->read_pointer = 0;
result = 0;
if (!seek_next) {
if (STps->eof == ST_FM_HIT) {
result = cross_eof(STp, FALSE); /* Back over the EOF hit */
if (!result)
STps->eof = ST_NOEOF;
else {
if (STps->drv_file >= 0)
STps->drv_file++;
STps->drv_block = 0;
}
}
if (!result && backspace > 0)
result = st_int_ioctl(inode, MTBSR, backspace);
}
else if (STps->eof == ST_FM_HIT) {
if (STps->drv_file >= 0)
STps->drv_file++;
STps->drv_block = 0;
STps->eof = ST_NOEOF;
}
return result;
}
/* Set the mode parameters */
static int
set_mode_densblk(struct inode * inode, Scsi_Tape *STp, ST_mode *STm)
{
int set_it = FALSE;
unsigned long arg;
int dev = TAPE_NR(inode->i_rdev);
if (!STp->density_changed &&
STm->default_density >= 0 &&
STm->default_density != STp->density) {
arg = STm->default_density;
set_it = TRUE;
}
else
arg = STp->density;
arg <<= MT_ST_DENSITY_SHIFT;
if (!STp->blksize_changed &&
STm->default_blksize >= 0 &&
STm->default_blksize != STp->block_size) {
arg |= STm->default_blksize;
set_it = TRUE;
}
else
arg |= STp->block_size;
if (set_it &&
st_int_ioctl(inode, SET_DENS_AND_BLK, arg)) {
printk(KERN_WARNING
"st%d: Can't set default block size to %d bytes and density %x.\n",
dev, STm->default_blksize, STm->default_density);
if (modes_defined)
return (-EINVAL);
}
return 0;
}
/* Open the device */
static int
scsi_tape_open(struct inode * inode, struct file * filp)
{
unsigned short flags;
int i, need_dma_buffer, new_session = FALSE;
unsigned char cmd[10];
Scsi_Cmnd * SCpnt;
Scsi_Tape * STp;
ST_mode * STm;
ST_partstat * STps;
int dev = TAPE_NR(inode->i_rdev);
int mode = TAPE_MODE(inode->i_rdev);
if (dev >= st_template.dev_max || !scsi_tapes[dev].device)
return (-ENXIO);
if( !scsi_block_when_processing_errors(scsi_tapes[dev].device) ) {
return -ENXIO;
}
STp = &(scsi_tapes[dev]);
if (STp->in_use) {
#if DEBUG
printk(ST_DEB_MSG "st%d: Device already in use.\n", dev);
#endif
return (-EBUSY);
}
STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
if (mode != STp->current_mode) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Mode change from %d to %d.\n",
dev, STp->current_mode, mode);
#endif
new_session = TRUE;
STp->current_mode = mode;
}
STm = &(STp->modes[STp->current_mode]);
/* Allocate a buffer for this user */
need_dma_buffer = STp->restr_dma;
for (i=0; i < st_nbr_buffers; i++)
if (!st_buffers[i]->in_use &&
(!need_dma_buffer || st_buffers[i]->dma))
break;
if (i >= st_nbr_buffers) {
STp->buffer = new_tape_buffer(FALSE, need_dma_buffer);
if (STp->buffer == NULL) {
printk(KERN_WARNING "st%d: Can't allocate tape buffer.\n", dev);
return (-EBUSY);
}
}
else
STp->buffer = st_buffers[i];
(STp->buffer)->in_use = 1;
(STp->buffer)->writing = 0;
(STp->buffer)->last_result_fatal = 0;
(STp->buffer)->use_sg = STp->device->host->sg_tablesize;
/* Compute the usable buffer size for this SCSI adapter */
if (!(STp->buffer)->use_sg)
(STp->buffer)->buffer_size = (STp->buffer)->sg[0].length;
else {
for (i=0, (STp->buffer)->buffer_size = 0; i < (STp->buffer)->use_sg &&
i < (STp->buffer)->sg_segs; i++)
(STp->buffer)->buffer_size += (STp->buffer)->sg[i].length;
}
flags = filp->f_flags;
STp->write_prot = ((flags & O_ACCMODE) == O_RDONLY);
STp->dirty = 0;
for (i=0; i < ST_NBR_PARTITIONS; i++) {
STps = &(STp->ps[i]);
STps->rw = ST_IDLE;
}
STp->ready = ST_READY;
STp->recover_count = 0;
#if DEBUG
STp->nbr_waits = STp->nbr_finished = 0;
#endif
if (scsi_tapes[dev].device->host->hostt->module)
__MOD_INC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
if(st_template.module)
__MOD_INC_USE_COUNT(st_template.module);
memset ((void *) &cmd[0], 0, 10);
cmd[0] = TEST_UNIT_READY;
SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->long_timeout, MAX_READY_RETRIES,
TRUE);
if (!SCpnt) {
if (scsi_tapes[dev].device->host->hostt->module)
__MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
if(st_template.module)
__MOD_DEC_USE_COUNT(st_template.module);
return (-EBUSY);
}
if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
(SCpnt->sense_buffer[2] & 0x0f) == UNIT_ATTENTION) { /* New media? */
memset ((void *) &cmd[0], 0, 10);
cmd[0] = TEST_UNIT_READY;
SCpnt = st_do_scsi(SCpnt, STp, cmd, 0, STp->long_timeout, MAX_READY_RETRIES,
TRUE);
(STp->device)->was_reset = 0;
STp->partition = STp->new_partition = 0;
if (STp->can_partitions)
STp->nbr_partitions = 1; /* This guess will be updated later if necessary */
for (i=0; i < ST_NBR_PARTITIONS; i++) {
STps = &(STp->ps[i]);
STps->rw = ST_IDLE;
STps->eof = ST_NOEOF;
STps->at_sm = 0;
STps->last_block_valid = FALSE;
STps->drv_block = 0;
STps->drv_file = 0 ;
}
new_session = TRUE;
}
if ((STp->buffer)->last_result_fatal != 0) {
if ((STp->device)->scsi_level >= SCSI_2 &&
(SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
(SCpnt->sense_buffer[2] & 0x0f) == NOT_READY &&
SCpnt->sense_buffer[12] == 0x3a) { /* Check ASC */
STp->ready = ST_NO_TAPE;
} else
STp->ready = ST_NOT_READY;
scsi_release_command(SCpnt);
SCpnt = NULL;
STp->density = 0; /* Clear the erroneous "residue" */
STp->write_prot = 0;
STp->block_size = 0;
STp->ps[0].drv_file = STp->ps[0].drv_block = 0;
STp->partition = STp->new_partition = 0;
STp->door_locked = ST_UNLOCKED;
STp->in_use = 1;
return 0;
}
if (STp->omit_blklims)
STp->min_block = STp->max_block = (-1);
else {
memset ((void *) &cmd[0], 0, 10);
cmd[0] = READ_BLOCK_LIMITS;
SCpnt = st_do_scsi(SCpnt, STp, cmd, 6, STp->timeout, MAX_READY_RETRIES, TRUE);
if (!SCpnt->result && !SCpnt->sense_buffer[0]) {
STp->max_block = ((STp->buffer)->b_data[1] << 16) |
((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
STp->min_block = ((STp->buffer)->b_data[4] << 8) |
(STp->buffer)->b_data[5];
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Block limits %d - %d bytes.\n", dev, STp->min_block,
STp->max_block);
#endif
}
else {
STp->min_block = STp->max_block = (-1);
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Can't read block limits.\n", dev);
#endif
}
}
memset ((void *) &cmd[0], 0, 10);
cmd[0] = MODE_SENSE;
cmd[4] = 12;
SCpnt = st_do_scsi(SCpnt, STp, cmd, 12, STp->timeout, MAX_READY_RETRIES, TRUE);
if ((STp->buffer)->last_result_fatal != 0) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: No Mode Sense.\n", dev);
#endif
STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
(STp->buffer)->last_result_fatal = 0; /* Prevent error propagation */
STp->drv_write_prot = 0;
}
else {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n",
dev,
(STp->buffer)->b_data[0], (STp->buffer)->b_data[1],
(STp->buffer)->b_data[2], (STp->buffer)->b_data[3]);
#endif
if ((STp->buffer)->b_data[3] >= 8) {
STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
STp->density = (STp->buffer)->b_data[4];
STp->block_size = (STp->buffer)->b_data[9] * 65536 +
(STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Density %x, tape length: %x, drv buffer: %d\n",
dev, STp->density, (STp->buffer)->b_data[5] * 65536 +
(STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7],
STp->drv_buffer);
#endif
}
if (STp->block_size > (STp->buffer)->buffer_size &&
!enlarge_buffer(STp->buffer, STp->block_size, STp->restr_dma)) {
printk(KERN_NOTICE "st%d: Blocksize %d too large for buffer.\n", dev,
STp->block_size);
(STp->buffer)->in_use = 0;
STp->buffer = NULL;
if (scsi_tapes[dev].device->host->hostt->module)
__MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
if(st_template.module)
__MOD_DEC_USE_COUNT(st_template.module);
return (-EIO);
}
STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
}
scsi_release_command(SCpnt);
SCpnt = NULL;
if (STp->block_size > 0)
(STp->buffer)->buffer_blocks = (STp->buffer)->buffer_size / STp->block_size;
else
(STp->buffer)->buffer_blocks = 1;
(STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Block size: %d, buffer size: %d (%d blocks).\n", dev,
STp->block_size, (STp->buffer)->buffer_size,
(STp->buffer)->buffer_blocks);
#endif
if (STp->drv_write_prot) {
STp->write_prot = 1;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Write protected\n", dev);
#endif
if ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR) {
(STp->buffer)->in_use = 0;
STp->buffer = NULL;
if (scsi_tapes[dev].device->host->hostt->module)
__MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
if(st_template.module)
__MOD_DEC_USE_COUNT(st_template.module);
return (-EROFS);
}
}
if (STp->can_partitions && STp->nbr_partitions < 1) {
/* This code is reached when the device is opened for the first time
after the driver has been initialized with tape in the drive and the
partition support has been enabled. */
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Updating partition number in status.\n", dev);
#endif
if ((STp->partition = find_partition(inode)) < 0) {
(STp->buffer)->in_use = 0;
STp->buffer = NULL;
if (scsi_tapes[dev].device->host->hostt->module)
__MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
if(st_template.module)
__MOD_DEC_USE_COUNT(st_template.module);
return STp->partition;
}
STp->new_partition = STp->partition;
STp->nbr_partitions = 1; /* This guess will be updated when necessary */
}
if (new_session) { /* Change the drive parameters for the new mode */
STp->density_changed = STp->blksize_changed = FALSE;
STp->compression_changed = FALSE;
if (!(STm->defaults_for_writes) &&
(i = set_mode_densblk(inode, STp, STm)) < 0) {
(STp->buffer)->in_use = 0;
STp->buffer = NULL;
if (scsi_tapes[dev].device->host->hostt->module)
__MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
if(st_template.module)
__MOD_DEC_USE_COUNT(st_template.module);
return i;
}
if (STp->default_drvbuffer != 0xff) {
if (st_int_ioctl(inode, MTSETDRVBUFFER, STp->default_drvbuffer))
printk(KERN_WARNING "st%d: Can't set default drive buffering to %d.\n",
dev, STp->default_drvbuffer);
}
}
STp->in_use = 1;
return 0;
}
/* Flush the tape buffer before close */
static int
scsi_tape_flush(struct file * filp)
{
int result = 0, result2;
static unsigned char cmd[10];
Scsi_Cmnd * SCpnt;
Scsi_Tape * STp;
ST_mode * STm;
ST_partstat * STps;
struct inode *inode = filp->f_dentry->d_inode;
kdev_t devt = inode->i_rdev;
int dev;
if (filp->f_count > 1)
return 0;
dev = TAPE_NR(devt);
STp = &(scsi_tapes[dev]);
STm = &(STp->modes[STp->current_mode]);
STps = &(STp->ps[STp->partition]);
if (STp->can_partitions &&
(result = update_partition(inode)) < 0) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: update_partition at close failed.\n", dev);
#endif
goto out;
}
if ( STps->rw == ST_WRITING && !(STp->device)->was_reset) {
result = flush_write_buffer(STp);
#if DEBUG
if (debugging) {
printk(ST_DEB_MSG "st%d: File length %ld bytes.\n",
dev, (long)(filp->f_pos));
printk(ST_DEB_MSG "st%d: Async write waits %d, finished %d.\n",
dev, STp->nbr_waits, STp->nbr_finished);
}
#endif
if (result == 0 || result == (-ENOSPC)) {
memset(cmd, 0, 10);
cmd[0] = WRITE_FILEMARKS;
cmd[4] = 1 + STp->two_fm;
SCpnt = st_do_scsi(NULL, STp, cmd, 0, STp->timeout, MAX_WRITE_RETRIES,
TRUE);
if (!SCpnt)
goto out;
if ((STp->buffer)->last_result_fatal != 0 &&
((SCpnt->sense_buffer[0] & 0x70) != 0x70 ||
(SCpnt->sense_buffer[2] & 0x4f) != 0x40 ||
((SCpnt->sense_buffer[0] & 0x80) != 0 &&
(SCpnt->sense_buffer[3] | SCpnt->sense_buffer[4] |
SCpnt->sense_buffer[5] |
SCpnt->sense_buffer[6]) == 0))) {
/* Filter out successful write at EOM */
scsi_release_command(SCpnt);
SCpnt = NULL;
printk(KERN_ERR "st%d: Error on write filemark.\n", dev);
if (result == 0)
result = (-EIO);
}
else {
scsi_release_command(SCpnt);
SCpnt = NULL;
if (STps->drv_file >= 0)
STps->drv_file++ ;
STps->drv_block = 0;
if (STp->two_fm)
cross_eof(STp, FALSE);
STps->eof = ST_FM;
}
}
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Buffer flushed, %d EOF(s) written\n",
dev, cmd[4]);
#endif
}
else if (!STp->rew_at_close) {
STps = &(STp->ps[STp->partition]);
if (!STm->sysv || STps->rw != ST_READING) {
if (STp->can_bsr)
result = flush_buffer(inode, filp, 0);
else if (STps->eof == ST_FM_HIT) {
result = cross_eof(STp, FALSE);
if (result) {
if (STps->drv_file >= 0)
STps->drv_file++;
STps->drv_block = 0;
STps->eof = ST_FM;
}
else
STps->eof = ST_NOEOF;
}
}
else if ((STps->eof == ST_NOEOF &&
!(result = cross_eof(STp, TRUE))) ||
STps->eof == ST_FM_HIT) {
if (STps->drv_file >= 0)
STps->drv_file++;
STps->drv_block = 0;
STps->eof = ST_FM;
}
}
out:
if (STp->rew_at_close) {
result2 = st_int_ioctl(inode, MTREW, 1);
if (result == 0)
result = result2;
}
return result;
}
/* Close the device and release it */
static int
scsi_tape_close(struct inode * inode, struct file * filp)
{
int result = 0;
Scsi_Tape * STp;
kdev_t devt = inode->i_rdev;
int dev;
dev = TAPE_NR(devt);
STp = &(scsi_tapes[dev]);
if (STp->door_locked == ST_LOCKED_AUTO)
st_int_ioctl(inode, MTUNLOCK, 0);
if (STp->buffer != NULL) {
normalize_buffer(STp->buffer);
(STp->buffer)->in_use = 0;
}
STp->in_use = 0;
if (scsi_tapes[dev].device->host->hostt->module)
__MOD_DEC_USE_COUNT(scsi_tapes[dev].device->host->hostt->module);
if(st_template.module)
__MOD_DEC_USE_COUNT(st_template.module);
return result;
}
/* Write command */
static ssize_t
st_write(struct file * filp, const char * buf, size_t count, loff_t *ppos)
{
struct inode *inode = filp->f_dentry->d_inode;
ssize_t total;
ssize_t i, do_count, blks, retval, transfer;
int write_threshold;
int doing_write = 0;
static unsigned char cmd[10];
const char *b_point;
Scsi_Cmnd * SCpnt = NULL;
Scsi_Tape * STp;
ST_mode * STm;
ST_partstat * STps;
int dev = TAPE_NR(inode->i_rdev);
STp = &(scsi_tapes[dev]);
/*
* If we are in the middle of error recovery, don't let anyone
* else try and use this device. Also, if error recovery fails, it
* may try and take the device offline, in which case all further
* access to the device is prohibited.
*/
if( !scsi_block_when_processing_errors(STp->device) ) {
return -ENXIO;
}
if (ppos != &filp->f_pos) {
/* "A request was outside the capabilities of the device." */
return -ENXIO;
}
if (STp->ready != ST_READY) {
if (STp->ready == ST_NO_TAPE)
return (-ENOMEDIUM);
else
return (-EIO);
}
STm = &(STp->modes[STp->current_mode]);
if (!STm->defined)
return (-ENXIO);
if (count == 0)
return 0;
/*
* If there was a bus reset, block further access
* to this device.
*/
if( STp->device->was_reset )
return (-EIO);
#if DEBUG
if (!STp->in_use) {
printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
return (-EIO);
}
#endif
/* Write must be integral number of blocks */
if (STp->block_size != 0 && (count % STp->block_size) != 0)
return (-EIO);
if (STp->can_partitions &&
(retval = update_partition(inode)) < 0)
return retval;
STps = &(STp->ps[STp->partition]);
if (STp->write_prot)
return (-EACCES);
if (STp->block_size == 0 &&
count > (STp->buffer)->buffer_size &&
!enlarge_buffer(STp->buffer, count, STp->restr_dma))
return (-EOVERFLOW);
if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
!st_int_ioctl(inode, MTLOCK, 0))
STp->door_locked = ST_LOCKED_AUTO;
if (STps->rw == ST_READING) {
retval = flush_buffer(inode, filp, 0);
if (retval)
return retval;
STps->rw = ST_WRITING;
}
else if (STps->rw != ST_WRITING &&
STps->drv_file == 0 && STps->drv_block == 0) {
if ((retval = set_mode_densblk(inode, STp, STm)) < 0)
return retval;
if (STm->default_compression != ST_DONT_TOUCH &&
!(STp->compression_changed)) {
if (st_compression(STp, (STm->default_compression == ST_YES))) {
printk(KERN_WARNING "st%d: Can't set default compression.\n",
dev);
if (modes_defined)
return (-EINVAL);
}
}
}
if ((STp->buffer)->writing) {
write_behind_check(STp);
if ((STp->buffer)->last_result_fatal) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Async write error (write) %x.\n", dev,
(STp->buffer)->last_result);
#endif
if ((STp->buffer)->last_result == INT_MAX)
STps->eof = ST_EOM_OK;
else
STps->eof = ST_EOM_ERROR;
}
}
if (STps->eof == ST_EOM_OK)
return (-ENOSPC);
else if (STps->eof == ST_EOM_ERROR)
return (-EIO);
/* Check the buffer readability in cases where copy_user might catch
the problems after some tape movement. */
if (STp->block_size != 0 &&
(copy_from_user(&i, buf, 1) != 0 ||
copy_from_user(&i, buf + count - 1, 1) != 0))
return (-EFAULT);
if (!STm->do_buffer_writes) {
#if 0
if (STp->block_size != 0 && (count % STp->block_size) != 0)
return (-EIO); /* Write must be integral number of blocks */
#endif
write_threshold = 1;
}
else
write_threshold = (STp->buffer)->buffer_blocks * STp->block_size;
if (!STm->do_async_writes)
write_threshold--;
total = count;
memset(cmd, 0, 10);
cmd[0] = WRITE_6;
cmd[1] = (STp->block_size != 0);
STps->rw = ST_WRITING;
b_point = buf;
while ((STp->block_size == 0 && !STm->do_async_writes && count > 0) ||
(STp->block_size != 0 &&
(STp->buffer)->buffer_bytes + count > write_threshold))
{
doing_write = 1;
if (STp->block_size == 0)
do_count = count;
else {
do_count = (STp->buffer)->buffer_blocks * STp->block_size -
(STp->buffer)->buffer_bytes;
if (do_count > count)
do_count = count;
}
i = append_to_buffer(b_point, STp->buffer, do_count);
if (i) {
if (SCpnt != NULL) {
scsi_release_command(SCpnt);
SCpnt = NULL;
}
return i;
}
if (STp->block_size == 0)
blks = transfer = do_count;
else {
blks = (STp->buffer)->buffer_bytes /
STp->block_size;
transfer = blks * STp->block_size;
}
cmd[2] = blks >> 16;
cmd[3] = blks >> 8;
cmd[4] = blks;
SCpnt = st_do_scsi(SCpnt, STp, cmd, transfer, STp->timeout,
MAX_WRITE_RETRIES, TRUE);
if (!SCpnt)
return (-EBUSY);
if ((STp->buffer)->last_result_fatal != 0) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Error on write:\n", dev);
#endif
if ((SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
(SCpnt->sense_buffer[2] & 0x40)) {
if (STp->block_size != 0 && (SCpnt->sense_buffer[0] & 0x80) != 0)
transfer = (SCpnt->sense_buffer[3] << 24) |
(SCpnt->sense_buffer[4] << 16) |
(SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
else if (STp->block_size == 0 &&
(SCpnt->sense_buffer[2] & 0x0f) == VOLUME_OVERFLOW)
transfer = do_count;
else
transfer = 0;
if (STp->block_size != 0)
transfer *= STp->block_size;
if (transfer <= do_count) {
filp->f_pos += do_count - transfer;
count -= do_count - transfer;
if (STps->drv_block >= 0) {
if (STp->block_size == 0 && transfer < do_count)
STps->drv_block++;
else if (STp->block_size != 0)
STps->drv_block += (do_count - transfer) / STp->block_size;
}
STps->eof = ST_EOM_OK;
retval = (-ENOSPC); /* EOM within current request */
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: EOM with %d bytes unwritten.\n",
dev, transfer);
#endif
}
else {
STps->eof = ST_EOM_ERROR;
STps->drv_block = (-1); /* Too cautious? */
retval = (-EIO); /* EOM for old data */
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: EOM with lost data.\n", dev);
#endif
}
}
else {
STps->drv_block = (-1); /* Too cautious? */
retval = (-EIO);
}
scsi_release_command(SCpnt);
SCpnt = NULL;
(STp->buffer)->buffer_bytes = 0;
STp->dirty = 0;
if (count < total)
return total - count;
else
return retval;
}
filp->f_pos += do_count;
b_point += do_count;
count -= do_count;
if (STps->drv_block >= 0) {
if (STp->block_size == 0)
STps->drv_block++;
else
STps->drv_block += blks;
}
(STp->buffer)->buffer_bytes = 0;
STp->dirty = 0;
}
if (count != 0) {
STp->dirty = 1;
i = append_to_buffer(b_point, STp->buffer, count);
if (i) {
if (SCpnt != NULL) {
scsi_release_command(SCpnt);
SCpnt = NULL;
}
return i;
}
filp->f_pos += count;
count = 0;
}
if (doing_write && (STp->buffer)->last_result_fatal != 0) {
scsi_release_command(SCpnt);
SCpnt = NULL;
return (STp->buffer)->last_result_fatal;
}
if (STm->do_async_writes &&
(((STp->buffer)->buffer_bytes >= STp->write_threshold &&
(STp->buffer)->buffer_bytes >= STp->block_size) ||
STp->block_size == 0) ) {
/* Schedule an asynchronous write */
if (STp->block_size == 0)
(STp->buffer)->writing = (STp->buffer)->buffer_bytes;
else
(STp->buffer)->writing = ((STp->buffer)->buffer_bytes /
STp->block_size) * STp->block_size;
STp->dirty = !((STp->buffer)->writing ==
(STp->buffer)->buffer_bytes);
if (STp->block_size == 0)
blks = (STp->buffer)->writing;
else
blks = (STp->buffer)->writing / STp->block_size;
cmd[2] = blks >> 16;
cmd[3] = blks >> 8;
cmd[4] = blks;
#if DEBUG
STp->write_pending = 1;
#endif
SCpnt = st_do_scsi(SCpnt, STp, cmd, (STp->buffer)->writing, STp->timeout,
MAX_WRITE_RETRIES, FALSE);
if (SCpnt == NULL)
return (-EIO);
}
else if (SCpnt != NULL) {
scsi_release_command(SCpnt);
SCpnt = NULL;
}
STps->at_sm &= (total == 0);
if (total > 0)
STps->eof = ST_NOEOF;
return( total);
}
/* Read data from the tape. Returns zero in the normal case, one if the
eof status has changed, and the negative error code in case of a
fatal error. Otherwise updates the buffer and the eof state. */
static long
read_tape(struct inode *inode, long count, Scsi_Cmnd **aSCpnt)
{
int transfer, blks, bytes;
static unsigned char cmd[10];
Scsi_Cmnd *SCpnt;
Scsi_Tape *STp;
ST_mode * STm;
ST_partstat * STps;
int dev = TAPE_NR(inode->i_rdev);
int retval = 0;
if (count == 0)
return 0;
STp = &(scsi_tapes[dev]);
STm = &(STp->modes[STp->current_mode]);
STps = &(STp->ps[STp->partition]);
if (STps->eof == ST_FM_HIT)
return 1;
memset(cmd, 0, 10);
cmd[0] = READ_6;
cmd[1] = (STp->block_size != 0);
if (STp->block_size == 0)
blks = bytes = count;
else {
if (STm->do_read_ahead) {
blks = (STp->buffer)->buffer_blocks;
bytes = blks * STp->block_size;
}
else {
bytes = count;
if (bytes > (STp->buffer)->buffer_size)
bytes = (STp->buffer)->buffer_size;
blks = bytes / STp->block_size;
bytes = blks * STp->block_size;
}
}
cmd[2] = blks >> 16;
cmd[3] = blks >> 8;
cmd[4] = blks;
SCpnt = *aSCpnt;
SCpnt = st_do_scsi(SCpnt, STp, cmd, bytes, STp->timeout, MAX_RETRIES, TRUE);
*aSCpnt = SCpnt;
if (!SCpnt)
return (-EBUSY);
(STp->buffer)->read_pointer = 0;
STps->at_sm = 0;
/* Something to check */
if ((STp->buffer)->last_result_fatal) {
retval = 1;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
dev,
SCpnt->sense_buffer[0], SCpnt->sense_buffer[1],
SCpnt->sense_buffer[2], SCpnt->sense_buffer[3],
SCpnt->sense_buffer[4], SCpnt->sense_buffer[5],
SCpnt->sense_buffer[6], SCpnt->sense_buffer[7]);
#endif
if ((SCpnt->sense_buffer[0] & 0x70) == 0x70) { /* extended sense */
if ((SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
SCpnt->sense_buffer[2] &= 0xcf; /* No need for EOM in this case */
if ((SCpnt->sense_buffer[2] & 0xe0) != 0) { /* EOF, EOM, or ILI */
/* Compute the residual count */
if ((SCpnt->sense_buffer[0] & 0x80) != 0)
transfer = (SCpnt->sense_buffer[3] << 24) |
(SCpnt->sense_buffer[4] << 16) |
(SCpnt->sense_buffer[5] << 8) | SCpnt->sense_buffer[6];
else
transfer = 0;
if (STp->block_size == 0 &&
(SCpnt->sense_buffer[2] & 0x0f) == MEDIUM_ERROR)
transfer = bytes;
if (SCpnt->sense_buffer[2] & 0x20) { /* ILI */
if (STp->block_size == 0) {
if (transfer <= 0)
transfer = 0;
(STp->buffer)->buffer_bytes = bytes - transfer;
}
else {
scsi_release_command(SCpnt);
SCpnt = *aSCpnt = NULL;
if (transfer == blks) { /* We did not get anything, error */
printk(KERN_NOTICE "st%d: Incorrect block size.\n", dev);
if (STps->drv_block >= 0)
STps->drv_block += blks - transfer + 1;
st_int_ioctl(inode, MTBSR, 1);
return (-EIO);
}
/* We have some data, deliver it */
(STp->buffer)->buffer_bytes = (blks - transfer) *
STp->block_size;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG
"st%d: ILI but enough data received %ld %d.\n",
dev, count, (STp->buffer)->buffer_bytes);
#endif
if (STps->drv_block >= 0)
STps->drv_block += 1;
if (st_int_ioctl(inode, MTBSR, 1))
return (-EIO);
}
}
else if (SCpnt->sense_buffer[2] & 0x80) { /* FM overrides EOM */
if (STps->eof != ST_FM_HIT)
STps->eof = ST_FM_HIT;
else
STps->eof = ST_EOD_2;
if (STp->block_size == 0)
(STp->buffer)->buffer_bytes = 0;
else
(STp->buffer)->buffer_bytes =
bytes - transfer * STp->block_size;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG
"st%d: EOF detected (%d bytes read).\n",
dev, (STp->buffer)->buffer_bytes);
#endif
}
else if (SCpnt->sense_buffer[2] & 0x40) {
if (STps->eof == ST_FM)
STps->eof = ST_EOD_1;
else
STps->eof = ST_EOM_OK;
if (STp->block_size == 0)
(STp->buffer)->buffer_bytes = bytes - transfer;
else
(STp->buffer)->buffer_bytes =
bytes - transfer * STp->block_size;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: EOM detected (%d bytes read).\n",
dev, (STp->buffer)->buffer_bytes);
#endif
}
} /* end of EOF, EOM, ILI test */
else { /* nonzero sense key */
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Tape error while reading.\n", dev);
#endif
STps->drv_block = (-1);
if (STps->eof == ST_FM &&
(SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG
"st%d: Zero returned for first BLANK CHECK after EOF.\n",
dev);
#endif
STps->eof = ST_EOD_2; /* First BLANK_CHECK after FM */
}
else /* Some other extended sense code */
retval = (-EIO);
}
} /* End of extended sense test */
else { /* Non-extended sense */
retval = (STp->buffer)->last_result_fatal;
}
} /* End of error handling */
else /* Read successful */
(STp->buffer)->buffer_bytes = bytes;
if (STps->drv_block >= 0) {
if (STp->block_size == 0)
STps->drv_block++;
else
STps->drv_block += (STp->buffer)->buffer_bytes / STp->block_size;
}
return retval;
}
/* Read command */
static ssize_t
st_read(struct file * filp, char * buf, size_t count, loff_t *ppos)
{
struct inode * inode = filp->f_dentry->d_inode;
ssize_t total;
ssize_t i, transfer;
int special;
Scsi_Cmnd * SCpnt = NULL;
Scsi_Tape * STp;
ST_mode * STm;
ST_partstat * STps;
int dev = TAPE_NR(inode->i_rdev);
STp = &(scsi_tapes[dev]);
/*
* If we are in the middle of error recovery, don't let anyone
* else try and use this device. Also, if error recovery fails, it
* may try and take the device offline, in which case all further
* access to the device is prohibited.
*/
if( !scsi_block_when_processing_errors(STp->device) ) {
return -ENXIO;
}
if (ppos != &filp->f_pos) {
/* "A request was outside the capabilities of the device." */
return -ENXIO;
}
if (STp->ready != ST_READY) {
if (STp->ready == ST_NO_TAPE)
return (-ENOMEDIUM);
else
return (-EIO);
}
STm = &(STp->modes[STp->current_mode]);
if (!STm->defined)
return (-ENXIO);
#if DEBUG
if (!STp->in_use) {
printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
return (-EIO);
}
#endif
if (STp->can_partitions &&
(total = update_partition(inode)) < 0)
return total;
if (STp->block_size == 0 &&
count > (STp->buffer)->buffer_size &&
!enlarge_buffer(STp->buffer, count, STp->restr_dma))
return (-EOVERFLOW);
if (!(STm->do_read_ahead) && STp->block_size != 0 &&
(count % STp->block_size) != 0)
return (-EIO); /* Read must be integral number of blocks */
if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
!st_int_ioctl(inode, MTLOCK, 0))
STp->door_locked = ST_LOCKED_AUTO;
STps = &(STp->ps[STp->partition]);
if (STps->rw == ST_WRITING) {
transfer = flush_buffer(inode, filp, 0);
if (transfer)
return transfer;
STps->rw = ST_READING;
}
#if DEBUG
if (debugging && STps->eof != ST_NOEOF)
printk(ST_DEB_MSG "st%d: EOF/EOM flag up (%d). Bytes %d\n", dev,
STps->eof, (STp->buffer)->buffer_bytes);
#endif
if ((STp->buffer)->buffer_bytes == 0 &&
STps->eof >= ST_EOD_1) {
if (STps->eof < ST_EOD) {
STps->eof += 1;
return 0;
}
return (-EIO); /* EOM or Blank Check */
}
/* Check the buffer writability before any tape movement. Don't alter
buffer data. */
if (copy_from_user(&i, buf, 1) != 0 ||
copy_to_user(buf, &i, 1) != 0 ||
copy_from_user(&i, buf + count - 1, 1) != 0 ||
copy_to_user(buf + count - 1, &i, 1) != 0)
return (-EFAULT);
STps->rw = ST_READING;
/* Loop until enough data in buffer or a special condition found */
for (total = 0, special = 0; total < count && !special; ) {
/* Get new data if the buffer is empty */
if ((STp->buffer)->buffer_bytes == 0) {
special = read_tape(inode, count - total, &SCpnt);
if (special < 0) { /* No need to continue read */
if (SCpnt != NULL) {
scsi_release_command(SCpnt);
}
return special;
}
}
/* Move the data from driver buffer to user buffer */
if ((STp->buffer)->buffer_bytes > 0) {
#if DEBUG
if (debugging && STps->eof != ST_NOEOF)
printk(ST_DEB_MSG "st%d: EOF up (%d). Left %d, needed %d.\n", dev,
STps->eof, (STp->buffer)->buffer_bytes, count - total);
#endif
transfer = (STp->buffer)->buffer_bytes < count - total ?
(STp->buffer)->buffer_bytes : count - total;
i = from_buffer(STp->buffer, buf, transfer);
if (i) {
if (SCpnt != NULL) {
scsi_release_command(SCpnt);
SCpnt = NULL;
}
return i;
}
filp->f_pos += transfer;
buf += transfer;
total += transfer;
}
if (STp->block_size == 0)
break; /* Read only one variable length block */
} /* for (total = 0, special = 0; total < count && !special; ) */
if (SCpnt != NULL) {
scsi_release_command(SCpnt);
SCpnt = NULL;
}
/* Change the eof state if no data from tape or buffer */
if (total == 0) {
if (STps->eof == ST_FM_HIT) {
STps->eof = ST_FM;
STps->drv_block = 0;
if (STps->drv_file >= 0)
STps->drv_file++;
}
else if (STps->eof == ST_EOD_1) {
STps->eof = ST_EOD_2;
STps->drv_block = 0;
if (STps->drv_file >= 0)
STps->drv_file++;
}
else if (STps->eof == ST_EOD_2)
STps->eof = ST_EOD;
}
else if (STps->eof == ST_FM)
STps->eof = ST_NOEOF;
return total;
}
/* Set the driver options */
static void
st_log_options(Scsi_Tape *STp, ST_mode *STm, int dev)
{
printk(KERN_INFO
"st%d: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n",
dev, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes,
STm->do_read_ahead);
printk(KERN_INFO
"st%d: can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n",
dev, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock);
printk(KERN_INFO
"st%d: defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n",
dev, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions,
STp->scsi2_logical);
printk(KERN_INFO
"st%d: sysv: %d\n", dev, STm->sysv);
#if DEBUG
printk(KERN_INFO
"st%d: debugging: %d\n",
dev, debugging);
#endif
}
static int
st_set_options(struct inode * inode, long options)
{
int value;
long code;
Scsi_Tape *STp;
ST_mode *STm;
int dev = TAPE_NR(inode->i_rdev);
STp = &(scsi_tapes[dev]);
STm = &(STp->modes[STp->current_mode]);
if (!STm->defined) {
memcpy(STm, &(STp->modes[0]), sizeof(ST_mode));
modes_defined = TRUE;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Initialized mode %d definition from mode 0\n",
dev, STp->current_mode);
#endif
}
code = options & MT_ST_OPTIONS;
if (code == MT_ST_BOOLEANS) {
STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0;
STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0;
STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0;
STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0;
STp->two_fm = (options & MT_ST_TWO_FM) != 0;
STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
if ((STp->device)->scsi_level >= SCSI_2)
STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
STm->sysv = (options & MT_ST_SYSV) != 0;
#if DEBUG
debugging = (options & MT_ST_DEBUGGING) != 0;
#endif
st_log_options(STp, STm, dev);
}
else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) {
value = (code == MT_ST_SETBOOLEANS);
if ((options & MT_ST_BUFFER_WRITES) != 0)
STm->do_buffer_writes = value;
if ((options & MT_ST_ASYNC_WRITES) != 0)
STm->do_async_writes = value;
if ((options & MT_ST_DEF_WRITES) != 0)
STm->defaults_for_writes = value;
if ((options & MT_ST_READ_AHEAD) != 0)
STm->do_read_ahead = value;
if ((options & MT_ST_TWO_FM) != 0)
STp->two_fm = value;
if ((options & MT_ST_FAST_MTEOM) != 0)
STp->fast_mteom = value;
if ((options & MT_ST_AUTO_LOCK) != 0)
STp->do_auto_lock = value;
if ((options & MT_ST_CAN_BSR) != 0)
STp->can_bsr = value;
if ((options & MT_ST_NO_BLKLIMS) != 0)
STp->omit_blklims = value;
if ((STp->device)->scsi_level >= SCSI_2 &&
(options & MT_ST_CAN_PARTITIONS) != 0)
STp->can_partitions = value;
if ((options & MT_ST_SCSI2LOGICAL) != 0)
STp->scsi2_logical = value;
if ((options & MT_ST_SYSV) != 0)
STm->sysv = value;
#if DEBUG
if ((options & MT_ST_DEBUGGING) != 0)
debugging = value;
#endif
st_log_options(STp, STm, dev);
}
else if (code == MT_ST_WRITE_THRESHOLD) {
value = (options & ~MT_ST_OPTIONS) * ST_KILOBYTE;
if (value < 1 || value > st_buffer_size) {
printk(KERN_WARNING "st%d: Write threshold %d too small or too large.\n",
dev, value);
return (-EIO);
}
STp->write_threshold = value;
printk(KERN_INFO "st%d: Write threshold set to %d bytes.\n",
dev, value);
}
else if (code == MT_ST_DEF_BLKSIZE) {
value = (options & ~MT_ST_OPTIONS);
if (value == ~MT_ST_OPTIONS) {
STm->default_blksize = (-1);
printk(KERN_INFO "st%d: Default block size disabled.\n", dev);
}
else {
STm->default_blksize = value;
printk(KERN_INFO "st%d: Default block size set to %d bytes.\n",
dev, STm->default_blksize);
}
}
else if (code == MT_ST_TIMEOUTS) {
value = (options & ~MT_ST_OPTIONS);
if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) {
STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
printk(KERN_INFO "st%d: Long timeout set to %d seconds.\n", dev,
(value & ~MT_ST_SET_LONG_TIMEOUT));
}
else {
STp->timeout = value * HZ;
printk(KERN_INFO "st%d: Normal timeout set to %d seconds.\n", dev,
value);
}
}
else if (code == MT_ST_DEF_OPTIONS) {
code = (options & ~MT_ST_CLEAR_DEFAULT);
value = (options & MT_ST_CLEAR_DEFAULT);
if (code == MT_ST_DEF_DENSITY) {
if (value == MT_ST_CLEAR_DEFAULT) {
STm->default_density = (-1);
printk(KERN_INFO "st%d: Density default disabled.\n", dev);
}
else {
STm->default_density = value & 0xff;
printk(KERN_INFO "st%d: Density default set to %x\n",
dev, STm->default_density);
}
}
else if (code == MT_ST_DEF_DRVBUFFER) {
if (value == MT_ST_CLEAR_DEFAULT) {
STp->default_drvbuffer = 0xff;
printk(KERN_INFO "st%d: Drive buffer default disabled.\n", dev);
}
else {
STp->default_drvbuffer = value & 7;
printk(KERN_INFO "st%d: Drive buffer default set to %x\n",
dev, STp->default_drvbuffer);
}
}
else if (code == MT_ST_DEF_COMPRESSION) {
if (value == MT_ST_CLEAR_DEFAULT) {
STm->default_compression = ST_DONT_TOUCH;
printk(KERN_INFO "st%d: Compression default disabled.\n", dev);
}
else {
STm->default_compression = (value & 1 ? ST_YES : ST_NO);
printk(KERN_INFO "st%d: Compression default set to %x\n",
dev, (value & 1));
}
}
}
else
return (-EIO);
return 0;
}
#define COMPRESSION_PAGE 0x0f
#define COMPRESSION_PAGE_LENGTH 16
#define MODE_HEADER_LENGTH 4
#define DCE_MASK 0x80
#define DCC_MASK 0x40
#define RED_MASK 0x60
/* Control the compression with mode page 15. Algorithm not changed if zero. */
static int
st_compression(Scsi_Tape * STp, int state)
{
int dev;
unsigned char cmd[10];
Scsi_Cmnd * SCpnt = NULL;
if (STp->ready != ST_READY)
return (-EIO);
/* Read the current page contents */
memset(cmd, 0, 10);
cmd[0] = MODE_SENSE;
cmd[1] = 8;
cmd[2] = COMPRESSION_PAGE;
cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout, 0, TRUE);
if (SCpnt == NULL)
return (-EBUSY);
dev = TAPE_NR(SCpnt->request.rq_dev);
if ((STp->buffer)->last_result_fatal != 0) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Compression mode page not supported.\n", dev);
#endif
scsi_release_command(SCpnt);
SCpnt = NULL;
return (-EIO);
}
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Compression state is %d.\n", dev,
((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCE_MASK ? 1 : 0));
#endif
/* Check if compression can be changed */
if (((STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] & DCC_MASK) == 0) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Compression not supported.\n", dev);
#endif
scsi_release_command(SCpnt);
SCpnt = NULL;
return (-EIO);
}
/* Do the change */
if (state)
(STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] |= DCE_MASK;
else
(STp->buffer)->b_data[MODE_HEADER_LENGTH + 2] &= ~DCE_MASK;
memset(cmd, 0, 10);
cmd[0] = MODE_SELECT;
cmd[1] = 0x10;
cmd[4] = COMPRESSION_PAGE_LENGTH + MODE_HEADER_LENGTH;
(STp->buffer)->b_data[0] = 0; /* Reserved data length */
(STp->buffer)->b_data[1] = 0; /* Reserved media type byte */
(STp->buffer)->b_data[MODE_HEADER_LENGTH] &= 0x3f;
SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->timeout, 0, TRUE);
if ((STp->buffer)->last_result_fatal != 0) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Compression change failed.\n", dev);
#endif
scsi_release_command(SCpnt);
SCpnt = NULL;
return (-EIO);
}
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Compression state changed to %d.\n",
dev, state);
#endif
scsi_release_command(SCpnt);
SCpnt = NULL;
STp->compression_changed = TRUE;
return 0;
}
/* Internal ioctl function */
static int
st_int_ioctl(struct inode * inode,
unsigned int cmd_in, unsigned long arg)
{
int timeout;
long ltmp;
int i, ioctl_result;
int chg_eof = TRUE;
unsigned char cmd[10];
Scsi_Cmnd * SCpnt;
Scsi_Tape * STp;
ST_partstat * STps;
int fileno, blkno, at_sm, undone, datalen;
int dev = TAPE_NR(inode->i_rdev);
STp = &(scsi_tapes[dev]);
if (STp->ready != ST_READY && cmd_in != MTLOAD) {
if (STp->ready == ST_NO_TAPE)
return (-ENOMEDIUM);
else
return (-EIO);
}
timeout = STp->long_timeout;
STps = &(STp->ps[STp->partition]);
fileno = STps->drv_file;
blkno = STps->drv_block;
at_sm = STps->at_sm;
memset(cmd, 0, 10);
datalen = 0;
switch (cmd_in) {
case MTFSFM:
chg_eof = FALSE; /* Changed from the FSF after this */
case MTFSF:
cmd[0] = SPACE;
cmd[1] = 0x01; /* Space FileMarks */
cmd[2] = (arg >> 16);
cmd[3] = (arg >> 8);
cmd[4] = arg;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Spacing tape forward over %d filemarks.\n",
dev, cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
#endif
if (fileno >= 0)
fileno += arg;
blkno = 0;
at_sm &= (arg == 0);
break;
case MTBSFM:
chg_eof = FALSE; /* Changed from the FSF after this */
case MTBSF:
cmd[0] = SPACE;
cmd[1] = 0x01; /* Space FileMarks */
ltmp = (-arg);
cmd[2] = (ltmp >> 16);
cmd[3] = (ltmp >> 8);
cmd[4] = ltmp;
#if DEBUG
if (debugging) {
if (cmd[2] & 0x80)
ltmp = 0xff000000;
ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
printk(ST_DEB_MSG "st%d: Spacing tape backward over %ld filemarks.\n",
dev, (-ltmp));
}
#endif
if (fileno >= 0)
fileno -= arg;
blkno = (-1); /* We can't know the block number */
at_sm &= (arg == 0);
break;
case MTFSR:
cmd[0] = SPACE;
cmd[1] = 0x00; /* Space Blocks */
cmd[2] = (arg >> 16);
cmd[3] = (arg >> 8);
cmd[4] = arg;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Spacing tape forward %d blocks.\n", dev,
cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
#endif
if (blkno >= 0)
blkno += arg;
at_sm &= (arg == 0);
break;
case MTBSR:
cmd[0] = SPACE;
cmd[1] = 0x00; /* Space Blocks */
ltmp = (-arg);
cmd[2] = (ltmp >> 16);
cmd[3] = (ltmp >> 8);
cmd[4] = ltmp;
#if DEBUG
if (debugging) {
if (cmd[2] & 0x80)
ltmp = 0xff000000;
ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
printk(ST_DEB_MSG "st%d: Spacing tape backward %ld blocks.\n", dev, (-ltmp));
}
#endif
if (blkno >= 0)
blkno -= arg;
at_sm &= (arg == 0);
break;
case MTFSS:
cmd[0] = SPACE;
cmd[1] = 0x04; /* Space Setmarks */
cmd[2] = (arg >> 16);
cmd[3] = (arg >> 8);
cmd[4] = arg;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Spacing tape forward %d setmarks.\n", dev,
cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
#endif
if (arg != 0) {
blkno = fileno = (-1);
at_sm = 1;
}
break;
case MTBSS:
cmd[0] = SPACE;
cmd[1] = 0x04; /* Space Setmarks */
ltmp = (-arg);
cmd[2] = (ltmp >> 16);
cmd[3] = (ltmp >> 8);
cmd[4] = ltmp;
#if DEBUG
if (debugging) {
if (cmd[2] & 0x80)
ltmp = 0xff000000;
ltmp = ltmp | (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
printk(ST_DEB_MSG "st%d: Spacing tape backward %ld setmarks.\n",
dev, (-ltmp));
}
#endif
if (arg != 0) {
blkno = fileno = (-1);
at_sm = 1;
}
break;
case MTWEOF:
case MTWSM:
if (STp->write_prot)
return (-EACCES);
cmd[0] = WRITE_FILEMARKS;
if (cmd_in == MTWSM)
cmd[1] = 2;
cmd[2] = (arg >> 16);
cmd[3] = (arg >> 8);
cmd[4] = arg;
timeout = STp->timeout;
#if DEBUG
if (debugging) {
if (cmd_in == MTWEOF)
printk(ST_DEB_MSG "st%d: Writing %d filemarks.\n", dev,
cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
else
printk(ST_DEB_MSG "st%d: Writing %d setmarks.\n", dev,
cmd[2] * 65536 + cmd[3] * 256 + cmd[4]);
}
#endif
if (fileno >= 0)
fileno += arg;
blkno = 0;
at_sm = (cmd_in == MTWSM);
break;
case MTREW:
cmd[0] = REZERO_UNIT;
#if ST_NOWAIT
cmd[1] = 1; /* Don't wait for completion */
timeout = STp->timeout;
#endif
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Rewinding tape.\n", dev);
#endif
fileno = blkno = at_sm = 0 ;
break;
case MTOFFL:
case MTLOAD:
case MTUNLOAD:
cmd[0] = START_STOP;
if (cmd_in == MTLOAD)
cmd[4] |= 1;
/*
* If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A
*/
if (cmd_in != MTOFFL &&
arg >= 1 + MT_ST_HPLOADER_OFFSET
&& arg <= 6 + MT_ST_HPLOADER_OFFSET) {
#if DEBUG
if (debugging) {
printk(ST_DEB_MSG "st%d: Enhanced %sload slot %2ld.\n",
dev, (cmd[4]) ? "" : "un",
arg - MT_ST_HPLOADER_OFFSET);
}
#endif
cmd[3] = arg - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */
}
#if ST_NOWAIT
cmd[1] = 1; /* Don't wait for completion */
timeout = STp->timeout;
#else
timeout = STp->long_timeout;
#endif
#if DEBUG
if (debugging) {
if (cmd_in != MTLOAD)
printk(ST_DEB_MSG "st%d: Unloading tape.\n", dev);
else
printk(ST_DEB_MSG "st%d: Loading tape.\n", dev);
}
#endif
fileno = blkno = at_sm = 0 ;
break;
case MTNOP:
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: No op on tape.\n", dev);
#endif
return 0; /* Should do something ? */
break;
case MTRETEN:
cmd[0] = START_STOP;
#if ST_NOWAIT
cmd[1] = 1; /* Don't wait for completion */
timeout = STp->timeout;
#endif
cmd[4] = 3;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Retensioning tape.\n", dev);
#endif
fileno = blkno = at_sm = 0;
break;
case MTEOM:
if (!STp->fast_mteom) {
/* space to the end of tape */
ioctl_result = st_int_ioctl(inode, MTFSF, 0x3fff);
fileno = STps->drv_file;
if (STps->eof >= ST_EOD_1)
return 0;
/* The next lines would hide the number of spaced FileMarks
That's why I inserted the previous lines. I had no luck
with detecting EOM with FSF, so we go now to EOM.
Joerg Weule */
}
else
fileno = (-1);
cmd[0] = SPACE;
cmd[1] = 3;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Spacing to end of recorded medium.\n", dev);
#endif
blkno = 0;
at_sm = 0;
break;
case MTERASE:
if (STp->write_prot)
return (-EACCES);
cmd[0] = ERASE;
cmd[1] = 1; /* To the end of tape */
#if ST_NOWAIT
cmd[1] |= 2; /* Don't wait for completion */
timeout = STp->timeout;
#else
timeout = STp->long_timeout * 8;
#endif
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Erasing tape.\n", dev);
#endif
fileno = blkno = at_sm = 0 ;
break;
case MTLOCK:
chg_eof = FALSE;
cmd[0] = ALLOW_MEDIUM_REMOVAL;
cmd[4] = SCSI_REMOVAL_PREVENT;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Locking drive door.\n", dev);
#endif;
break;
case MTUNLOCK:
chg_eof = FALSE;
cmd[0] = ALLOW_MEDIUM_REMOVAL;
cmd[4] = SCSI_REMOVAL_ALLOW;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Unlocking drive door.\n", dev);
#endif;
break;
case MTSETBLK: /* Set block length */
case MTSETDENSITY: /* Set tape density */
case MTSETDRVBUFFER: /* Set drive buffering */
case SET_DENS_AND_BLK: /* Set density and block size */
chg_eof = FALSE;
if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
return (-EIO); /* Not allowed if data in buffer */
if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) &&
(arg & MT_ST_BLKSIZE_MASK) != 0 &&
((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
(arg & MT_ST_BLKSIZE_MASK) > STp->max_block ||
(arg & MT_ST_BLKSIZE_MASK) > st_buffer_size)) {
printk(KERN_WARNING "st%d: Illegal block size.\n", dev);
return (-EINVAL);
}
cmd[0] = MODE_SELECT;
cmd[4] = datalen = 12;
memset((STp->buffer)->b_data, 0, 12);
if (cmd_in == MTSETDRVBUFFER)
(STp->buffer)->b_data[2] = (arg & 7) << 4;
else
(STp->buffer)->b_data[2] =
STp->drv_buffer << 4;
(STp->buffer)->b_data[3] = 8; /* block descriptor length */
if (cmd_in == MTSETDENSITY) {
(STp->buffer)->b_data[4] = arg;
STp->density_changed = TRUE; /* At least we tried ;-) */
}
else if (cmd_in == SET_DENS_AND_BLK)
(STp->buffer)->b_data[4] = arg >> 24;
else
(STp->buffer)->b_data[4] = STp->density;
if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
ltmp = arg & MT_ST_BLKSIZE_MASK;
if (cmd_in == MTSETBLK)
STp->blksize_changed = TRUE; /* At least we tried ;-) */
}
else
ltmp = STp->block_size;
(STp->buffer)->b_data[9] = (ltmp >> 16);
(STp->buffer)->b_data[10] = (ltmp >> 8);
(STp->buffer)->b_data[11] = ltmp;
timeout = STp->timeout;
#if DEBUG
if (debugging) {
if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK)
printk(ST_DEB_MSG "st%d: Setting block size to %d bytes.\n", dev,
(STp->buffer)->b_data[9] * 65536 +
(STp->buffer)->b_data[10] * 256 +
(STp->buffer)->b_data[11]);
if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK)
printk(ST_DEB_MSG "st%d: Setting density code to %x.\n", dev,
(STp->buffer)->b_data[4]);
if (cmd_in == MTSETDRVBUFFER)
printk(ST_DEB_MSG "st%d: Setting drive buffer code to %d.\n", dev,
((STp->buffer)->b_data[2] >> 4) & 7);
}
#endif
break;
default:
return (-ENOSYS);
}
SCpnt = st_do_scsi(NULL, STp, cmd, datalen, timeout, MAX_RETRIES, TRUE);
if (!SCpnt)
return (-EBUSY);
ioctl_result = (STp->buffer)->last_result_fatal;
if (!ioctl_result) { /* SCSI command successful */
scsi_release_command(SCpnt);
SCpnt = NULL;
STps->drv_block = blkno;
STps->drv_file = fileno;
STps->at_sm = at_sm;
if (cmd_in == MTLOCK)
STp->door_locked = ST_LOCKED_EXPLICIT;
else if (cmd_in == MTUNLOCK)
STp->door_locked = ST_UNLOCKED;
if (cmd_in == MTBSFM)
ioctl_result = st_int_ioctl(inode, MTFSF, 1);
else if (cmd_in == MTFSFM)
ioctl_result = st_int_ioctl(inode, MTBSF, 1);
if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
STp->block_size = arg & MT_ST_BLKSIZE_MASK;
if (STp->block_size != 0)
(STp->buffer)->buffer_blocks =
(STp->buffer)->buffer_size / STp->block_size;
(STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
if (cmd_in == SET_DENS_AND_BLK)
STp->density = arg >> MT_ST_DENSITY_SHIFT;
}
else if (cmd_in == MTSETDRVBUFFER)
STp->drv_buffer = (arg & 7);
else if (cmd_in == MTSETDENSITY)
STp->density = arg;
if (cmd_in == MTEOM)
STps->eof = ST_EOD;
else if (cmd_in == MTFSF)
STps->eof = ST_FM;
else if (chg_eof)
STps->eof = ST_NOEOF;
if (cmd_in == MTOFFL || cmd_in == MTUNLOAD)
STp->rew_at_close = 0;
else if (cmd_in == MTLOAD) {
STp->rew_at_close = (MINOR(inode->i_rdev) & 0x80) == 0;
for (i=0; i < ST_NBR_PARTITIONS; i++) {
STp->ps[i].rw = ST_IDLE;
STp->ps[i].last_block_valid = FALSE;
}
STp->partition = 0;
}
} else { /* SCSI command was not completely successful. Don't return
from this block without releasing the SCSI command block! */
if (SCpnt->sense_buffer[2] & 0x40) {
if (cmd_in != MTBSF && cmd_in != MTBSFM &&
cmd_in != MTBSR && cmd_in != MTBSS)
STps->eof = ST_EOM_OK;
STps->drv_block = 0;
}
undone = (
(SCpnt->sense_buffer[3] << 24) +
(SCpnt->sense_buffer[4] << 16) +
(SCpnt->sense_buffer[5] << 8) +
SCpnt->sense_buffer[6] );
if (cmd_in == MTWEOF &&
(SCpnt->sense_buffer[0] & 0x70) == 0x70 &&
(SCpnt->sense_buffer[2] & 0x4f) == 0x40 &&
((SCpnt->sense_buffer[0] & 0x80) == 0 || undone == 0)) {
ioctl_result = 0; /* EOF written succesfully at EOM */
if (fileno >= 0)
fileno++;
STps->drv_file = fileno;
STps->eof = ST_NOEOF;
}
else if ( (cmd_in == MTFSF) || (cmd_in == MTFSFM) ) {
if (fileno >= 0)
STps->drv_file = fileno - undone ;
else
STps->drv_file = fileno;
STps->drv_block = 0;
STps->eof = ST_NOEOF;
}
else if ( (cmd_in == MTBSF) || (cmd_in == MTBSFM) ) {
if (fileno >= 0)
STps->drv_file = fileno + undone ;
else
STps->drv_file = fileno;
STps->drv_block = 0;
STps->eof = ST_NOEOF;
}
else if (cmd_in == MTFSR) {
if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
if (STps->drv_file >= 0)
STps->drv_file++;
STps->drv_block = 0;
STps->eof = ST_FM;
}
else {
if (blkno >= undone)
STps->drv_block = blkno - undone;
else
STps->drv_block = (-1);
STps->eof = ST_NOEOF;
}
}
else if (cmd_in == MTBSR) {
if (SCpnt->sense_buffer[2] & 0x80) { /* Hit filemark */
STps->drv_file--;
STps->drv_block = (-1);
}
else {
if (blkno >= 0)
STps->drv_block = blkno + undone;
else
STps->drv_block = (-1);
}
STps->eof = ST_NOEOF;
}
else if (cmd_in == MTEOM) {
STps->drv_file = (-1);
STps->drv_block = (-1);
STps->eof = ST_EOD;
}
else if (chg_eof)
STps->eof = ST_NOEOF;
if ((SCpnt->sense_buffer[2] & 0x0f) == BLANK_CHECK)
STps->eof = ST_EOD;
if (cmd_in == MTLOCK)
STp->door_locked = ST_LOCK_FAILS;
scsi_release_command(SCpnt);
SCpnt = NULL;
}
return ioctl_result;
}
/* Get the tape position. If bt == 2, arg points into a kernel space mt_loc
structure. */
static int
get_location(struct inode * inode, unsigned int *block, int *partition,
int logical)
{
Scsi_Tape *STp;
int dev = TAPE_NR(inode->i_rdev);
int result;
unsigned char scmd[10];
Scsi_Cmnd *SCpnt;
STp = &(scsi_tapes[dev]);
if (STp->ready != ST_READY)
return (-EIO);
memset (scmd, 0, 10);
if ((STp->device)->scsi_level < SCSI_2) {
scmd[0] = QFA_REQUEST_BLOCK;
scmd[4] = 3;
}
else {
scmd[0] = READ_POSITION;
if (!logical && !STp->scsi2_logical)
scmd[1] = 1;
}
SCpnt = st_do_scsi(NULL, STp, scmd, 20, STp->timeout, MAX_READY_RETRIES, TRUE);
if (!SCpnt)
return (-EBUSY);
if ((STp->buffer)->last_result_fatal != 0 ||
(STp->device->scsi_level >= SCSI_2 &&
((STp->buffer)->b_data[0] & 4) != 0)) {
*block = *partition = 0;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Can't read tape position.\n", dev);
#endif
result = (-EIO);
}
else {
result = 0;
if ((STp->device)->scsi_level < SCSI_2) {
*block = ((STp->buffer)->b_data[0] << 16)
+ ((STp->buffer)->b_data[1] << 8)
+ (STp->buffer)->b_data[2];
*partition = 0;
}
else {
*block = ((STp->buffer)->b_data[4] << 24)
+ ((STp->buffer)->b_data[5] << 16)
+ ((STp->buffer)->b_data[6] << 8)
+ (STp->buffer)->b_data[7];
*partition = (STp->buffer)->b_data[1];
if (((STp->buffer)->b_data[0] & 0x80) &&
(STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
}
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Got tape pos. blk %d part %d.\n", dev,
*block, *partition);
#endif
}
scsi_release_command(SCpnt);
SCpnt = NULL;
return result;
}
/* Set the tape block and partition. Negative partition means that only the
block should be set in vendor specific way. */
static int
set_location(struct inode * inode, unsigned int block, int partition,
int logical)
{
Scsi_Tape *STp;
ST_partstat *STps;
int dev = TAPE_NR(inode->i_rdev);
int result, p;
unsigned int blk;
int timeout;
unsigned char scmd[10];
Scsi_Cmnd *SCpnt;
STp = &(scsi_tapes[dev]);
if (STp->ready != ST_READY)
return (-EIO);
timeout = STp->long_timeout;
STps = &(STp->ps[STp->partition]);
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Setting block to %d and partition to %d.\n",
dev, block, partition);
if (partition < 0)
return (-EIO);
#endif
/* Update the location at the partition we are leaving */
if ((!STp->can_partitions && partition != 0) ||
partition >= ST_NBR_PARTITIONS)
return (-EINVAL);
if (partition != STp->partition) {
if (get_location(inode, &blk, &p, 1))
STps->last_block_valid = FALSE;
else {
STps->last_block_valid = TRUE;
STps->last_block_visited = blk;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Visited block %d for partition %d saved.\n",
dev, blk, STp->partition);
#endif
}
}
memset (scmd, 0, 10);
if ((STp->device)->scsi_level < SCSI_2) {
scmd[0] = QFA_SEEK_BLOCK;
scmd[2] = (block >> 16);
scmd[3] = (block >> 8);
scmd[4] = block;
scmd[5] = 0;
}
else {
scmd[0] = SEEK_10;
scmd[3] = (block >> 24);
scmd[4] = (block >> 16);
scmd[5] = (block >> 8);
scmd[6] = block;
if (!logical && !STp->scsi2_logical)
scmd[1] = 4;
if (STp->partition != partition) {
scmd[1] |= 2;
scmd[8] = partition;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Trying to change partition from %d to %d\n",
dev, STp->partition, partition);
#endif
}
}
#if ST_NOWAIT
scmd[1] |= 1; /* Don't wait for completion */
timeout = STp->timeout;
#endif
SCpnt = st_do_scsi(NULL, STp, scmd, 20, timeout, MAX_READY_RETRIES, TRUE);
if (!SCpnt)
return (-EBUSY);
STps->drv_block = STps->drv_file = (-1);
STps->eof = ST_NOEOF;
if ((STp->buffer)->last_result_fatal != 0) {
result = (-EIO);
if (STp->can_partitions &&
(STp->device)->scsi_level >= SCSI_2 &&
(p = find_partition(inode)) >= 0)
STp->partition = p;
}
else {
if (STp->can_partitions) {
STp->partition = partition;
STps = &(STp->ps[partition]);
if (!STps->last_block_valid ||
STps->last_block_visited != block) {
STps->at_sm = 0;
STps->rw = ST_IDLE;
}
}
else
STps->at_sm = 0;
if (block == 0)
STps->drv_block = STps->drv_file = 0;
result = 0;
}
scsi_release_command(SCpnt);
SCpnt = NULL;
return result;
}
/* Find the current partition number for the drive status. Called from open and
returns either partition number of negative error code. */
static int
find_partition(struct inode *inode)
{
int i, partition;
unsigned int block;
if ((i = get_location(inode, &block, &partition, 1)) < 0)
return i;
if (partition >= ST_NBR_PARTITIONS)
return (-EIO);
return partition;
}
/* Change the partition if necessary */
static int
update_partition(struct inode * inode)
{
int dev = TAPE_NR(inode->i_rdev);
Scsi_Tape *STp;
ST_partstat *STps;
STp = &(scsi_tapes[dev]);
if (STp->partition == STp->new_partition)
return 0;
STps = &(STp->ps[STp->new_partition]);
if (!STps->last_block_valid)
STps->last_block_visited = 0;
return set_location(inode, STps->last_block_visited, STp->new_partition, 1);
}
/* Functions for reading and writing the medium partition mode page. These
seem to work with Wangtek 6200HS and HP C1533A. */
#define PART_PAGE 0x11
#define PART_PAGE_LENGTH 10
/* Get the number of partitions on the tape. As a side effect reads the
mode page into the tape buffer. */
static int
nbr_partitions(struct inode * inode)
{
int dev = TAPE_NR(inode->i_rdev), result;
Scsi_Tape *STp;
Scsi_Cmnd * SCpnt = NULL;
unsigned char cmd[10];
STp = &(scsi_tapes[dev]);
if (STp->ready != ST_READY)
return (-EIO);
memset ((void *) &cmd[0], 0, 10);
cmd[0] = MODE_SENSE;
cmd[1] = 8; /* Page format */
cmd[2] = PART_PAGE;
cmd[4] = 200;
SCpnt = st_do_scsi(SCpnt, STp, cmd, 200, STp->timeout, MAX_READY_RETRIES, TRUE);
if (SCpnt == NULL)
return (-EBUSY);
scsi_release_command(SCpnt);
SCpnt = NULL;
if ((STp->buffer)->last_result_fatal != 0) {
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Can't read medium partition page.\n", dev);
#endif
result = (-EIO);
}
else {
result = (STp->buffer)->b_data[MODE_HEADER_LENGTH + 3] + 1;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Number of partitions %d.\n", dev, result);
#endif
}
return result;
}
/* Partition the tape into two partitions if size > 0 or one partition if
size == 0 */
static int
partition_tape(struct inode * inode, int size)
{
int dev = TAPE_NR(inode->i_rdev), result;
int length;
Scsi_Tape *STp;
Scsi_Cmnd * SCpnt = NULL;
unsigned char cmd[10], *bp;
if ((result = nbr_partitions(inode)) < 0)
return result;
STp = &(scsi_tapes[dev]);
/* The mode page is in the buffer. Let's modify it and write it. */
bp = &((STp->buffer)->b_data[0]);
if (size <= 0) {
length = 8;
bp[MODE_HEADER_LENGTH + 3] = 0;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Formatting tape with one partition.\n", dev);
#endif
}
else {
length = 10;
bp[MODE_HEADER_LENGTH + 3] = 1;
bp[MODE_HEADER_LENGTH + 8] = (size >> 8) & 0xff;
bp[MODE_HEADER_LENGTH + 9] = size & 0xff;
#if DEBUG
if (debugging)
printk(ST_DEB_MSG "st%d: Formatting tape with two partition (1 = %d MB).\n",
dev, size);
#endif
}
bp[MODE_HEADER_LENGTH + 6] = 0;
bp[MODE_HEADER_LENGTH + 7] = 0;
bp[MODE_HEADER_LENGTH + 4] = 0x30; /* IDP | PSUM = MB */
bp[0] = 0;
bp[1] = 0;
bp[MODE_HEADER_LENGTH] &= 0x3f;
bp[MODE_HEADER_LENGTH + 1] = length - 2;
memset(cmd, 0, 10);
cmd[0] = MODE_SELECT;
cmd[1] = 0x10;
cmd[4] = length + MODE_HEADER_LENGTH;
SCpnt = st_do_scsi(SCpnt, STp, cmd, cmd[4], STp->long_timeout,
MAX_READY_RETRIES, TRUE);
if (SCpnt == NULL)
return (-EBUSY);
scsi_release_command(SCpnt);
SCpnt = NULL;
if ((STp->buffer)->last_result_fatal != 0) {
printk(KERN_INFO "st%d: Partitioning of tape failed.\n", dev);
result = (-EIO);
}
else
result = 0;
return result;
}
/* The ioctl command */
static int
st_ioctl(struct inode * inode,struct file * file,
unsigned int cmd_in, unsigned long arg)
{
int i, cmd_nr, cmd_type, bt;
unsigned int blk;
struct mtop mtc;
struct mtpos mt_pos;
Scsi_Tape *STp;
ST_mode *STm;
ST_partstat *STps;
int dev = TAPE_NR(inode->i_rdev);
STp = &(scsi_tapes[dev]);
#if DEBUG
if (debugging && !STp->in_use) {
printk(ST_DEB_MSG "st%d: Incorrect device.\n", dev);
return (-EIO);
}
#endif
STm = &(STp->modes[STp->current_mode]);
STps = &(STp->ps[STp->partition]);
/*
* If we are in the middle of error recovery, don't let anyone
* else try and use this device. Also, if error recovery fails, it
* may try and take the device offline, in which case all further
* access to the device is prohibited.
*/
if( !scsi_block_when_processing_errors(STp->device) ) {
return -ENXIO;
}
cmd_type = _IOC_TYPE(cmd_in);
cmd_nr = _IOC_NR(cmd_in);
if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) {
if (_IOC_SIZE(cmd_in) != sizeof(mtc))
return (-EINVAL);
i = copy_from_user((char *) &mtc, (char *)arg, sizeof(struct mtop));
if (i)
return (-EFAULT);
if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) {
printk(KERN_WARNING "st%d: MTSETDRVBUFFER only allowed for root.\n", dev);
return (-EPERM);
}
if (!STm->defined &&
(mtc.mt_op != MTSETDRVBUFFER && (mtc.mt_count & MT_ST_OPTIONS) == 0))
return (-ENXIO);
if (!(STp->device)->was_reset) {
if (STps->eof == ST_FM_HIT) {
if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM|| mtc.mt_op == MTEOM) {
mtc.mt_count -= 1;
if (STps->drv_file >= 0)
STps->drv_file += 1;
}
else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) {
mtc.mt_count += 1;
if (STps->drv_file >= 0)
STps->drv_file += 1;
}
}
if (mtc.mt_op == MTSEEK) {
/* Old position must be restored if partition will be changed */
i = !STp->can_partitions ||
(STp->new_partition != STp->partition);
}
else {
i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL ||
mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM ||
mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD ||
mtc.mt_op == MTCOMPRESSION;
}
i = flush_buffer(inode, file, i);
if (i < 0)
return i;
}
else {
/*
* If there was a bus reset, block further access
* to this device. If the user wants to rewind the tape,
* then reset the flag and allow access again.
*/
if(mtc.mt_op != MTREW &&
mtc.mt_op != MTOFFL &&
mtc.mt_op != MTRETEN &&
mtc.mt_op != MTERASE &&
mtc.mt_op != MTSEEK &&
mtc.mt_op != MTEOM)
return (-EIO);
STp->device->was_reset = 0;
if (STp->door_locked != ST_UNLOCKED &&
STp->door_locked != ST_LOCK_FAILS) {
if (st_int_ioctl(inode, MTLOCK, 0)) {
printk(KERN_NOTICE "st%d: Could not relock door after bus reset.\n",
dev);
STp->door_locked = ST_UNLOCKED;
}
}
}
if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK &&
mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM &&
mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART)
STps->rw = ST_IDLE; /* Prevent automatic WEOF and fsf */
if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
st_int_ioctl(inode, MTUNLOCK, 0); /* Ignore result! */
if (mtc.mt_op == MTSETDRVBUFFER &&
(mtc.mt_count & MT_ST_OPTIONS) != 0)
return st_set_options(inode, mtc.mt_count);
if (mtc.mt_op == MTSETPART) {
if (!STp->can_partitions ||
mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS)
return (-EINVAL);
if (mtc.mt_count >= STp->nbr_partitions &&
(STp->nbr_partitions = nbr_partitions(inode)) < 0)
return (-EIO);
if (mtc.mt_count >= STp->nbr_partitions)
return (-EINVAL);
STp->new_partition = mtc.mt_count;
return 0;
}
if (mtc.mt_op == MTMKPART) {
if (!STp->can_partitions)
return (-EINVAL);
if ((i = st_int_ioctl(inode, MTREW, 0)) < 0 ||
(i = partition_tape(inode, mtc.mt_count)) < 0)
return i;
for (i=0; i < ST_NBR_PARTITIONS; i++) {
STp->ps[i].rw = ST_IDLE;
STp->ps[i].at_sm = 0;
STp->ps[i].last_block_valid = FALSE;
}
STp->partition = STp->new_partition = 0;
STp->nbr_partitions = 1; /* Bad guess ?-) */
STps->drv_block = STps->drv_file = 0;
return 0;
}
if (mtc.mt_op == MTSEEK) {
i = set_location(inode, mtc.mt_count, STp->new_partition, 0);
if (!STp->can_partitions)
STp->ps[0].rw = ST_IDLE;
return i;
}
if (STp->can_partitions && STp->ready == ST_READY &&
(i = update_partition(inode)) < 0)
return i;
if (mtc.mt_op == MTCOMPRESSION)
return st_compression(STp, (mtc.mt_count & 1));
else
return st_int_ioctl(inode, mtc.mt_op, mtc.mt_count);
}
if (!STm->defined)
return (-ENXIO);
if ((i = flush_buffer(inode, file, FALSE)) < 0)
return i;
if (STp->can_partitions &&
(i = update_partition(inode)) < 0)
return i;
if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) {
if (_IOC_SIZE(cmd_in) != sizeof(struct mtget))
return (-EINVAL);
(STp->mt_status)->mt_dsreg =
((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
(STp->mt_status)->mt_blkno = STps->drv_block;
(STp->mt_status)->mt_fileno = STps->drv_file;
if (STp->block_size != 0) {
if (STps->rw == ST_WRITING)
(STp->mt_status)->mt_blkno +=
(STp->buffer)->buffer_bytes / STp->block_size;
else if (STps->rw == ST_READING)
(STp->mt_status)->mt_blkno -= ((STp->buffer)->buffer_bytes +
STp->block_size - 1) / STp->block_size;
}
(STp->mt_status)->mt_gstat = 0;
if (STp->drv_write_prot)
(STp->mt_status)->mt_gstat |= GMT_WR_PROT(0xffffffff);
if ((STp->mt_status)->mt_blkno == 0) {
if ((STp->mt_status)->mt_fileno == 0)
(STp->mt_status)->mt_gstat |= GMT_BOT(0xffffffff);
else
(STp->mt_status)->mt_gstat |= GMT_EOF(0xffffffff);
}
(STp->mt_status)->mt_resid = STp->partition;
if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR)
(STp->mt_status)->mt_gstat |= GMT_EOT(0xffffffff);
else if (STps->eof >= ST_EOM_OK)
(STp->mt_status)->mt_gstat |= GMT_EOD(0xffffffff);
if (STp->density == 1)
(STp->mt_status)->mt_gstat |= GMT_D_800(0xffffffff);
else if (STp->density == 2)
(STp->mt_status)->mt_gstat |= GMT_D_1600(0xffffffff);
else if (STp->density == 3)
(STp->mt_status)->mt_gstat |= GMT_D_6250(0xffffffff);
if (STp->ready == ST_READY)
(STp->mt_status)->mt_gstat |= GMT_ONLINE(0xffffffff);
if (STp->ready == ST_NO_TAPE)
(STp->mt_status)->mt_gstat |= GMT_DR_OPEN(0xffffffff);
if (STps->at_sm)
(STp->mt_status)->mt_gstat |= GMT_SM(0xffffffff);
if (STm->do_async_writes || (STm->do_buffer_writes && STp->block_size != 0) ||
STp->drv_buffer != 0)
(STp->mt_status)->mt_gstat |= GMT_IM_REP_EN(0xffffffff);
i = copy_to_user((char *)arg, (char *)(STp->mt_status),
sizeof(struct mtget));
if (i)
return (-EFAULT);
(STp->mt_status)->mt_erreg = 0; /* Clear after read */
return 0;
} /* End of MTIOCGET */
if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos))
return (-EINVAL);
if ((i = get_location(inode, &blk, &bt, 0)) < 0)
return i;
mt_pos.mt_blkno = blk;
i = copy_to_user((char *)arg, (char *) (&mt_pos), sizeof(struct mtpos));
if (i)
return (-EFAULT);
return 0;
}
return scsi_ioctl(STp->device, cmd_in, (void *) arg);
}
/* Try to allocate a new tape buffer */
static ST_buffer *
new_tape_buffer( int from_initialization, int need_dma )
{
int i, priority, b_size, got = 0, segs = 0;
ST_buffer *tb;
if (st_nbr_buffers >= st_template.dev_max)
return NULL; /* Should never happen */
if (from_initialization)
priority = GFP_ATOMIC;
else
priority = GFP_KERNEL;
i = sizeof(ST_buffer) + (st_max_sg_segs - 1) * sizeof(struct scatterlist);
tb = (ST_buffer *)scsi_init_malloc(i, priority);
if (tb) {
tb->this_size = i;
if (need_dma)
priority |= GFP_DMA;
/* Try to allocate the first segment up to ST_FIRST_ORDER and the
others big enough to reach the goal */
for (b_size = PAGE_SIZE << ST_FIRST_ORDER;
b_size / 2 >= st_buffer_size && b_size > PAGE_SIZE; )
b_size /= 2;
for ( ; b_size >= PAGE_SIZE; b_size /= 2) {
tb->sg[0].address =
(unsigned char *)scsi_init_malloc(b_size, priority);
if (tb->sg[0].address != NULL) {
tb->sg[0].alt_address = NULL;
tb->sg[0].length = b_size;
break;
}
}
if (tb->sg[segs].address == NULL) {
scsi_init_free((char *)tb, tb->this_size);
tb = NULL;
}
else { /* Got something, continue */
for (b_size = PAGE_SIZE;
st_buffer_size > tb->sg[0].length + (ST_FIRST_SG - 1) * b_size; )
b_size *= 2;
for (segs=1, got=tb->sg[0].length;
got < st_buffer_size && segs < ST_FIRST_SG; ) {
tb->sg[segs].address =
(unsigned char *)scsi_init_malloc(b_size, priority);
if (tb->sg[segs].address == NULL) {
if (st_buffer_size - got <=
(ST_FIRST_SG - segs) * b_size / 2) {
b_size /= 2; /* Large enough for the rest of the buffers */
continue;
}
for (i=0; i < segs - 1; i++)
scsi_init_free(tb->sg[i].address, tb->sg[i].length);
scsi_init_free((char *)tb, tb->this_size);
tb = NULL;
break;
}
tb->sg[segs].alt_address = NULL;
tb->sg[segs].length = b_size;
got += b_size;
segs++;
}
}
}
if (!tb) {
printk(KERN_NOTICE "st: Can't allocate new tape buffer (nbr %d).\n",
st_nbr_buffers);
return NULL;
}
tb->sg_segs = tb->orig_sg_segs = segs;
tb->b_data = tb->sg[0].address;
#if DEBUG
if (debugging) {
printk(ST_DEB_MSG
"st: Allocated tape buffer %d (%d bytes, %d segments, dma: %d, a: %p).\n",
st_nbr_buffers, got, tb->sg_segs, need_dma, tb->b_data);
printk(ST_DEB_MSG
"st: segment sizes: first %d, last %d bytes.\n",
tb->sg[0].length, tb->sg[segs-1].length);
}
#endif
tb->in_use = 0;
tb->dma = need_dma;
tb->buffer_size = got;
tb->writing = 0;
st_buffers[st_nbr_buffers++] = tb;
return tb;
}
/* Try to allocate a temporary enlarged tape buffer */
static int
enlarge_buffer(ST_buffer *STbuffer, int new_size, int need_dma)
{
int segs, nbr, max_segs, b_size, priority, got;
normalize_buffer(STbuffer);
max_segs = STbuffer->use_sg;
if (max_segs > st_max_sg_segs)
max_segs = st_max_sg_segs;
nbr = max_segs - STbuffer->sg_segs;
if (nbr <= 0)
return FALSE;
priority = GFP_KERNEL;
if (need_dma)
priority |= GFP_DMA;
for (b_size = PAGE_SIZE; b_size * nbr < new_size - STbuffer->buffer_size; )
b_size *= 2;
for (segs=STbuffer->sg_segs, got=STbuffer->buffer_size;
segs < max_segs && got < new_size; ) {
STbuffer->sg[segs].address =
(unsigned char *)scsi_init_malloc(b_size, priority);
if (STbuffer->sg[segs].address == NULL) {
if (new_size - got <= (max_segs - segs) * b_size / 2) {
b_size /= 2; /* Large enough for the rest of the buffers */
continue;
}
printk(KERN_NOTICE "st: failed to enlarge buffer to %d bytes.\n",
new_size);
normalize_buffer(STbuffer);
return FALSE;
}
STbuffer->sg[segs].alt_address = NULL;
STbuffer->sg[segs].length = b_size;
STbuffer->sg_segs += 1;
got += b_size;
STbuffer->buffer_size = got;
segs++;
}
#if DEBUG
if (debugging)
printk(ST_DEB_MSG
"st: Succeeded to enlarge buffer to %d bytes (segs %d->%d, %d).\n",
got, STbuffer->orig_sg_segs, STbuffer->sg_segs, b_size);
#endif
return TRUE;
}
/* Release the extra buffer */
static void
normalize_buffer(ST_buffer *STbuffer)
{
int i;
for (i=STbuffer->orig_sg_segs; i < STbuffer->sg_segs; i++) {
scsi_init_free(STbuffer->sg[i].address, STbuffer->sg[i].length);
STbuffer->buffer_size -= STbuffer->sg[i].length;
}
#if DEBUG
if (debugging && STbuffer->orig_sg_segs < STbuffer->sg_segs)
printk(ST_DEB_MSG "st: Buffer at %p normalized to %d bytes (segs %d).\n",
STbuffer->b_data, STbuffer->buffer_size, STbuffer->sg_segs);
#endif
STbuffer->sg_segs = STbuffer->orig_sg_segs;
}
/* Move data from the user buffer to the tape buffer. Returns zero (success) or
negative error code. */
static int
append_to_buffer(const char *ubp, ST_buffer *st_bp, int do_count)
{
int i, cnt, res, offset;
for (i=0, offset=st_bp->buffer_bytes;
i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
offset -= st_bp->sg[i].length;
if (i == st_bp->sg_segs) { /* Should never happen */
printk(KERN_WARNING "st: append_to_buffer offset overflow.\n");
return (-EIO);
}
for ( ; i < st_bp->sg_segs && do_count > 0; i++) {
cnt = st_bp->sg[i].length - offset < do_count ?
st_bp->sg[i].length - offset : do_count;
res = copy_from_user(st_bp->sg[i].address + offset, ubp, cnt);
if (res)
return (-EFAULT);
do_count -= cnt;
st_bp->buffer_bytes += cnt;
ubp += cnt;
offset = 0;
}
if (do_count) { /* Should never happen */
printk(KERN_WARNING "st: append_to_buffer overflow (left %d).\n",
do_count);
return (-EIO);
}
return 0;
}
/* Move data from the tape buffer to the user buffer. Returns zero (success) or
negative error code. */
static int
from_buffer(ST_buffer *st_bp, char *ubp, int do_count)
{
int i, cnt, res, offset;
for (i=0, offset=st_bp->read_pointer;
i < st_bp->sg_segs && offset >= st_bp->sg[i].length; i++)
offset -= st_bp->sg[i].length;
if (i == st_bp->sg_segs) { /* Should never happen */
printk(KERN_WARNING "st: from_buffer offset overflow.\n");
return (-EIO);
}
for ( ; i < st_bp->sg_segs && do_count > 0; i++) {
cnt = st_bp->sg[i].length - offset < do_count ?
st_bp->sg[i].length - offset : do_count;
res = copy_to_user(ubp, st_bp->sg[i].address + offset, cnt);
if (res)
return (-EFAULT);
do_count -= cnt;
st_bp->buffer_bytes -= cnt;
st_bp->read_pointer += cnt;
ubp += cnt;
offset = 0;
}
if (do_count) { /* Should never happen */
printk(KERN_WARNING "st: from_buffer overflow (left %d).\n",
do_count);
return (-EIO);
}
return 0;
}
#ifndef MODULE
/* Set the boot options. Syntax: st=xxx,yyy
where xxx is buffer size in 1024 byte blocks and yyy is write threshold
in 1024 byte blocks. */
__initfunc( void
st_setup(char *str, int *ints))
{
if (ints[0] > 0 && ints[1] > 0)
st_buffer_size = ints[1] * ST_KILOBYTE;
if (ints[0] > 1 && ints[2] > 0) {
st_write_threshold = ints[2] * ST_KILOBYTE;
if (st_write_threshold > st_buffer_size)
st_write_threshold = st_buffer_size;
}
if (ints[0] > 2 && ints[3] > 0)
st_max_buffers = ints[3];
}
#endif
static struct file_operations st_fops = {
NULL, /* lseek - default */
st_read, /* read - general block-dev read */
st_write, /* write - general block-dev write */
NULL, /* readdir - bad */
NULL, /* select */
st_ioctl, /* ioctl */
NULL, /* mmap */
scsi_tape_open, /* open */
scsi_tape_flush, /* flush */
scsi_tape_close, /* release */
NULL /* fsync */
};
static int st_attach(Scsi_Device * SDp){
Scsi_Tape * tpnt;
ST_mode * STm;
ST_partstat * STps;
int i;
if (SDp->type != TYPE_TAPE)
return 1;
if (st_template.nr_dev >= st_template.dev_max) {
SDp->attached--;
return 1;
}
for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
if(!tpnt->device) break;
if(i >= st_template.dev_max) panic ("scsi_devices corrupt (st)");
scsi_tapes[i].device = SDp;
if (SDp->scsi_level <= 2)
scsi_tapes[i].mt_status->mt_type = MT_ISSCSI1;
else
scsi_tapes[i].mt_status->mt_type = MT_ISSCSI2;
tpnt->devt = MKDEV(SCSI_TAPE_MAJOR, i);
tpnt->dirty = 0;
tpnt->waiting = NULL;
tpnt->in_use = 0;
tpnt->drv_buffer = 1; /* Try buffering if no mode sense */
tpnt->restr_dma = (SDp->host)->unchecked_isa_dma;
tpnt->density = 0;
tpnt->do_auto_lock = ST_AUTO_LOCK;
tpnt->can_bsr = ST_IN_FILE_POS;
tpnt->can_partitions = 0;
tpnt->two_fm = ST_TWO_FM;
tpnt->fast_mteom = ST_FAST_MTEOM;
tpnt->scsi2_logical = ST_SCSI2LOGICAL;
tpnt->write_threshold = st_write_threshold;
tpnt->default_drvbuffer = 0xff; /* No forced buffering */
tpnt->partition = 0;
tpnt->new_partition = 0;
tpnt->nbr_partitions = 0;
tpnt->timeout = ST_TIMEOUT;
tpnt->long_timeout = ST_LONG_TIMEOUT;
for (i=0; i < ST_NBR_MODES; i++) {
STm = &(tpnt->modes[i]);
STm->defined = FALSE;
STm->sysv = ST_SYSV;
STm->defaults_for_writes = 0;
STm->do_async_writes = ST_ASYNC_WRITES;
STm->do_buffer_writes = ST_BUFFER_WRITES;
STm->do_read_ahead = ST_READ_AHEAD;
STm->default_compression = ST_DONT_TOUCH;
STm->default_blksize = (-1); /* No forced size */
STm->default_density = (-1); /* No forced density */
}
for (i=0; i < ST_NBR_PARTITIONS; i++) {
STps = &(tpnt->ps[i]);
STps->rw = ST_IDLE;
STps->eof = ST_NOEOF;
STps->at_sm = 0;
STps->last_block_valid = FALSE;
STps->drv_block = 0;
STps->drv_file = 0;
}
tpnt->current_mode = 0;
tpnt->modes[0].defined = TRUE;
tpnt->density_changed = tpnt->compression_changed =
tpnt->blksize_changed = FALSE;
st_template.nr_dev++;
return 0;
};
static int st_detect(Scsi_Device * SDp)
{
if(SDp->type != TYPE_TAPE) return 0;
printk(KERN_WARNING
"Detected scsi tape st%d at scsi%d, channel %d, id %d, lun %d\n",
st_template.dev_noticed++,
SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
return 1;
}
static int st_registered = 0;
/* Driver initialization (not __initfunc because may be called later) */
static int st_init()
{
int i;
Scsi_Tape * STp;
#if !ST_RUNTIME_BUFFERS
int target_nbr;
#endif
if (st_template.dev_noticed == 0) return 0;
if(!st_registered) {
if (register_chrdev(SCSI_TAPE_MAJOR,"st",&st_fops)) {
printk(KERN_ERR "Unable to get major %d for SCSI tapes\n",MAJOR_NR);
return 1;
}
st_registered++;
}
if (scsi_tapes) return 0;
st_template.dev_max = st_template.dev_noticed + ST_EXTRA_DEVS;
if (st_template.dev_max < ST_MAX_TAPES)
st_template.dev_max = ST_MAX_TAPES;
if (st_template.dev_max > 128 / ST_NBR_MODES)
printk(KERN_INFO "st: Only %d tapes accessible.\n", 128 / ST_NBR_MODES);
scsi_tapes =
(Scsi_Tape *) scsi_init_malloc(st_template.dev_max * sizeof(Scsi_Tape),
GFP_ATOMIC);
if (scsi_tapes == NULL) {
printk(KERN_ERR "Unable to allocate descriptors for SCSI tapes.\n");
unregister_chrdev(SCSI_TAPE_MAJOR, "st");
return 1;
}
#if DEBUG
printk(ST_DEB_MSG "st: Buffer size %d bytes, write threshold %d bytes.\n",
st_buffer_size, st_write_threshold);
#endif
memset(scsi_tapes, 0, st_template.dev_max * sizeof(Scsi_Tape));
for (i=0; i < st_template.dev_max; ++i) {
STp = &(scsi_tapes[i]);
STp->capacity = 0xfffff;
STp->mt_status = (struct mtget *) scsi_init_malloc(sizeof(struct mtget),
GFP_ATOMIC);
/* Initialize status */
memset((void *) scsi_tapes[i].mt_status, 0, sizeof(struct mtget));
}
/* Allocate the buffers */
st_buffers =
(ST_buffer **) scsi_init_malloc(st_template.dev_max * sizeof(ST_buffer *),
GFP_ATOMIC);
if (st_buffers == NULL) {
printk(KERN_ERR "Unable to allocate tape buffer pointers.\n");
unregister_chrdev(SCSI_TAPE_MAJOR, "st");
scsi_init_free((char *) scsi_tapes,
st_template.dev_max * sizeof(Scsi_Tape));
return 1;
}
#if ST_RUNTIME_BUFFERS
st_nbr_buffers = 0;
#else
target_nbr = st_template.dev_noticed;
if (target_nbr < ST_EXTRA_DEVS)
target_nbr = ST_EXTRA_DEVS;
if (target_nbr > st_max_buffers)
target_nbr = st_max_buffers;
for (i=st_nbr_buffers=0; i < target_nbr; i++) {
if (!new_tape_buffer(TRUE, TRUE)) {
if (i == 0) {
#if 0
printk(KERN_ERR "Can't continue without at least one tape buffer.\n");
unregister_chrdev(SCSI_TAPE_MAJOR, "st");
scsi_init_free((char *) st_buffers,
st_template.dev_max * sizeof(ST_buffer *));
scsi_init_free((char *) scsi_tapes,
st_template.dev_max * sizeof(Scsi_Tape));
return 1;
#else
printk(KERN_INFO "No tape buffers allocated at initialization.\n");
break;
#endif
}
printk(KERN_INFO "Number of tape buffers adjusted.\n");
break;
}
}
#endif
return 0;
}
static void st_detach(Scsi_Device * SDp)
{
Scsi_Tape * tpnt;
int i;
for(tpnt = scsi_tapes, i=0; i<st_template.dev_max; i++, tpnt++)
if(tpnt->device == SDp) {
tpnt->device = NULL;
SDp->attached--;
st_template.nr_dev--;
st_template.dev_noticed--;
return;
}
return;
}
#ifdef MODULE
int init_module(void) {
int result;
if (buffer_kbs > 0)
st_buffer_size = buffer_kbs * ST_KILOBYTE;
if (write_threshold_kbs > 0)
st_write_threshold = write_threshold_kbs * ST_KILOBYTE;
if (st_write_threshold > st_buffer_size)
st_write_threshold = st_buffer_size;
if (max_buffers > 0)
st_max_buffers = max_buffers;
if (max_sg_segs >= ST_FIRST_SG)
st_max_sg_segs = max_sg_segs;
printk(KERN_INFO "st: bufsize %d, wrt %d, max buffers %d, s/g segs %d.\n",
st_buffer_size, st_write_threshold, st_max_buffers, st_max_sg_segs);
st_template.module = &__this_module;
result = scsi_register_module(MODULE_SCSI_DEV, &st_template);
if (result)
return result;
return 0;
}
void cleanup_module( void)
{
int i, j;
scsi_unregister_module(MODULE_SCSI_DEV, &st_template);
unregister_chrdev(SCSI_TAPE_MAJOR, "st");
st_registered--;
if(scsi_tapes != NULL) {
scsi_init_free((char *) scsi_tapes,
st_template.dev_max * sizeof(Scsi_Tape));
if (st_buffers != NULL) {
for (i=0; i < st_nbr_buffers; i++)
if (st_buffers[i] != NULL) {
for (j=0; j < st_buffers[i]->sg_segs; j++)
scsi_init_free((char *) st_buffers[i]->sg[j].address,
st_buffers[i]->sg[j].length);
scsi_init_free((char *) st_buffers[i], st_buffers[i]->this_size);
}
scsi_init_free((char *) st_buffers,
st_template.dev_max * sizeof(ST_buffer *));
}
}
st_template.dev_max = 0;
printk(KERN_INFO "st: Unloaded.\n");
}
#endif /* MODULE */
|