/
vtltape.c
2637 lines (2319 loc) · 69 KB
/
vtltape.c
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
/*
* This daemon is the SCSI SSC target (Sequential device - tape drive)
* portion of the vtl package.
*
* The vtl package consists of:
* a kernel module (vlt.ko) - Currently on 2.6.x Linux kernel support.
* SCSI target daemons for both SMC and SSC devices.
*
* Copyright (C) 2005 - 2009 Mark Harvey markh794@gmail.com
* mark_harvey@symantec.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* v0.1 -> Proof (proof of concept) that this may actually work (just)
* v0.2 -> Get queueCommand() callback working -
* (Note to self: Sleeping in kernel is bad!)
* v0.3 -> Message queues + make into daemon
* changed lseek to lseek64
* v0.4 -> First copy given to anybody,
* v0.10 -> First start of a Solaris x86 port.. Still underway.
* v0.11 -> First start of a Linux 2.4 kernel port.. Still underway.
* However I'm scrapping this kfifo stuff and passing a pointer
* and using copy{to|from}_user routines instead.
* v0.12 -> Forked into 'stable' (0.12) and 'devel' (0.13).
* My current thinking : This is a dead end anyway.
* An iSCSI target done in user-space is now my perferred solution.
* This means I don't have to do any kernel level drivers
* and leaverage the hosts native iSCSI initiator.
* 0.14 13 Feb 2008
* Since ability to define device serial number, increased ver from
* 0.12 to 0.14
*
* 0.16 Jun 2009
* Moved SCSI Inquiry into user-space.
* SCSI lu are created/destroyed as the daemon is started/shutdown
*/
#define _FILE_OFFSET_BITS 64
#define _XOPEN_SOURCE 500
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <strings.h>
#include <syslog.h>
#include <inttypes.h>
#include <pwd.h>
#include <signal.h>
#include "list.h"
#include "be_byteshift.h"
#include "vtl_common.h"
#include "scsi.h"
#include "q.h"
#include "vtllib.h"
#include "vtltape.h"
#include "spc.h"
#include "ssc.h"
#include "log.h"
char vtl_driver_name[] = "vtltape";
/* Variables for simple, logical only SCSI Encryption system */
static struct encryption encryption;
#define UKAD_LENGTH encryption.ukad_length
#define AKAD_LENGTH encryption.akad_length
#define KEY_LENGTH encryption.key_length
#define UKAD encryption.ukad
#define AKAD encryption.akad
#define KEY encryption.key
#include <zlib.h>
extern uint8_t last_cmd;
extern int current_state; /* scope, Global -> Last status sent to fifo */
/* Suppress Incorrect Length Indicator */
#define SILI 0x2
/* Fixed block format */
#define FIXED 0x1
#ifndef Solaris
/* I'm sure there must be a header where lseek64() is defined */
loff_t lseek64(int, loff_t, int);
int ioctl(int, int, void *);
#endif
int verbose = 0;
int debug = 0;
long my_id;
#define MEDIA_WRITABLE 0
#define MEDIA_READONLY 1
struct priv_lu_ssc lu_ssc;
struct lu_phy_attr lunit;
struct MAM_Attributes_table {
int attribute;
int length;
int read_only;
int format;
void *value;
} MAM_Attributes[] = {
{0x000, 8, 1, 0, &mam.remaining_capacity },
{0x001, 8, 1, 0, &mam.max_capacity },
{0x002, 8, 1, 0, &mam.TapeAlert },
{0x003, 8, 1, 0, &mam.LoadCount },
{0x004, 8, 1, 0, &mam.MAMSpaceRemaining },
{0x005, 8, 1, 1, &mam.AssigningOrganization_1 },
{0x006, 1, 1, 0, &mam.FormattedDensityCode },
{0x007, 2, 1, 0, &mam.InitializationCount },
{0x20a, 40, 1, 1, &mam.DevMakeSerialLastLoad },
{0x20b, 40, 1, 1, &mam.DevMakeSerialLastLoad1 },
{0x20c, 40, 1, 1, &mam.DevMakeSerialLastLoad2 },
{0x20d, 40, 1, 1, &mam.DevMakeSerialLastLoad3 },
{0x220, 8, 1, 0, &mam.WrittenInMediumLife },
{0x221, 8, 1, 0, &mam.ReadInMediumLife },
{0x222, 8, 1, 0, &mam.WrittenInLastLoad },
{0x223, 8, 1, 0, &mam.ReadInLastLoad },
{0x400, 8, 1, 1, &mam.MediumManufacturer },
{0x401, 32, 1, 1, &mam.MediumSerialNumber },
{0x402, 4, 1, 0, &mam.MediumLength },
{0x403, 4, 1, 0, &mam.MediumWidth },
{0x404, 8, 1, 1, &mam.AssigningOrganization_2 },
{0x405, 1, 1, 0, &mam.MediumDensityCode },
{0x406, 8, 1, 1, &mam.MediumManufactureDate },
{0x407, 8, 1, 0, &mam.MAMCapacity },
{0x408, 1, 0, 0, &mam.MediumType },
{0x409, 2, 1, 0, &mam.MediumTypeInformation },
{0x800, 8, 0, 1, &mam.ApplicationVendor },
{0x801, 32, 0, 1, &mam.ApplicationName },
{0x802, 8, 0, 1, &mam.ApplicationVersion },
{0x803, 160, 0, 2, &mam.UserMediumTextLabel },
{0x804, 12, 0, 1, &mam.DateTimeLastWritten },
{0x805, 1, 0, 0, &mam.LocalizationIdentifier },
{0x806, 32, 0, 1, &mam.Barcode },
{0x807, 80, 0, 2, &mam.OwningHostTextualName },
{0x808, 160, 0, 2, &mam.MediaPool },
{0xbff, 0, 1, 0, NULL }
};
static struct tape_drives_table {
char *name;
void (*init)(struct lu_phy_attr *);
} tape_drives[] = {
{ "ULT3580-TD1 ", init_ult3580_td1 },
{ "ULT3580-TD2 ", init_ult3580_td2 },
{ "ULT3580-TD3 ", init_ult3580_td3 },
{ "ULT3580-TD4 ", init_ult3580_td4 },
{ "ULT3580-TD5 ", init_ult3580_td5 },
{ "ULTRIUM-TD1 ", init_ult3580_td1 },
{ "ULTRIUM-TD2 ", init_ult3580_td2 },
{ "ULTRIUM-TD3 ", init_ult3580_td3 },
{ "ULTRIUM-TD4 ", init_ult3580_td4 },
{ "ULTRIUM-TD5 ", init_ult3580_td5 },
{ "Ultrium 1-SCSI ", init_hp_ult_1 },
{ "Ultrium 2-SCSI ", init_hp_ult_2 },
{ "Ultrium 3-SCSI ", init_hp_ult_3 },
{ "Ultrium 4-SCSI ", init_hp_ult_4 },
{ "Ultrium 5-SCSI ", init_hp_ult_5 },
{ "SDX-300C ", init_ait1_ssc },
{ "SDX-500C ", init_ait2_ssc },
{ "SDX-500V ", init_ait2_ssc },
{ "SDX-700C ", init_ait3_ssc },
{ "SDX-700V ", init_ait3_ssc },
{ "SDX-900V ", init_ait4_ssc },
{ "03592J1A ", init_3592_j1a },
{ "03592E05 ", init_3592_E05 },
{ "03592E06 ", init_3592_E06 },
{ "T10000C ", init_t10kC_ssc },
{ "T10000B ", init_t10kB_ssc },
{ "T10000 ", init_t10kA_ssc },
{ "T9840D ", init_9840D_ssc },
{ "T9840C ", init_9840C_ssc },
{ "T9840B ", init_9840B_ssc },
{ "T9840A ", init_9840A_ssc },
{ "T9940B ", init_9940B_ssc },
{ "T9940A ", init_9940A_ssc },
{ NULL, NULL},
};
static struct media_name_index_table {
char *name;
int media_type;
int mode_media_type;
int media_density;
} media_info[] = {
{"Undefined", Media_undefined,
media_type_unknown, medium_density_code_unknown},
/* Ultrium media */
{"LTO1", Media_LTO1,
media_type_lto1_data, medium_density_code_lto1},
{"LTO1 Clean", Media_LTO1_CLEAN,
media_type_lto1_data, medium_density_code_lto1},
{"LTO2", Media_LTO2,
media_type_lto2_data, medium_density_code_lto2},
{"LTO2 Clean", Media_LTO2_CLEAN,
media_type_lto2_data, medium_density_code_lto2},
{"LTO3", Media_LTO3,
media_type_lto3_data, medium_density_code_lto3},
{"LTO3 Clean", Media_LTO3_CLEAN,
media_type_lto3_data, medium_density_code_lto3},
{"LTO3 WORM", Media_LTO3_WORM,
media_type_lto3_worm, medium_density_code_lto3_WORM},
{"LTO4", Media_LTO4,
media_type_lto4_data, medium_density_code_lto4},
{"LTO4 Clean", Media_LTO4_CLEAN,
media_type_lto4_data, medium_density_code_lto4},
{"LTO4 WORM", Media_LTO4_WORM,
media_type_lto4_worm, medium_density_code_lto4_WORM},
{"LTO5", Media_LTO5,
media_type_lto5_data, medium_density_code_lto5},
{"LTO5 Clean", Media_LTO5_CLEAN,
media_type_lto5_data, medium_density_code_lto5},
{"LTO5 WORM", Media_LTO5_WORM,
media_type_lto5_worm, medium_density_code_lto5_WORM},
/* IBM 03592 media */
{"03592 JA", Media_3592_JA,
media_type_unknown, medium_density_code_j1a},
{"03592 JA Clean", Media_3592_JA_CLEAN,
media_type_unknown, medium_density_code_j1a},
{"03592 JA WORM", Media_3592_JW,
media_type_unknown, medium_density_code_j1a},
{"03592 JB", Media_3592_JB,
media_type_unknown, medium_density_code_e05},
{"03592 JB Clean", Media_3592_JB_CLEAN,
media_type_unknown, medium_density_code_e05},
{"03592 JB ENCR", Media_3592_JB,
media_type_unknown, medium_density_code_e05_ENCR},
{"03592 JC", Media_3592_JX,
media_type_unknown, medium_density_code_e06},
{"03592 JC Clean", Media_3592_JX_CLEAN,
media_type_unknown, medium_density_code_e06},
{"03592 JC ENCR", Media_3592_JX,
media_type_unknown, medium_density_code_e06_ENCR},
/* AIT media */
{"AIT1", Media_AIT1,
media_type_unknown, medium_density_code_ait1},
{"AIT1 Clean", Media_AIT1_CLEAN,
media_type_unknown, medium_density_code_ait1},
{"AIT2", Media_AIT2,
media_type_unknown, medium_density_code_ait2},
{"AIT2 Clean", Media_AIT2_CLEAN,
media_type_unknown, medium_density_code_ait2},
{"AIT3", Media_AIT3,
media_type_unknown, medium_density_code_ait3},
{"AIT3 Clean", Media_AIT3_CLEAN,
media_type_unknown, medium_density_code_ait3},
{"AIT4", Media_AIT4,
media_type_unknown, medium_density_code_ait4},
{"AIT4 Clean", Media_AIT4_CLEAN,
media_type_unknown, medium_density_code_ait4},
{"AIT4 WORM", Media_AIT4_WORM,
media_type_unknown, medium_density_code_ait4},
/* STK 9x40 media */
{"9840A", Media_9840A,
media_type_unknown, medium_density_code_9840A},
{"9840A Clean", Media_9840A_CLEAN,
media_type_unknown, medium_density_code_9840A},
{"9840B", Media_9840B,
media_type_unknown, medium_density_code_9840B},
{"9840B Clean", Media_9840B_CLEAN,
media_type_unknown, medium_density_code_9840B},
{"9840C", Media_9840C,
media_type_unknown, medium_density_code_9840C},
{"9840C Clean", Media_9840C_CLEAN,
media_type_unknown, medium_density_code_9840C},
{"9840D", Media_9840D,
media_type_unknown, medium_density_code_9840D},
{"9840D Clean", Media_9840D_CLEAN,
media_type_unknown, medium_density_code_9840D},
{"9940A", Media_9940A,
media_type_unknown, medium_density_code_9940A},
{"9940A Clean", Media_9940A_CLEAN,
media_type_unknown, medium_density_code_9940A},
{"9940B", Media_9940B,
media_type_unknown, medium_density_code_9940B},
{"9940B Clean", Media_9940B_CLEAN,
media_type_unknown, medium_density_code_9940B},
/* STK T10000 media */
{"T10KA", Media_T10KA,
media_type_unknown, medium_density_code_10kA},
{"T10KA WORM", Media_T10KA_WORM,
media_type_unknown, medium_density_code_10kA},
{"T10KA Clean", Media_T10KA_CLEAN,
media_type_unknown, medium_density_code_10kA},
{"T10KB", Media_T10KB,
media_type_unknown, medium_density_code_10kB},
{"T10KB WORM", Media_T10KB_WORM,
media_type_unknown, medium_density_code_10kB},
{"T10KB Clean", Media_T10KB_CLEAN,
media_type_unknown, medium_density_code_10kB},
{"T10KC", Media_T10KC,
media_type_unknown, medium_density_code_10kC},
{"T10KC WORM", Media_T10KC_WORM,
media_type_unknown, medium_density_code_10kC},
{"T10KC Clean", Media_T10KC_CLEAN,
media_type_unknown, medium_density_code_10kC},
/* Quantum DLT / SDLT media */
{"DLT2", Media_DLT2,
media_type_unknown, medium_density_code_dlt2},
{"DLT3", Media_DLT3,
media_type_unknown, medium_density_code_dlt3},
{"DLT4", Media_DLT4,
media_type_unknown, medium_density_code_dlt4},
{"SDLT", Media_SDLT,
media_type_unknown, medium_density_code_sdlt},
{"SDLT 220", Media_SDLT220,
media_type_unknown, medium_density_code_220},
{"SDLT 320", Media_SDLT320,
media_type_unknown, medium_density_code_320},
{"SDLT 320 Clean", Media_SDLT320_CLEAN,
media_type_unknown, medium_density_code_320},
{"SDLT 600", Media_SDLT600,
media_type_unknown, medium_density_code_600},
{"SDLT 600 Clean", Media_SDLT600_CLEAN,
media_type_unknown, medium_density_code_600},
{"SDLT 600 WORM", Media_SDLT600_WORM,
media_type_unknown, medium_density_code_600},
/* 4MM DAT media */
{"DDS1", Media_DDS1,
media_type_unknown, medium_density_code_DDS1},
{"DDS2", Media_DDS2,
media_type_unknown, medium_density_code_DDS2},
{"DDS3", Media_DDS3,
media_type_unknown, medium_density_code_DDS3},
{"DDS4", Media_DDS4,
media_type_unknown, medium_density_code_DDS4},
{"DDS5", Media_DDS5,
media_type_unknown, medium_density_code_DDS5},
};
static void (*drive_init)(struct lu_phy_attr *) = init_default_ssc;
static void usage(char *progname) {
printf("Usage: %s -q <Q number> [-d] [-v]\n", progname);
printf(" Where:\n");
printf(" 'q number' is the queue priority number\n");
printf(" 'd' == debug\n");
printf(" 'v' == verbose\n");
}
static void
mk_sense_short_block(uint32_t requested, uint32_t processed, uint8_t *sense_valid)
{
int difference = (int)requested - (int)processed;
/* No sense, ILI bit set */
mkSenseBuf(SD_ILI, NO_ADDITIONAL_SENSE, sense_valid);
MHVTL_DBG(2, "Short block read: Requested: %d, Read: %d,"
" short by %d bytes",
requested, processed, difference);
/* Now fill in the datablock with number of bytes not read/written */
put_unaligned_be32(difference, &sense[3]);
}
static int lookup_media_int(char *s)
{
int i;
MHVTL_DBG(2, "looking for media type %s", s);
for (i = 0; i < ARRAY_SIZE(media_info); i++)
if (!strcmp(media_info[i].name, s))
return media_info[i].media_type;
return Media_undefined;
}
static const char *lookup_density_name(int den)
{
int i;
MHVTL_DBG(2, "looking for density type 0x%02x", den);
for (i = 0; i < ARRAY_SIZE(media_info); i++)
if (media_info[i].media_density == den)
return media_info[i].name;
return "(UNKNOWN density)";
}
static const char *lookup_media_type(int med)
{
int i;
MHVTL_DBG(2, "looking for media type 0x%02x", med);
for (i = 0; i < ARRAY_SIZE(media_info); i++)
if (media_info[i].media_type == med)
return media_info[i].name;
return "(UNKNOWN media type)";
}
int lookup_mode_media_type(int med)
{
int i;
MHVTL_DBG(2, "looking for mode media type for 0x%02x", med);
for (i = 0; i < ARRAY_SIZE(media_info); i++) {
MHVTL_DBG(3, "%s : 0x%02x mode media type 0x%02x",
media_info[i].name,
media_info[i].media_type,
media_info[i].mode_media_type);
if (media_info[i].media_type == med)
return media_info[i].mode_media_type;
}
return media_type_unknown;
}
/***********************************************************************/
/*
* Report supported densities
*/
#define REPORT_DENSITY_LEN 52
int resp_report_density(struct priv_lu_ssc *lu_priv, uint8_t media,
struct vtl_ds *dbuf_p)
{
uint8_t *buf = dbuf_p->data;
struct list_head *l_head;
struct density_info *di;
struct supported_density_list *den;
int len = dbuf_p->sz;
int count;
uint32_t a;
uint8_t *ds; /* Density Support Data Block Descriptor */
l_head = &lu_priv->pm->lu->den_list;
/* Zero out buf */
memset(buf, 0, len);
ds = &buf[4];
count = 0;
buf[2] = 0; /* Reserved */
buf[3] = 0; /* Reserved */
/* Assigning Oranization (8 chars long) */
if (media) { /* Report supported density by this media */
count = 1;
ds[0] = mam.MediumDensityCode;
ds[1] = mam.MediumDensityCode;
ds[2] = (OK_to_write) ? 0xa0 : 0x20; /* Set write OK flg */
put_unaligned_be16(REPORT_DENSITY_LEN, &ds[3]);
memcpy(&ds[5], &mam.media_info.bits_per_mm, 3);
memcpy(&ds[8], &mam.MediumWidth, 2);
memcpy(&ds[10], &mam.MediumLength, 2);
memcpy(&ds[12], &mam.max_capacity, 4);
snprintf((char *)&ds[16], 9, "%-8s",
mam.AssigningOrganization_1);
snprintf((char *)&ds[24], 9, "%-8s",
mam.media_info.density_name);
snprintf((char *)&ds[32], 20, "%-20s",
mam.media_info.description);
/* Fudge.. Now 'fix' up the spaces. */
for (a = 16; a < REPORT_DENSITY_LEN; a++)
if (!ds[a])
ds[a] = 0x20; /* replace 0 with ' ' */
} else { /* Report supported density by this drive */
list_for_each_entry(den, l_head, siblings) {
di = den->density_info;
count++;
MHVTL_DBG(2, "%s -> %s", di->description,
(den->rw) ? "RW" : "RO");
ds[0] = di->density;
ds[1] = di->density;
ds[2] = (den->rw) ? 0xa0 : 0x20; /* Set write OK flg */
put_unaligned_be16(REPORT_DENSITY_LEN, &ds[3]);
put_unaligned_be24(di->bits_per_mm, &ds[5]);
put_unaligned_be16(di->media_width, &ds[8]);
put_unaligned_be16(di->tracks, &ds[10]);
put_unaligned_be32(di->capacity, &ds[12]);
snprintf((char *)&ds[16], 9, "%-8s", di->assigning_org);
snprintf((char *)&ds[24], 9, "%-8s", di->density_name);
snprintf((char *)&ds[32], 20, "%-20s",
di->description);
/* Fudge.. Now 'fix' up the spaces. */
for (a = 16; a < REPORT_DENSITY_LEN; a++)
if (!ds[a])
ds[a] = 0x20; /* replace 0 with ' ' */
ds += REPORT_DENSITY_LEN;
}
}
put_unaligned_be16(REPORT_DENSITY_LEN * count, &buf[0]);
return REPORT_DENSITY_LEN * count + 4;
}
/*
* Read Attribute
*
* Fill in 'buf' with data and return number of bytes
*/
int resp_read_attribute(struct scsi_cmd *cmd)
{
uint16_t attrib;
uint32_t alloc_len;
int ret_val = 0;
int byte_index = 4;
int indx, found_attribute;
uint8_t *cdb = cmd->scb;
uint8_t *buf = cmd->dbuf_p->data;
uint8_t *sam_stat = &cmd->dbuf_p->sam_stat;
attrib = get_unaligned_be16(&cdb[8]);
alloc_len = get_unaligned_be32(&cdb[10]);
MHVTL_DBG(2, "Read Attribute: 0x%x, allocation len: %d",
attrib, alloc_len);
memset(buf, 0, alloc_len); /* Clear memory */
if (cdb[1] == 0) {
/* Attribute Values */
for (indx = found_attribute = 0; MAM_Attributes[indx].length; indx++) {
if (attrib == MAM_Attributes[indx].attribute)
found_attribute = 1;
if (found_attribute) {
/* calculate available data length */
ret_val += MAM_Attributes[indx].length + 5;
if (ret_val < alloc_len) {
/* add it to output */
buf[byte_index++] = MAM_Attributes[indx].attribute >> 8;
buf[byte_index++] = MAM_Attributes[indx].attribute;
buf[byte_index++] = (MAM_Attributes[indx].read_only << 7) | MAM_Attributes[indx].format;
buf[byte_index++] = MAM_Attributes[indx].length >> 8;
buf[byte_index++] = MAM_Attributes[indx].length;
memcpy(&buf[byte_index], MAM_Attributes[indx].value, MAM_Attributes[indx].length);
byte_index += MAM_Attributes[indx].length;
}
}
}
if (!found_attribute) {
mkSenseBuf(ILLEGAL_REQUEST, E_INVALID_FIELD_IN_CDB, sam_stat);
return 0;
}
} else {
/* Attribute List */
for (indx = found_attribute = 0; MAM_Attributes[indx].length; indx++) {
/* calculate available data length */
ret_val += 2;
if (ret_val <= alloc_len) {
/* add it to output */
buf[byte_index++] = MAM_Attributes[indx].attribute >> 8;
buf[byte_index++] = MAM_Attributes[indx].attribute;
}
}
}
put_unaligned_be32(ret_val, &buf[0]);
if (ret_val > alloc_len)
ret_val = alloc_len;
return ret_val;
}
/*
* Process WRITE ATTRIBUTE scsi command
* Returns 0 if OK
* or 1 if MAM needs to be written.
* or -1 on failure.
*/
int resp_write_attribute(struct scsi_cmd *cmd)
{
uint32_t alloc_len;
int byte_index;
int indx, attrib, attribute_length, found_attribute = 0;
struct MAM *mamp;
struct MAM mam_backup;
uint8_t *buf = cmd->dbuf_p->data;
uint8_t *sam_stat = &cmd->dbuf_p->sam_stat;
uint8_t *cdb = cmd->scb;
struct priv_lu_ssc *lu_priv;
alloc_len = get_unaligned_be32(&cdb[10]);
lu_priv = cmd->lu->lu_private;
mamp = lu_priv->mamp;
memcpy(&mam_backup, mamp, sizeof(struct MAM));
for (byte_index = 4; byte_index < alloc_len; ) {
attrib = ((uint16_t)buf[byte_index++] << 8);
attrib += buf[byte_index++];
for (indx = found_attribute = 0; MAM_Attributes[indx].length; indx++) {
if (attrib == MAM_Attributes[indx].attribute) {
found_attribute = 1;
byte_index += 1;
attribute_length = ((uint16_t)buf[byte_index++] << 8);
attribute_length += buf[byte_index++];
if ((attrib == 0x408) &&
(attribute_length == 1) &&
(buf[byte_index] == 0x80)) {
/* set media to worm */
MHVTL_LOG("Converted media to WORM");
mamp->MediumType = MEDIA_TYPE_WORM;
} else {
memcpy(MAM_Attributes[indx].value,
&buf[byte_index],
MAM_Attributes[indx].length);
}
byte_index += attribute_length;
break;
} else {
found_attribute = 0;
}
}
if (!found_attribute) {
memcpy(&mamp, &mam_backup, sizeof(mamp));
mkSenseBuf(ILLEGAL_REQUEST, E_INVALID_FIELD_IN_PARMS, sam_stat);
return 0;
}
}
return found_attribute;
}
/*
*
* Return number of bytes read.
* 0 on error with sense[] filled in...
*/
int readBlock(uint8_t *buf, uint32_t request_sz, int sili, uint8_t *sam_stat)
{
uint32_t disk_blk_size, blk_size;
uLongf uncompress_sz;
uint8_t *cbuf, *c2buf;
int z;
uint32_t tgtsize, rc;
loff_t nread = 0;
uint32_t save_sense;
MHVTL_DBG(3, "Request to read: %d bytes, SILI: %d", request_sz, sili);
/* check for a zero length read
* This is not an error, and shouldn't change the tape position */
if (request_sz == 0)
return 0;
switch (c_pos->blk_type) {
case B_DATA:
break;
case B_FILEMARK:
MHVTL_DBG(1, "Expected to find DATA header, found: FILEMARK");
position_blocks_forw(1, sam_stat);
mk_sense_short_block(request_sz, 0, sam_stat);
save_sense = get_unaligned_be32(&sense[3]);
mkSenseBuf(NO_SENSE | SD_FILEMARK, E_MARK, sam_stat);
put_unaligned_be32(save_sense, &sense[3]);
return 0;
break;
case B_EOD:
MHVTL_DBG(1, "Expected to find DATA header, found: EOD");
mk_sense_short_block(request_sz, 0, sam_stat);
save_sense = get_unaligned_be32(&sense[3]);
mkSenseBuf(BLANK_CHECK, E_END_OF_DATA, sam_stat);
put_unaligned_be32(save_sense, &sense[3]);
return 0;
break;
default:
MHVTL_LOG("Unknown blk header at offset %u"
" - Abort read cmd", c_pos->blk_number);
mkSenseBuf(MEDIUM_ERROR, E_UNRECOVERED_READ, sam_stat);
return 0;
break;
}
/* The tape block is compressed. Save field values we will need after
the read causes the tape block to advance.
*/
blk_size = c_pos->blk_size;
disk_blk_size = c_pos->disk_blk_size;
/* We have a data block to read.
Only read upto size of allocated buffer by initiator
*/
tgtsize = min(request_sz, blk_size);
/* If the tape block is uncompressed, we can read the number of bytes
we need directly into the scsi read buffer and we are done.
*/
if (!(c_pos->blk_flags & BLKHDR_FLG_COMPRESSED)) {
if (read_tape_block(buf, tgtsize, sam_stat) != tgtsize) {
MHVTL_LOG("read failed, %s", strerror(errno));
mkSenseBuf(MEDIUM_ERROR, E_UNRECOVERED_READ, sam_stat);
return 0;
}
if (tgtsize != request_sz)
mk_sense_short_block(request_sz, tgtsize, sam_stat);
else if (!sili) {
if (request_sz < blk_size)
mk_sense_short_block(request_sz, blk_size, sam_stat);
}
lu_ssc.bytesRead_I += tgtsize;
lu_ssc.bytesRead_M += tgtsize;
return tgtsize;
}
/* Malloc a buffer to hold the compressed data, and read the
data into it.
*/
cbuf = malloc(disk_blk_size);
if (!cbuf) {
MHVTL_LOG("Out of memory: %d", __LINE__);
mkSenseBuf(MEDIUM_ERROR, E_DECOMPRESSION_CRC, sam_stat);
return 0;
}
nread = read_tape_block(cbuf, disk_blk_size, sam_stat);
if (nread != disk_blk_size) {
MHVTL_LOG("read failed, %s", strerror(errno));
mkSenseBuf(MEDIUM_ERROR, E_UNRECOVERED_READ, sam_stat);
free(cbuf);
return 0;
}
rc = tgtsize;
uncompress_sz = blk_size;
/* If the scsi read buffer is at least as big as the size of
the uncompressed data then we can uncompress directly into
the read buffer. If not, then we need an extra buffer to
uncompress into, then memcpy the subrange we need to the
read buffer.
*/
if (tgtsize == blk_size) {
/* block sizes match, uncompress directly into buf */
z = uncompress(buf, &uncompress_sz, cbuf, disk_blk_size);
} else {
/* Initiator hasn't requested same size as data block */
if ((c2buf = malloc(uncompress_sz)) == NULL) {
MHVTL_LOG("Out of memory: %d", __LINE__);
mkSenseBuf(MEDIUM_ERROR, E_DECOMPRESSION_CRC, sam_stat);
free(cbuf);
return 0;
}
z = uncompress(c2buf, &uncompress_sz, cbuf, disk_blk_size);
/* Now copy 'requested size' of data into buffer */
memcpy(buf, c2buf, tgtsize);
free(c2buf);
}
lu_ssc.bytesRead_I += blk_size;
lu_ssc.bytesRead_M += disk_blk_size;
switch (z) {
case Z_OK:
MHVTL_DBG(2, "Read %u (%u) bytes of compressed"
" data, have %u bytes for result",
(uint32_t)nread, disk_blk_size,
tgtsize);
break;
case Z_MEM_ERROR:
MHVTL_LOG("Not enough memory to decompress");
mkSenseBuf(MEDIUM_ERROR, E_DECOMPRESSION_CRC, sam_stat);
rc = 0;
break;
case Z_DATA_ERROR:
MHVTL_LOG("Block corrupt or incomplete");
mkSenseBuf(MEDIUM_ERROR, E_DECOMPRESSION_CRC, sam_stat);
rc = 0;
break;
case Z_BUF_ERROR:
MHVTL_LOG("Not enough memory in destination buf");
mkSenseBuf(MEDIUM_ERROR, E_DECOMPRESSION_CRC, sam_stat);
rc = 0;
break;
}
free(cbuf);
if (rc != request_sz)
mk_sense_short_block(request_sz, rc, sam_stat);
else if (!sili) {
if (request_sz < blk_size)
mk_sense_short_block(request_sz, blk_size, sam_stat);
}
return rc;
}
/*
* Return number of bytes written to 'file'
*
* Zero on error with sense buffer already filled in
*/
int writeBlock(struct scsi_cmd *cmd, uint32_t src_sz)
{
Bytef *dest_buf;
uLong dest_len;
uLong src_len = src_sz;
uint8_t *sam_stat = &cmd->dbuf_p->sam_stat;
uint8_t *src_buf = cmd->dbuf_p->data;
struct priv_lu_ssc *lu_priv;
uint64_t current_position;
int rc;
int z;
lu_priv = cmd->lu->lu_private;
/* Determine whether or not to store the crypto info in the tape
* blk_header.
* We may adjust this decision for the 3592. (See ibm_3592_xx.pm)
*/
lu_priv->cryptop = lu_priv->ENCRYPT_MODE == 2 ? &encryption : NULL;
if (lu_priv->pm->valid_encryption_media)
lu_priv->pm->valid_encryption_media(cmd);
if (*lu_priv->compressionFactor) {
dest_len = compressBound(src_sz);
dest_buf = malloc(dest_len);
if (!dest_buf) {
MHVTL_LOG("malloc(%d) failed", (int)dest_len);
mkSenseBuf(MEDIUM_ERROR, E_WRITE_ERROR, sam_stat);
return 0;
}
z = compress2(dest_buf, &dest_len, src_buf, src_sz,
*lu_priv->compressionFactor);
if (z != Z_OK) {
switch (z) {
case Z_MEM_ERROR:
MHVTL_LOG("Not enough memory to compress "
"data");
break;
case Z_BUF_ERROR:
MHVTL_LOG("Not enough memory in destination "
"buf to compress data");
break;
case Z_DATA_ERROR:
MHVTL_LOG("Input data corrupt / incomplete");
break;
}
mkSenseBuf(HARDWARE_ERROR, E_COMPRESSION_CHECK,
sam_stat);
return 0;
}
MHVTL_DBG(2, "Compression: Orig %d, after comp: %ld"
", Compression factor: %d",
src_sz, (unsigned long)dest_len,
*lu_priv->compressionFactor);
} else {
dest_buf = src_buf;
dest_len = 0; /* no compression */
}
rc = write_tape_block(dest_buf, src_len, dest_len, lu_priv->cryptop, sam_stat);
if (*lu_priv->compressionFactor != Z_NO_COMPRESSION) {
free(dest_buf);
lu_priv->bytesWritten_I += src_len;
lu_priv->bytesWritten_M += dest_len;
} else {
lu_priv->bytesWritten_I += src_len;
lu_priv->bytesWritten_M += dest_len;
}
if (rc < 0)
return 0;
current_position = current_tape_offset();
if (current_position <= lu_priv->prog_early_warning_position) {
put_unaligned_be64(lu_priv->max_capacity - current_position,
&mam.remaining_capacity);
} else if (current_position >= lu_priv->max_capacity) {
mam.remaining_capacity = 0L;
MHVTL_DBG(1, "End of Medium - VOLUME_OVERFLOW/EOM");
mkSenseBuf(VOLUME_OVERFLOW | SD_EOM, E_EOM, sam_stat);
} else if (current_position >= lu_priv->early_warning_position) {
mam.remaining_capacity = 0L;
MHVTL_DBG(1, "End of Medium (early warning) - Setting EOM flag");
mkSenseBuf(NO_SENSE | SD_EOM, NO_ADDITIONAL_SENSE, sam_stat);
} else if (current_position >= lu_priv->prog_early_warning_position) {
mam.remaining_capacity = 0L;
MHVTL_DBG(1, "End of Medium - Programmable Early Warning");
mkSenseBuf(NO_SENSE | SD_EOM,
E_PROGRAMMABLE_EARLY_WARNING, sam_stat);
}
return src_len;
}
/*
* Space over (to) x filemarks. Setmarks not supported as yet.
*/
void resp_space(int32_t count, int code, uint8_t *sam_stat)
{
switch (code) {
/* Space 'count' blocks */
case 0:
if (count >= 0)
position_blocks_forw(count, sam_stat);
else
position_blocks_back(-count, sam_stat);
break;
/* Space 'count' filemarks */
case 1:
if (count >= 0)
position_filemarks_forw(count, sam_stat);
else
position_filemarks_back(-count, sam_stat);
break;
/* Space to end-of-data - Ignore 'count' */
case 3:
position_to_eod(sam_stat);
break;
default:
mkSenseBuf(ILLEGAL_REQUEST,E_INVALID_FIELD_IN_CDB, sam_stat);
break;
}
}
#ifdef MHVTL_DEBUG
static char *sps_pg0 = "Tape Data Encyrption in Support page";
static char *sps_pg1 = "Tape Data Encyrption Out Support Page";
static char *sps_pg16 = "Data Encryption Capabilities page";
static char *sps_pg17 = "Supported key formats page";
static char *sps_pg18 = "Data Encryption management capabilities page";
static char *sps_pg32 = "Data Encryption Status page";
static char *sps_pg33 = "Next Block Encryption Status Page";
static char *sps_pg48 = "Random Number Page";
static char *sps_pg49 = "Device Server Key Wrapping Public Key page";
static char *sps_reserved = "Security Protcol Specific : reserved value";
static char *lookup_sp_specific(uint16_t field)
{
MHVTL_DBG(3, "Lookup %d", field);
switch (field) {
case 0: return sps_pg0;
case 1: return sps_pg1;
case 16: return sps_pg16;
case 17: return sps_pg17;
case 18: return sps_pg18;
case 32: return sps_pg32;
case 33: return sps_pg33;
case 48: return sps_pg48;
case 49: return sps_pg49;
default: return sps_reserved;
break;
}
}
#endif
#define SUPPORTED_SECURITY_PROTOCOL_LIST 0
#define CERTIFICATE_DATA 1
#define SECURITY_PROTOCOL_INFORMATION 0
#define TAPE_DATA_ENCRYPTION 0x20
/* FIXME:
* Took this certificate from my Ubuntu install
* /usr/share/doc/libssl-dev/demos/tunala/CA.pem
* I wonder if RIAA is in NZ ?
*
* Need to insert a valid certificate of my own here...
*/