Structures.hpp 54 KB
Newer Older
1
2
3
4
5
6
7
/*
 * @project        The CERN Tape Archive (CTA)
 * @copyright      Copyright(C) 2021 CERN
 * @license        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 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 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.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17
18
19
20

#pragma once

#include <sstream>
21
#include <iomanip>
22
#include <algorithm>
23
#include <arpa/inet.h>
24
#include <endian.h>
Eric Cano's avatar
Eric Cano committed
25
#include <string.h>
26
27
#include <scsi/sg.h>
#include <climits>
Eric Cano's avatar
Eric Cano committed
28
#include <cstdio>
29

30
#include "Constants.hpp"
31
#include "common/exception/Exception.hpp"
32

33
34
namespace castor {
namespace tape {
35
namespace SCSI {
36
  const unsigned int defaultTimeout=900000; //millisecs
37
38
39
40
41
42
43
44
45
46
47
48

  /**
   * Maximum number of tape wraps.
   *
   * This is used to determine the maximum size of the response from the REOWP command,
   * which returns 12 bytes per wrap, plus a 4-byte header.
   *
   * LTO-8 has 208 physical wraps. LTO-9 has 280 wraps. This number should be adjusted
   * upwards when the LTO-10 specification is announced.
   */
  const unsigned int maxLTOTapeWraps = 280;

49
50
51
52
53
54
55
56
  /**
   * Structures as defined in the SCSI specifications, and helper functions for them.
   * SPC-4 (SCSI primary commands) can be found at:
   * http://hackipedia.org/Hardware/SCSI/Primary%20Commands/SCSI%20Primary%20Commands%20-%204.pdf
   * 
   * and SSC-3 (SCSI stream commands, i.e. tape drives) at:
   * http://hackipedia.org/Hardware/SCSI/Stream%20Commands/SCSI%20Stream%20Commands%20-%203.pdf
   */
