github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/smart/types.go (about)

     1  /*
     2  Copyright 2018 The OpenEBS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6      http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  See the License for the specific language governing permissions and
    11  limitations under the License.
    12  */
    13  
    14  package smart
    15  
    16  // SCSI generic (sg)
    17  // See dxfer_direction http://sg.danny.cz/sg/p/sg_v3_ho.html
    18  const (
    19  	SGDxferNone      = -1     //SCSI Test Unit Ready command
    20  	SGDxferToDev     = -2     //SCSI WRITE command
    21  	SGDxferFromDev   = -3     //SCSI READ command
    22  	SGDxferToFromDev = -4     //relevant to indirect IO (otherwise it is treated like SGDxferFromDev)
    23  	SGInfoOk         = 0x0    //no sense, host nor driver "noise" or error
    24  	SGInfoOkMask     = 0x1    //indicates whether some error or status field is non-zero
    25  	SGIO             = 0x2285 //scsi generic ioctl command
    26  	DefaultTimeout   = 20000  //DefaultTimeout in millisecs
    27  )
    28  
    29  // ATA command being used
    30  const (
    31  	AtaIdentifyDevice = 0xec
    32  )
    33  
    34  // Constants being used by switch case for returning disk details
    35  const (
    36  	Compliance         = "Compliance"
    37  	Vendor             = "Vendor"
    38  	Capacity           = "Capacity"
    39  	LogicalSectorSize  = "LogicalSectorSize"
    40  	PhysicalSectorSize = "PhysicalSectorSize"
    41  	SerialNumber       = "SerialNumber"
    42  	WWN                = "LuWWNDeviceID"
    43  	FirmwareRev        = "FirmwareRevision"
    44  	ModelNumber        = "ModelNumber"
    45  	RPM                = "RPM"
    46  	ATAMajor           = "ATAMajorVersion"
    47  	ATAMinor           = "ATAMinorVersion"
    48  	AtATransport       = "AtaTransport"
    49  	SupportedBusType   = "SCSI"
    50  )
    51  
    52  // Constants being used as keys for sending map of errors
    53  const (
    54  	SCSIInqErr           = "SCSIInquiryError"
    55  	SCSIReadCapErr       = "SCSIReadcapacityError"
    56  	ATAIdentifyErr       = "AtaIdentifyError"
    57  	RPMErr               = "RPMError"
    58  	SCSiGetLBSizeErr     = "GetLogicalBlockSizeError"
    59  	DetectSCSITypeErr    = "DetectScsiTypeError"
    60  	errorCheckConditions = "errorCheckingConditions"
    61  )
    62  
    63  // ataMajorVersions are the major versions defined for an ATA device in ATA command set page
    64  // Table 9 of X3T13/2008D (ATA-3) Revision 7b, See http://www.scs.stanford.edu/11wi-cs140/pintos/specs/ata-3-std.pdf
    65  // Table 29 of T13/1699-D Revision 6a, See http://www.t13.org/documents/uploadeddocuments/docs2008/d1699r6a-ata8-acs.pdf
    66  // Table 45 of T13/2161-D Revision 5, See http://www.t13.org/Documents/UploadedDocuments/docs2013/d2161r5-ATAATAPI_Command_Set_-_3.pdf
    67  // Table 55 of T13/BSR INCITS 529 Revision 18 , See http://t13.org/Documents/UploadedDocuments/docs2017/di529r18-ATAATAPI_Command_Set_-_4.pdf
    68  var ataMajorVersions = map[int]string{
    69  	1:  "ATA-1",       //obsolete
    70  	2:  "ATA-2",       //obsolete
    71  	3:  "ATA-3",       //obsolete
    72  	4:  "ATA-ATAPI-4", //obsolete
    73  	5:  "ATA-ATAPI-5",
    74  	6:  "ATA-ATAPI-6",
    75  	7:  "ATA-ATAPI-7",
    76  	8:  "ATA8-ACS",
    77  	9:  "ACS-2",
    78  	10: "ACS-3",
    79  	11: "ACS-4",
    80  }
    81  
    82  // Table 10 of X3T13/2008D (ATA-3) Revision 7b, See http://www.scs.stanford.edu/11wi-cs140/pintos/specs/ata-3-std.pdf
    83  // Table 31 of T13/1699-D Revision 6a, See http://www.t13.org/documents/uploadeddocuments/docs2008/d1699r6a-ata8-acs.pdf
    84  // Table 47 of T13/2161-D Revision 5, See http://www.t13.org/Documents/UploadedDocuments/docs2013/d2161r5-ATAATAPI_Command_Set_-_3.pdf
    85  // Table 57 of T13/BSR INCITS 529 Revision 18 , See http://t13.org/Documents/UploadedDocuments/docs2017/di529r18-ATAATAPI_Command_Set_-_4.pdf
    86  var ataMinorVersions = map[uint16]string{
    87  	0x0001: "ATA-1 X3T9.2/781D prior to revision 4",       // obsolete
    88  	0x0002: "ATA-1 published, ANSI X3.221-1994",           // obsolete
    89  	0x0003: "ATA-1 X3T9.2/781D revision 4",                // obsolete
    90  	0x0004: "ATA-2 published, ANSI X3.279-1996",           // obsolete
    91  	0x0005: "ATA-2 X3T10/948D prior to revision 2k",       // obsolete
    92  	0x0006: "ATA-3 X3T10/2008D revision 1",                // obsolete
    93  	0x0007: "ATA-2 X3T10/948D revision 2k",                // obsolete
    94  	0x0008: "ATA-3 X3T10/2008D revision 0",                // obsolete
    95  	0x0009: "ATA-2 X3T10/948D revision 3",                 // obsolete
    96  	0x000a: "ATA-3 published, ANSI X3.298-1997",           // obsolete
    97  	0x000b: "ATA-3 X3T10/2008D revision 6",                // obsolete
    98  	0x000c: "ATA-3 X3T13/2008D revision 7 and 7a",         // obsolete
    99  	0x000d: "ATA/ATAPI-4 X3T13/1153D version 6",           // obsolete
   100  	0x000e: "ATA/ATAPI-4 T13/1153D version 13",            // obsolete
   101  	0x000f: "ATA/ATAPI-4 X3T13/1153D version 7",           // obsolete
   102  	0x0010: "ATA/ATAPI-4 T13/1153D version 18",            // obsolete
   103  	0x0011: "ATA/ATAPI-4 T13/1153D version 15",            // obsolete
   104  	0x0012: "ATA/ATAPI-4 published, ANSI NCITS 317-1998",  // obsolete
   105  	0x0013: "ATA/ATAPI-5 T13/1321D version 3",             // obsolete
   106  	0x0014: "ATA/ATAPI-4 T13/1153D version 14",            // obsolete
   107  	0x0015: "ATA/ATAPI-5 T13/1321D revision 1",            // obsolete
   108  	0x0016: "ATA/ATAPI-5 published, ANSI NCITS 340-2000",  // obsolete
   109  	0x0017: "ATA/ATAPI-4 T13/1153D revision 17",           // obsolete
   110  	0x0018: "ATA/ATAPI-6 T13/1410D version 0",             // obsolete
   111  	0x0019: "ATA/ATAPI-6 T13/1410D version 3a",            // obsolete
   112  	0x001a: "ATA/ATAPI-7 T13/1532D version 1",             // obsolete
   113  	0x001b: "ATA/ATAPI-6 T13/1410D version 2",             // obsolete
   114  	0x001c: "ATA/ATAPI-6 T13/1410D version 1",             // obsolete
   115  	0x001d: "ATA/ATAPI-7 published, ANSI INCITS 397-2005", // obsolete
   116  	0x001e: "ATA/ATAPI-7 T13/1532D version 0",             // obsolete
   117  	0x001f: "ACS-3 revision 3b",
   118  	0x0021: "ATA/ATAPI-7 T13/1532D version 4a",            // obsolete
   119  	0x0022: "ATA/ATAPI-6 published, ANSI INCITS 361-2002", // obsolete
   120  	0x0027: "ATA8-ACS version 3c",
   121  	0x0028: "ATA8-ACS version 6",
   122  	0x0029: "ATA8-ACS version 4",
   123  	0x0031: "ACS-2 revision 2",
   124  	0x0033: "ATA8-ACS version 3e",
   125  	0x0039: "ATA8-ACS version 4c",
   126  	0x0042: "ATA8-ACS version 3f",
   127  	0x0052: "ATA8-ACS version 3b",
   128  	0x005e: "ACS-4 revision 5",
   129  	0x006d: "ACS-3 revision 5",
   130  	0x0082: "ACS-2 published, ANSI INCITS 482-2012",
   131  	0x0107: "ATA8-ACS version 2d",
   132  	0x010a: "ACS-3 published, ANSI INCITS 522-2014",
   133  	0x0110: "ACS-2 revision 3",
   134  	0x011b: "ACS-3 revision 4",
   135  }
   136  
   137  // serialATAType contains the various types of serial ata transport
   138  var serialATAType = map[int]string{
   139  	0: " ATA8-AST",
   140  	1: " SATA 1.0a",
   141  	2: " SATA II Ext",
   142  	3: " SATA 2.5",
   143  	4: " SATA 2.6",
   144  	5: " SATA 3.0",
   145  	6: " SATA 3.1",
   146  	7: " SATA 3.2",
   147  }
   148  
   149  // ScsiInqAttr is the list of attributes fetched by SCSI Inquiry command
   150  var ScsiInqAttr = map[string]bool{
   151  	Compliance:   true,
   152  	Vendor:       true,
   153  	SerialNumber: true,
   154  	ModelNumber:  true,
   155  	FirmwareRev:  true,
   156  }
   157  
   158  // SimpleSCSIAttr is the list of attributes fetched by simple SCSI
   159  // commands such as readCapacity,etc
   160  var SimpleSCSIAttr = map[string]bool{
   161  	PhysicalSectorSize: true,
   162  	LogicalSectorSize:  true,
   163  	Capacity:           true,
   164  }
   165  
   166  // ATACSAttr is the list of attributes fetched using ATACSPage
   167  var ATACSAttr = map[string]bool{
   168  	WWN:                true,
   169  	AtATransport:       true,
   170  	ATAMajor:           true,
   171  	ATAMinor:           true,
   172  	RPM:                true,
   173  	LogicalSectorSize:  true,
   174  	PhysicalSectorSize: true,
   175  }
   176  
   177  // Identifier (devPath such as /dev/sda,etc) is an identifier for smart probe
   178  type Identifier struct {
   179  	DevPath string
   180  }
   181  
   182  // ATACSPage struct is an ATA IDENTIFY DEVICE struct. ATA8-ACS defines this as a page of 16-bit words.
   183  // _ (underscore) is used here to skip the words which we don't want to parse or get the data while parsing
   184  // the ata identify device data struct page.
   185  type ATACSPage struct {
   186  	_                 [10]uint16  // ...
   187  	SerialNumber      [20]byte    // Word 10..19, device serial number.
   188  	_                 [60]uint16  // ...
   189  	MajorVer          uint16      // Word 80, major version number.
   190  	MinorVer          uint16      // Word 81, minor version number.
   191  	_                 [24]uint16  // ...
   192  	SectorSize        uint16      // Word 106, Logical/physical sector size.
   193  	_                 [1]uint16   // ...
   194  	WWN               [4]uint16   // Word 108..111, WWN (World Wide Name).
   195  	_                 [105]uint16 // ...
   196  	RotationRate      uint16      // Word 217, nominal media rotation rate.
   197  	_                 [4]uint16   // ...
   198  	AtaTransportMajor uint16      // Word 222, Transport major version number.
   199  	_                 [33]uint16  // ...
   200  } // 512 bytes
   201  
   202  // SCSIDev represents a particular scsi device with device name
   203  // and file descriptor
   204  type SCSIDev struct {
   205  	DevName string // SCSI device name
   206  	fd      int    // File descriptor for the scsi device
   207  }
   208  
   209  // sg_io_hdr_t structure See http://sg.danny.cz/sg/p/sg_v3_ho.html
   210  type sgIOHeader struct {
   211  	interfaceID    int32   // 'S' for SCSI generic (required)
   212  	dxferDirection int32   // data transfer direction
   213  	cmdLen         uint8   // SCSI command length (<= 16 bytes)
   214  	mxSBLen        uint8   // max length to write to sbp
   215  	iovecCount     uint16  // 0 implies no scatter gather
   216  	dxferLen       uint32  // byte count of data transfer
   217  	dxferp         uintptr // points to data transfer memory or scatter gather list
   218  	cmdp           uintptr // points to command to perform
   219  	sbp            uintptr // points to sense_buffer memory
   220  	timeout        uint32  // MAX_UINT -> no timeout (unit: millisec)
   221  	flags          uint32  // 0 -> default, see SG_FLAG...
   222  	packID         int32   // unused internally (normally)
   223  	usrPtr         uintptr // unused internally
   224  	status         uint8   // SCSI status
   225  	maskedStatus   uint8   // shifted, masked scsi status
   226  	msgStatus      uint8   // messaging level data (optional)
   227  	SBLenwr        uint8   // byte count actually written to sbp
   228  	hostStatus     uint16  // errors from host adapter
   229  	driverStatus   uint16  // errors from software driver
   230  	resid          int32   // dxfer_len - actual_transferred
   231  	duration       uint32  // time taken by cmd (unit: millisec)
   232  	info           uint32  // auxiliary information
   233  }
   234  
   235  // sgIOErr is the format in which error could be returned while sending
   236  // a scsi generic ioctl command
   237  type sgIOErr struct {
   238  	scsiStatus   uint8
   239  	hostStatus   uint16
   240  	driverStatus uint16
   241  }
   242  
   243  // DiskAttr is struct being used for returning all the available disk details (both basic and smart)
   244  // For now, only basic disk attr are being fetched so it is returning only basic attrs
   245  type DiskAttr struct {
   246  	BasicDiskAttr
   247  	ATADiskAttr
   248  }
   249  
   250  // BasicDiskAttr is the structure being used for returning basic disk details
   251  type BasicDiskAttr struct {
   252  	Compliance       string
   253  	Vendor           string
   254  	ModelNumber      string
   255  	SerialNumber     string
   256  	FirmwareRevision string
   257  	WWN              string
   258  	Capacity         uint64
   259  	LBSize           uint32
   260  	PBSize           uint32
   261  	RotationRate     uint16
   262  }
   263  
   264  // SmartDiskAttr is the structure defined for smart disk attrs (Note : Not being used yet)
   265  type SmartDiskAttr struct {
   266  }
   267  
   268  // ATADiskAttr is the struct for disk attributes that are specific to ATA disks
   269  type ATADiskAttr struct {
   270  	ATAMajorVersion string
   271  	ATAMinorVersion string
   272  	AtaTransport    string
   273  }
   274  
   275  // InquiryResponse is used for parsing response fetched
   276  // by sending a scsi inquiry command to a scsi device
   277  // Here underscore (_) is used to skip the words which we don't want to
   278  // parse as of now..
   279  type InquiryResponse struct {
   280  	_            [2]byte  // ...
   281  	Version      byte     // implemented specification version such as SPC-1,SPC-2,etc
   282  	_            [5]byte  // ...
   283  	VendorID     [8]byte  // Vendor Identification
   284  	ProductID    [16]byte // Product Identification
   285  	ProductRev   [4]byte  // Product Revision Level
   286  	SerialNumber [20]byte // Serial Number
   287  }