57
  namespace Structures {
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

    /**
     * Helper template to zero a structure. Small boilerplate reduction.
     * Should not be used with classes with virtual tables! With a zeroed
     * out virtual table pointer, this will be detected soon enough.
     * @param s pointer the struct/class.
     */
    template <typename C>
    void zeroStruct(C * s) {
      memset (s, 0, sizeof(C));
    }
    
    /**
     * Class wrapping around Linux' SG_IO struct, providing
     * zeroing and automatic filling up for the mandatory structures
     * (cdb, databuffer, sense buffer, magic 'S', and default timeout).
     * Make it look like a bare sg_io_hdr_t when using & operator.
     * Another little boilerplate killer.
     */
    class LinuxSGIO_t: public sg_io_hdr_t {
    public:
79
      LinuxSGIO_t() { zeroStruct(this); interface_id = 'S'; timeout = defaultTimeout; }
80
81
82
83
      template <typename T>
      void setCDB(T * cdb) { cmdp = (unsigned char *)cdb; cmd_len = sizeof(T); }
      
      template <typename T>
84
      void setSenseBuffer(T * senseBuff)  
85
86
      { 
        if (sizeof(T) > UCHAR_MAX)
87
          throw cta::exception::Exception("sense structure too big in LinuxSGIO_t::setSense");
88
        mx_sb_len = (unsigned char) sizeof(T);
89
90
91
92
93
        sbp = (unsigned char *)senseBuff;
      }
      
      template <typename T>
      void setDataBuffer(T * dataBuff) { dxferp = dataBuff; dxfer_len = sizeof (T); }
Cristina Moraru's avatar
Cristina Moraru committed
94
95
96
97
98
99
100
101
102
103

      // If dataBuff is a pointer to a variable length array, sizeof will return
      // the size of one element. This function allows to manually set the buffer size
      template <typename T>
      void setDataBuffer(T * dataBuff, unsigned int dataBuffSize)
      {
        dxferp = dataBuff;
        dxfer_len = dataBuffSize;
      }

104
105
      sg_io_hdr_t * operator & () { return (sg_io_hdr_t *) this; }
    };
Eric Cano's avatar
Eric Cano committed
106
    
107
108
109
110
111
112
113
114
115
116
    /**
     * Helper function to deal with endianness.
     * @param t byte array in SCSI order representing a 64 bits number
     * @return 
     */
    inline uint64_t toU64(const unsigned char(& t)[8])
    {
      /* Like network, SCSI is BigEndian */
      return (uint64_t) ntohl ( (*(uint64_t *) t << 32) >> 32)  << 32 | ntohl(*(uint64_t *) t >>32);
    }  
117
118

    /**
119
     * Helper function to deal with endianness: 6-byte version
120
121
122
123
124
125
126
127
     *
     * The REOWP SCSI command assigns 48 bits to store the LOGICAL OBJECT IDENTIFIER (although other
     * commands use 64 bits or sometimes 32 bits). This function converts the 48-bit byte array
     * into a 64-bit unsigned integer.
     *
     * @param t byte array in SCSI order representing a 48-bit number
     * @return 64-bit unsigned integer
     */
128
    inline uint64_t toU64(const unsigned char(& t)[6])
129
130
131
132
133
    {
      /* Like network, SCSI is BigEndian */
      return (uint64_t) ntohl ( (*(uint64_t *) t << 32) >> 16)  << 32 | ntohl(*(uint64_t *) t >>16);
    }

Eric Cano's avatar
Eric Cano committed
134
135
136
137
138
    /**
     * Helper function to deal with endianness.
     * @param t byte array in SCSI order representing a 32 bits number
     * @return 
     */
139
    inline uint32_t toU32(const unsigned char(& t)[4])
Eric Cano's avatar
Eric Cano committed
140
141
142
143
144
    {
      /* Like network, SCSI is BigEndian */
      return ntohl (*((uint32_t *) t));
    }

145
146
147
148
149
150
151
152
     /**
     * Helper function to deal with endianness.
     * for 3 bytes! fields in SCSI replies 
     * @param t byte array in SCSI order representing a 32 bits number
     * @return 
     */
    inline uint32_t toU32(const unsigned char(& t)[3])
    {
153
154
155
156
157
      union {
	unsigned char tmp[4];
	uint32_t val;
      } u;
      u.tmp[0]=0;u.tmp[1]=t[0];u.tmp[2]=t[1];u.tmp[3]=t[2];
158
159
      
      /* Like network, SCSI is BigEndian */
160
      return ntohl (u.val);
161
162
    }
    
163
164
165
166
167
168
169
170
171
172
173
174
     /**
     * Helper function to deal with endianness.
     * for signed values
     * @param t byte array in SCSI order representing a 32 bits number
     * @return 
     */
    inline int32_t toS32(const unsigned char(& t)[4])
    {
      /* Like network, SCSI is BigEndian */
      return (int32_t)(ntohl (*((uint32_t *) t)));
    }
    
175
    /**
176
177
178
     * Helper function to deal with endianness.
     * @param t byte array in SCSI order representing a 16 bits number
     * @return 
179
     */
180
    inline uint16_t toU16(const unsigned char(& t)[2])
181
182
    {
      /* Like network, SCSI is BigEndian */
183
      return ntohs (*((uint16_t *) t));
184
185
    }
    
Eric Cano's avatar
Eric Cano committed
186
    /**
187
     * Helper function setting in place a 32 bits SCSI number from a value
Eric Cano's avatar
Eric Cano committed
188
     * expressed in the local endianness.
189
     * @param t pointer to the char array at the 32 bits value position.
Eric Cano's avatar
Eric Cano committed
190
191
     * @param val the value.
     */
192
193
    inline void setU32(unsigned char(& t)[4], uint32_t val) {
      *((uint32_t *) t) = htonl(val);
Eric Cano's avatar
Eric Cano committed
194
195
    }
    
Eric Cano's avatar
Eric Cano committed
196
    /**
197
198
199
200
     * Helper function setting in place a 16 bits SCSI number from a value
     * expressed in the local endianness.
     * @param t pointer to the char array at the 16 bits value position.
     * @param val the value.
Eric Cano's avatar
Eric Cano committed
201
     */
202
203
    inline void setU16(unsigned char(& t)[2], uint16_t val) {
      *((uint16_t *) t) = htons(val);
Eric Cano's avatar
Eric Cano committed
204
    }
205

206
207
208
209
210
211
212
213
214
215
    /**
     * Helper function setting in place a 64 bits SCSI number from a value
     * expressed in the local endianness.
     * @param t pointer to the char array at the 64 bits value position.
     * @param val the value.
     */
    inline void setU64(unsigned char(& t)[8], uint64_t val) {
        *((uint64_t *) t) = htobe64(val);
    }
    
Eric Cano's avatar
Eric Cano committed
216
    /**
217
218
     * Inquiry CDB as described in SPC-4.
     */
219
220
    class inquiryCDB_t {
    public:
Eric Cano's avatar
Eric Cano committed
221
      inquiryCDB_t() { zeroStruct(this); opCode = SCSI::Commands::INQUIRY; }
222
223
224
225
226
227
228
      unsigned char opCode;
      
      unsigned char EVPD : 1;
      unsigned char : 7;
      
      unsigned char pageCode;
      
229
      unsigned char allocationLength[2];
230
231
      
      unsigned char control;
232
    };
233
    
Eric Cano's avatar
Eric Cano committed
234
    /**
235
236
     * Inquiry data as described in SPC-4.
     */
Eric Cano's avatar
Eric Cano committed
237
238
239
    class inquiryData_t {
    public:
      inquiryData_t () { zeroStruct(this); }
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
      unsigned char perifDevType : 5;
      unsigned char perifQualifyer : 3;

      unsigned char : 7;
      unsigned char RMB : 1;

      unsigned char version : 8;

      unsigned char respDataFmt : 4;
      unsigned char HiSup : 1;
      unsigned char normACA : 1;
      unsigned char : 2;

      unsigned char addLength : 8;

      unsigned char protect : 1;
      unsigned char : 2;
      unsigned char threePC : 1;
      unsigned char TPGS : 2;
      unsigned char ACC : 1;
      unsigned char SCCS : 1;

      unsigned char addr16 : 1;
      unsigned char : 3;
      unsigned char multiP : 1;
      unsigned char VS1 : 1;
      unsigned char encServ : 1;
      unsigned char : 1;

      unsigned char VS2 : 1;
      unsigned char cmdQue : 1;
      unsigned char : 2;
      unsigned char sync : 1;
      unsigned char wbus16 : 1;
      unsigned char : 2;

      char T10Vendor[8];
      char prodId[16];
      char prodRevLvl[4];
      char vendorSpecific1[20];

      unsigned char IUS : 1;
      unsigned char QAS : 1;
      unsigned char clocking : 2;
      unsigned char : 4;

      unsigned char reserved1;

288
      unsigned char versionDescriptor[8][2];
289
290
291

      unsigned char reserved2[22];
      unsigned char vendorSpecific2[1];
Eric Cano's avatar
Eric Cano committed
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
   
   /**
    * Oracle T10K Inquiry data
    */
    class inquiryDataT10k_t {
    public:
      inquiryDataT10k_t () { zeroStruct(this); }
      unsigned char perifDevType : 5;
      unsigned char perifQualifyer : 3;

      unsigned char : 7;
      unsigned char RMB : 1;

      unsigned char version;

      unsigned char respDataFmt : 4;
      unsigned char HiSup : 1;
      unsigned char normACA : 1;
      unsigned char RSVD1: 1;
      unsigned char AERC: 1;

      unsigned char addLength;

      unsigned char protect : 1;
      unsigned char : 2;
      unsigned char threePC : 1;
      unsigned char TPGS : 2;
      unsigned char ACC : 1;
      unsigned char SCCS : 1;

      unsigned char : 3;
      unsigned char mChngr: 1;
      unsigned char multiP : 1;
      unsigned char VS1 : 1;
      unsigned char encServ : 1;
      unsigned char bQue : 1;

      unsigned char VS2 : 1;
      unsigned char cmdQue : 1;
      unsigned char RSVD2: 1;
      unsigned char linked: 1;
      unsigned char : 3;
      unsigned char relAdr : 1;

      char vendorId[8];
      char prodId[16];
      char prodRevLvl[8];
      char vendorSpecific1[14];

      unsigned char keyMgmt;

      unsigned char CSL : 1;
      unsigned char DCMP : 1;
      unsigned char volSafe : 1;
      unsigned char libAtt : 1;
      unsigned char encr : 1;
      unsigned char : 3;

      unsigned char reserved1[2];

      unsigned char versionDescriptor[8][2];
    };
 
   /**
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
     * Inquiry unit serial number vital product data as described in SPC-4.
     */
    class inquiryUnitSerialNumberData_t {
    public:
      inquiryUnitSerialNumberData_t() { zeroStruct(this); }
      // byte 0
      unsigned char peripheralDeviceType: 5; // (000b) connected to this LUN
      unsigned char peripheralQualifier : 3; // (01h) tape drive  
      
      // byte 1
      unsigned char pageCode;                // (80h) Vital Product Data page for serial
      
      // byte 2
      unsigned char :8;                      // Reserved
      
      // byte 3
      unsigned char pageLength;              // n-3
      
      // bytes 4-n
      char productSerialNumber[12];          // 12 bytes for T10000&IBM, 10 for LTO
    };
    
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
     * LOCATE(10) CDB as described in SSC-3.
     */
    class locate10CDB_t {
    public:
      locate10CDB_t() {
        zeroStruct(this);
        opCode = SCSI::Commands::LOCATE_10; 
      }
      // byte 0
      unsigned char opCode;                // OPERATION CODE (2Bh)
      
      // byte 1
      unsigned char IMMED : 1;             // Immediate 
      unsigned char CP    : 1;             // Change Partition
      unsigned char BT    : 1;             // Block address Type
      unsigned char       : 5;             // Reserved
      
      // byte 2
      unsigned char       : 8;             // Reserved
      
      // bytes 3-6
      unsigned char logicalObjectID[4] ;   // Logical object identifier or block address
            
      // byte 7
      unsigned char        :8;             // Reserved
      
      // byte 8
      unsigned char partition;             // Partition
            
      // byte 9
      unsigned char control;               // Control byte
    };
    
413
    /**
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
     * READ POSITION CDB as described in SSC-3.
     */
    class readPositionCDB_t {
    public:
      readPositionCDB_t() {
        zeroStruct(this);
        opCode = SCSI::Commands::READ_POSITION; 
      }
      // byte 0
      unsigned char opCode;                // OPERATION CODE (34h)
      
      // byte 1 
      // *note* for T10000 we have BT:1, LONG:1, TCLP:1, Reserved:5
      unsigned char serviceAction: 5;      // Service action to choice FORM
      unsigned char              : 3;      // Reserved
      
      // bytes 2-6
      unsigned char reserved[5];           // Reserved
      
      // bytes 7-8 
Eric Cano's avatar
Eric Cano committed
434
      unsigned char allocationLength[2] ;  // used for EXTENDENT FORM
435
436
437
438
439
            
      // byte 9
      unsigned char control;               // Control byte
    };
    
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
     * READ POSITION  data format, short form as described in SSC-3.
     */
    class readPositionDataShortForm_t {
    public:
      readPositionDataShortForm_t() { zeroStruct(this); }
      // byte 0
      unsigned char BPEW :1;                // Beyond Programmable Early Warning
      unsigned char PERR :1;                // Position ERroR
      unsigned char LOLU :1;                // Logical Object Location Unknown or Block Position Unknown(BPU) for T10000 
      unsigned char      :1;                // Reserved
      unsigned char BYCU :1;                // BYte Count Unknown 
      unsigned char LOCU :1;                // Logical Object Count Unknown or Block Count Unknown(BCU) for T10000 
      unsigned char EOP  :1;                // End Of Partition
      unsigned char BOP  :1;                // Beginning of Partition
      
      // byte 1 
      unsigned char partitionNumber;        // Service action to choice FORM
      
      // bytes 2-3
      unsigned char reserved[2];            // Reserved
      
      // bytes 4-7 
      unsigned char firstBlockLocation[4];  // First Logical object location in SSC3,IBM,LTO
      
      // bytes 8-11
      unsigned char lastBlockLocation[4];   // Last Logical object location in SSC3,IBM,LTO 
      
      // byte 12
      unsigned char    :8;                  // Reserved
      
      // bytes 13-15
      unsigned char blocksInBuffer[3];      // Number of logical objects in object buffer 
      
      // bytes 16-19
      unsigned char bytesInBuffer[4];       // Number if bytes in object buffer
    };
    
478
    /**
479
480
481
482
     * LOG SELECT CDB as described in SPC-4.
     */
    class logSelectCDB_t {
    public:
Eric Cano's avatar
Eric Cano committed
483
484
485
486
      logSelectCDB_t() {
        zeroStruct(this);
        opCode = SCSI::Commands::LOG_SELECT; 
      }
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
      // byte 0
      unsigned char opCode;                // OPERATION CODE (4Ch)
      
      // byte 1
      unsigned char SP : 1;                // the Save Parameters
      unsigned char PCR: 1;                // the Parameter Code Reset
      unsigned char    : 6;                // Reserved 
      
      // byte 2
      unsigned char pageCode: 6;           // PAGE CODE
      unsigned char PC: 2;                 // the Page Control
      
      // byte 3
      unsigned char subPageCode;           // SUBPAGE CODE (Reserved for T10000)
      
      // bytes 4-6
      unsigned char reserved[3];           // Reserved
      
      // bytes 7-8
      unsigned char parameterListLength[2];// PARAMETER LIST LENGTH
      
      // byte 9
Eric Cano's avatar
Eric Cano committed
509
      unsigned char control;               // CONTROL
510
    };
511

Eric Cano's avatar
Eric Cano committed
512
    /**
513
     * Log sense CDB as described in SPC-4, 
Eric Cano's avatar
Eric Cano committed
514
515
516
     */
    class logSenseCDB_t {
    public:
Eric Cano's avatar
Eric Cano committed
517
      logSenseCDB_t() { zeroStruct(this); opCode = SCSI::Commands::LOG_SENSE; }
Eric Cano's avatar
Eric Cano committed
518
519
520
      unsigned char opCode;
      
      unsigned char SP : 1;
521
522
      unsigned char PPC: 1;
      unsigned char :6;
Eric Cano's avatar
Eric Cano committed
523
524
525
526
      
      unsigned char pageCode : 6;
      unsigned char PC : 2;
      
527
      unsigned char subPageCode;
Eric Cano's avatar
Eric Cano committed
528
      
529
530
      unsigned char reserved;
      
Eric Cano's avatar
Eric Cano committed
531
532
533
534
535
536
      unsigned char parameterPointer[2];
      
      unsigned char allocationLength[2];
      
      unsigned char control;
    };
Eric Cano's avatar
Eric Cano committed
537

538
    /**
539
540
     * Log sense Log Page Parameter Format as described in SPC-4, 
     */
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
    class logSenseParameterHeader_t  {
    public:
      // bytes 0-1
      unsigned char parameterCode [2];
      
      // byte 2
      unsigned char formatAndLinking : 2; // reserved and List Parameter bits
      unsigned char TMC : 2;              // Threshold Met Criteria
      unsigned char ETC : 1;              // Enable Threshold Comparison
      unsigned char TSD : 1;              // Target Save Disable
      unsigned char : 1;                  // DS Disable Save for T10000
      unsigned char DU : 1;               // Disable Update 
      
      // byte 3
      unsigned char parameterLength;      // n-3          
    };
    
    class logSenseParameter_t {
    public:
      // bytes 0-3
      logSenseParameterHeader_t header;
           
      // bytes 4-n
      unsigned char parameterValue[1];     // parameters have variable length 
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
      /**
       * Gets the parameter value
       * 
       * @return The value  of the log sense parameter as uint64_t.
       *         If we have a parameter length more than 8 bytes the returning
       *         value is not determined. 
       */
      inline uint64_t getU64Value()  {
        union {
          unsigned char tmp[8];
          uint64_t val64;
        } u;
        
        u.tmp[0]=(header.parameterLength>0)?parameterValue[0]:0;
        u.tmp[1]=(header.parameterLength>1)?parameterValue[1]:0;
        u.tmp[2]=(header.parameterLength>2)?parameterValue[2]:0;
        u.tmp[3]=(header.parameterLength>3)?parameterValue[3]:0;
        u.tmp[4]=(header.parameterLength>4)?parameterValue[4]:0;
        u.tmp[5]=(header.parameterLength>5)?parameterValue[5]:0;
        u.tmp[6]=(header.parameterLength>6)?parameterValue[6]:0;
        u.tmp[7]=(header.parameterLength>7)?parameterValue[7]:0;

        u.val64 = be64toh(u.val64);
     
        return u.val64>>(64-(header.parameterLength<<3));     
      }
      
      /**
       * Gets the parameter value.
       * 
       * @return The value  of the log sense parameter as int64_t.
       *         If we have a parameter length more than 8 bytes the returning
       *         value is not determined. 
       */
      inline int64_t getS64Value()  {
        union {
          unsigned char tmp[8];
          uint64_t val64U;
          int64_t  val64S;
        } u;
        
        u.tmp[0]=(header.parameterLength>0)?parameterValue[0]:0;
        u.tmp[1]=(header.parameterLength>1)?parameterValue[1]:0;
        u.tmp[2]=(header.parameterLength>2)?parameterValue[2]:0;
        u.tmp[3]=(header.parameterLength>3)?parameterValue[3]:0;
        u.tmp[4]=(header.parameterLength>4)?parameterValue[4]:0;
        u.tmp[5]=(header.parameterLength>5)?parameterValue[5]:0;
        u.tmp[6]=(header.parameterLength>6)?parameterValue[6]:0;
        u.tmp[7]=(header.parameterLength>7)?parameterValue[7]:0;
      
        u.val64U = be64toh(u.val64U);
        
        return  (u.val64S < 0?-(-u.val64S>> (64-(header.parameterLength<<3))):
          (u.val64S>>(64-(header.parameterLength<<3))));     
      }  
621
    };
622

623
    /**
624
625
     * Log sense Log Page Format as described in SPC-4, 
     */
626
627
628
629
630
631
632
633
634
635
636
637
638
639
    class logSenseLogPageHeader_t {
    public:
      // byte 0
      unsigned char pageCode : 6;
      unsigned char SPF: 1;          // the Subpage format
      unsigned char DS: 1;           // the Disable Slave bit
      
      // byte 1
      unsigned char subPageCode;
      
      // bytes 2-3
      unsigned char pageLength[2];   // n-3 number of bytes without header   
    };
    /**
640
641
     * Log sense Log Page Format as described in SPC-4, 
     */
642
643
644
645
646
647
648
649
650
    class logSenseLogPage_t {
    public:
      // bytes 0-3
      logSenseLogPageHeader_t header;
      
      // bytes 4-n      
      logSenseParameter_t parameters [1]; // parameters have variable length
    };
    
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
    /**
     * MODE SENSE(6) CDB as described in SPC-4.
     */
    class modeSense6CDB_t {
    public:
      modeSense6CDB_t() {
        zeroStruct(this);
        opCode = SCSI::Commands::MODE_SENSE_6; 
      }
      // byte 0
      unsigned char opCode;           // OPERATION CODE (1Ah)
      
      // byte 1 
      unsigned char     : 3;          // Reserved
      unsigned char DBD : 1;          // Disable Block Descriptors
      unsigned char     : 4;          // Reserved  
      
      // byte 2
      unsigned char pageCode : 6;     // Page code
      unsigned char PC       : 2;     // Page Control
            
      // byte3  
      unsigned char subPageCode ;     // Subpage code
      
      // byte4  
676
      unsigned char allocationLength; // The maximum number of bytes to be transferred
677
678
679
680
            
      // byte 5
      unsigned char control;          // Control byte
    };
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
    /**
     * MODE SENSE(6) MODE SELECT(6) parameter header as described in SPC-4.
     */
    class modeParameterHeader6_t {
    public:
      // byte 0
      unsigned char modeDataLength;   // The mode data length does not include itself 
      
      // byte 1 
      unsigned char mediumType;       // The medium type in the drive
      
      // byte 2
      /* in SPC-4 we have device-specific parameter byte here
       * but from all drive specifications the fields are the same
       * so we use them here.
       */
      unsigned char speed        : 4; // Read/write speed
      unsigned char bufferedMode : 3; // Returns after data is in the buffer or on the medium
      unsigned char WP           : 1; // Write Protect
            
      // byte3  
      unsigned char blockDescriptorLength ; //  (08h) or (00h)
    };
    
    /**
     * MODE SENSE(6,10) and MODE SELECT(6,10) block descriptor as described in SPC-4.
     */
    class modeParameterBlockDecriptor_t {
    public:
      // byte 0
      unsigned char densityCode;       // Density code
      
      // bytes 1-3 
      unsigned char numberOfBlocks[3]; // Number of block or block count
      
      // byte 4
      unsigned char : 8;               // Reserved
                 
      // bytes 5-7 
      unsigned char blockLength[3] ;   //  Block length
    };
    
    /**
     * MODE SENSE(6) or MODE SENSE(10) mode page 10h: Device Configuration.
     * There is no description in SPC-4 or SSC-3.
     * We use descriptions from: 
     * IBM System Storage Tape Drive 3592 SCSI Reference,
     * Sun StorageTekTM T10000 Tape Drive Fibre Channel Interface Reference Manual,
     * IBM TotalStorage LTO Ultrium Tape Drive SCSI Reference.
    */
    class modePageDeviceConfiguration_t {
    public:
      // byte 0
      unsigned char pageCode :6;          // Page code (10h)
      unsigned char SPF      :1;          // SubPage Format (0b)
      unsigned char PS       :1;          // Parameters Savable
      
      // byte 1 
      unsigned char pageLength;           // (0Eh)
      
      // byte 2
      unsigned char activeFormat : 5;     // Active Format
      unsigned char CAF          : 1;     // Change Active Format 
      unsigned char CAP          : 1;     // Change Active Partition 
      unsigned char              : 1;     // Reserved
                 
      // byte 3
      unsigned char activePartition ;     //  Active Partition
      
      // byte 4
      unsigned char writeBufferFullRatio; // Write object buffer full ratio
      
      // byte 5
      unsigned char readBufferEmptyRatio; // Read object buffer empty ratio
      
      // bytes 6-7
      unsigned char writeDelayTime[2];    // Write delay time in 100ms for IBM, LTO and in sec for T1000
      
      // byte 8
      unsigned char REW : 1;  // Report Early Warning
      unsigned char RBO : 1;  // Recover Buffer Order
      unsigned char SOCF: 2;  // Stop On Consecutive Filemarks
      unsigned char AVC : 1;  // Automatic Velocity Control
      unsigned char RSMK : 1; // Report SetMarKs (obsolete for IBM,LTO)
      unsigned char LOIS : 1; // Logical Object ID Supported or Block IDs Supported for T10000
      unsigned char OBR  : 1; // Object Buffer Recovery or Data Buffer Recovery for T10000
      
      // byte 9
      unsigned char gapSize;  // Obsolete for IBM, LTO
      
      // byte 10
      unsigned char BAM : 1;  // Block Address Mode or reserved for T10000
      unsigned char BAML: 1;  // Block Address Mode Lock or reserved for T10000
      unsigned char SWP : 1;  // Soft Write Protect
      unsigned char SEW : 1;  // Synchronize at Early Warning 
      unsigned char EEG : 1;  // EOD Enabled Generation
      unsigned char eodDefined :3; // End Of Data
      
      // bytes 11-13
      unsigned char bufSizeAtEarlyWarning[3]; // Object buffer size at early warning
      
      // byte 14
      unsigned char selectDataComprAlgorithm; // Select data compression algorithm
      
      // byte 15
      unsigned char PRMWP  : 1;        // PeRManent Write Protect
      unsigned char PERSWP : 1;        // PERSistent Write Protect
      unsigned char ASOCWP : 1;        // ASsOCiated Write Protect
      unsigned char rewindOnReset : 2; // Reserved for T10000
      unsigned char OIR  : 1;          // Only If Reserved  or reserved for T10000
      unsigned char WTRE : 2;          // WORM Tamper Read Enable
    };
    
    class modeSenseDeviceConfiguration_t {
    public:
      modeSenseDeviceConfiguration_t() { zeroStruct(this); }
      modeParameterHeader6_t header;
      modeParameterBlockDecriptor_t blockDescriptor;
      modePageDeviceConfiguration_t modePage;
    };
    
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
    /**
     * MODE SENSE(6) or MODE SENSE(10) mode page 0Ah: Control Data Protection.
     * as described in SSC-5.
     */
    class modePageControlDataProtection_t {
    public:
      // byte 0
      unsigned char pageCode :6;       // Page code (0Ah)
      unsigned char SPF      :1;       // SubPage Format (1b)
      unsigned char PS       :1;       // Parameters Savable 
                                       // 0b required for MODE SELECT IBM,LTO
                                       // 1b returned in MODE SENSE IBM, LTO
                                       // 0b Not supported for T10000
      
      // byte 1 
      unsigned char subpageCode;       // SubPage code (F0h)  
      
      // bytes 2-3
      unsigned char pageLength[2];     // Page length (n - 3) 1Ch for IBM,LTO
      
      // byte 4
      unsigned char LBPMethod;         // LBP method
      
      // byte 5
      unsigned char LBPInformationLength : 6; // LBP information length 
      unsigned char                      : 2; // Reserved
      
      // byte 6
      unsigned char : 5;               // Reserved
      unsigned char RBDP : 1;          // Recover Buffered Data Protected
      unsigned char LBP_R : 1;         // Logical blocks protected during read
      unsigned char LBP_W : 1;         // Logical blocks protected during write
      
      // byte 7
      unsigned char : 4;               // Reserved
      unsigned char T10PIexponent : 4; // T1000 only for T10 PI mode
      
      // bytes 8-31
      unsigned char reserved[24];      // Reserved. Added for IBM, LTO and do  
                                       // not used by T10000
    };
  
    /**
     * MODE SENSE(6) structure for mode page 0Ah: Control Data Protection.
     * as described in SSC-5.
     */
    class modeSenseControlDataProtection_t {
    public:
      modeSenseControlDataProtection_t() { zeroStruct(this); }
      modeParameterHeader6_t header;
      modeParameterBlockDecriptor_t blockDescriptor;
      modePageControlDataProtection_t modePage;
    };    
    
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
     /**
     * MODE SELECT(6) CDB as described in SPC-4.
     */
    class modeSelect6CDB_t {
    public:
      modeSelect6CDB_t() {
        zeroStruct(this);
        opCode = SCSI::Commands::MODE_SELECT_6; 
      }
      // byte 0
      unsigned char opCode;          // OPERATION CODE (15h)
      
      // byte 1 
      unsigned char SP : 1;          // Save Parameters
      unsigned char    : 3;          // Reserved
      unsigned char PF : 1;          // Page Format
      unsigned char    : 3;          // Reserved
      
      // bytes 2-3
      unsigned char reserved[2];     // Reserved
                  
      // byte 4
      unsigned char paramListLength; // Parameter list length
            
      // byte 5
      unsigned char control;         // Control byte
    };
    
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
    /**
     * TEST UNIT READY as described in SPC-4.
     */
    class testUnitReadyCDB_t {
    public:
      testUnitReadyCDB_t() {
        zeroStruct(this);
        opCode = SCSI::Commands::TEST_UNIT_READY; 
      }
      // byte 0
      unsigned char opCode;       // OPERATION CODE (00h)
      
      // byte 1 
      unsigned char      : 8;     // Reserved
            
      // byte 2
      unsigned char EDCC : 1;     // Enable Deferred CHECK CONDITION (IBM only)
      unsigned char      : 7;     // Reserved
            
      // byte 3-4  
      unsigned char reserverd[2]; // Reserved
            
      // byte 5
      unsigned char control;      // Control byte
    };
    
Eric Cano's avatar
Eric Cano committed
911
912
913
914
915
    /**
     * Part of a tape alert log page.
     * This structure does not need to be initialized, as the containing structure
     * (tapeAlertLogPage_t) will do it while initializing itself.
     */
Eric Cano's avatar
Eric Cano committed
916
917
    class tapeAlertLogParameter_t {
    public:
918
      unsigned char parameterCode [2];
Eric Cano's avatar
Eric Cano committed
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
      
      unsigned char formatAndLinking : 2;
      unsigned char TMC : 2;
      unsigned char ETC : 1;
      unsigned char TSD : 1;
      unsigned char : 1;
      unsigned char DU : 1;
      
      unsigned char parameterLength;
      
      unsigned char flag : 1;
      unsigned char : 7;
    };
    
    /**
934
     * Tape alert log page, returned by LOG SENSE. Defined in SSC-3, section 8.2.3 TapeAler log page.
Eric Cano's avatar
Eric Cano committed
935
     */
936
    template <int n>
Eric Cano's avatar
Eric Cano committed
937
938
    class tapeAlertLogPage_t {
    public:
Eric Cano's avatar
Eric Cano committed
939
      tapeAlertLogPage_t() { zeroStruct(this); }
Eric Cano's avatar
Eric Cano committed
940
941
942
943
944
      unsigned char pageCode : 6;
      unsigned char : 2;
      
      unsigned char subPageCode;
      
945
      unsigned char pageLength[2];
Eric Cano's avatar
Eric Cano committed
946
      
947
      tapeAlertLogParameter_t parameters [n];
Eric Cano's avatar
Eric Cano committed
948
949
      
      /**
950
951
       * Utility function computing the number of parameters. This converts a
       * length in bytes (as found in the struct) in a parameter count.
Eric Cano's avatar
Eric Cano committed
952
953
       * @return number of parameters.
       */
954
      unsigned int parameterNumber()  {
955
        unsigned int numFromLength = SCSI::Structures::toU16(pageLength) / sizeof (tapeAlertLogParameter_t);
956
957
        return numFromLength;
      }
Eric Cano's avatar
Eric Cano committed
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
    };
    
    /**
     * Sense buffer as defined in SPC-4, 
     * section 4.5.2 Descriptor format sense data and 
     * section 4.5.3 Fixed format sense data
     * The sense buffer size is stored in the form of
     * a single byte. Therefore, the constructor forbids
     * creation of a senseData_t structure bigger than
     * 255 bytes.
     * As the structure will be different depending on the response code,
     * everything after the first byte is represented by a union, which
     * can be any of the 2 forms (fixedFormat/descriptorFormat).
     * getXXXXX helper member function allow the getting of on of the other
     * version of the common fields.
     */
    template <int n>
    class senseData_t {
    public:
      senseData_t() {
        if (sizeof(*this) > 255)
979
          throw cta::exception::Exception("In SCSI::Structures::senseData_t::senseData_t(): size too big (> 255>");      
Eric Cano's avatar
Eric Cano committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        zeroStruct(this);
      }
      // byte 0
      unsigned char responseCode: 7;
      unsigned char : 1;
      // Following bytes can take 2 versions:
      union {
       struct {
          // byte 1
          unsigned char senseKey : 4;
          unsigned char : 4;
          // Additional sense code (byte 2))
          unsigned char ASC;
          // Additional sense code qualifier (byte 3)
          unsigned char ASCQ;
          // byte 4
          unsigned char : 7;
          unsigned char SDAT_OVFL : 1;
          // byte 5-6
          unsigned char reserved[2];
          // byte 7