1 /*
2  * Copyright (c)  2018-present Facebook. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 #include "sdrutils.hpp"
19 
20 #define SEL_JSON_DATA_FILE "/var/log/fbSelRaw.json"
21 #define KEY_SEL_COUNT "SelCount"
22 #define KEY_SEL_ENTRY_RAW "SelEntry"
23 #define KEY_ADD_TIME "AddTimeStamp"
24 #define KEY_ERASE_TIME "EraseTimeStamp"
25 #define KEY_FREE_SPACE "FreeSpace"
26 #define KEY_OPER_SUPP "OperationalSupport"
27 #define KEY_SEL_VER "SELVersion"
28 #define BIT(value, index) ((value >> index) & 1)
29 
30 static constexpr uint8_t ipmiSdrVersion = 0x51;
31 
32 #pragma pack(push, 1)
33 
34 struct GetSDRReq
35 {
36     uint16_t reservationID;
37     uint16_t recordID;
38     uint8_t offset;
39     uint8_t bytesToRead;
40 };
41 
42 struct GetFRUAreaResp
43 {
44     uint8_t inventorySizeLSB;
45     uint8_t inventorySizeMSB;
46     uint8_t accessType;
47 };
48 
49 struct GetFRUAreaReq
50 {
51     uint8_t fruDeviceID;
52     uint16_t fruInventoryOffset;
53     uint8_t countToRead;
54 };
55 
56 struct WriteFRUDataReq
57 {
58     uint8_t fruDeviceID;
59     uint16_t fruInventoryOffset;
60     uint8_t data[];
61 };
62 
63 #pragma pack(pop)
64 
65 enum class GetFRUAreaAccessType : uint8_t
66 {
67     byte = 0x0,
68     words = 0x1
69 };
70 
71 enum class IPMINetfnStorageCmds : ipmi_cmd_t
72 {
73     ipmiCmdGetFRUInvAreaInfo = 0x10,
74     ipmiCmdReadFRUData = 0x11,
75     ipmiCmdWriteFRUData = 0x12,
76     ipmiCmdGetRepositoryInfo = 0x20,
77     ipmiCmdGetSDRAllocationInfo = 0x21,
78     ipmiCmdReserveSDR = 0x22,
79     ipmiCmdGetSDR = 0x23,
80     ipmiCmdGetSELInfo = 0x40,
81     ipmiCmdReserveSEL = 0x42,
82     ipmiCmdGetSELEntry = 0x43,
83     ipmiCmdAddSEL = 0x44,
84     ipmiCmdDeleteSEL = 0x46,
85     ipmiCmdClearSEL = 0x47,
86     ipmiCmdGetSELTime = 0x48,
87     ipmiCmdSetSELTime = 0x49,
88 };
89 
90 #pragma pack(push, 1)
91 struct FRUHeader
92 {
93     uint8_t commonHeaderFormat;
94     uint8_t internalOffset;
95     uint8_t chassisOffset;
96     uint8_t boardOffset;
97     uint8_t productOffset;
98     uint8_t multiRecordOffset;
99     uint8_t pad;
100     uint8_t checksum;
101 };
102 #pragma pack(pop)
103 
104 namespace fb_oem::ipmi::sel
105 {
106 
107 static constexpr auto selVersion = 0x51;
108 static constexpr auto invalidTimeStamp = 0xFFFFFFFF;
109 /* Spec indicates that more than 64kB is free */
110 static constexpr auto freeSpace = 0xFFFF;
111 static constexpr uint8_t selOperationSupport = 0x02;
112 
113 static constexpr auto firstEntry = 0x0000;
114 static constexpr auto lastEntry = 0xFFFF;
115 static constexpr auto entireRecord = 0xFF;
116 static constexpr auto selRecordSize = 16;
117 
118 static constexpr auto stdErr = "Standard";
119 static constexpr auto oemTSErr = "OEM timestamped";
120 static constexpr auto fbUniSELErr = "Facebook Unified SEL";
121 static constexpr auto oemNTSErr = "OEM non-timestamped";
122 static constexpr auto unknownErr = "Unknown";
123 
124 static constexpr uint8_t stdErrType = 0x02;
125 static constexpr uint8_t oemTSErrTypeMin = 0xC0;
126 static constexpr uint8_t oemTSErrTypeMax = 0xDF;
127 static constexpr uint8_t oemNTSErrTypeMin = 0xE0;
128 static constexpr uint8_t fbUniErrType = 0xFB;
129 static constexpr uint8_t oemNTSErrTypeMax = 0xFF;
130 
131 static constexpr uint8_t unifiedPcieErr = 0;
132 static constexpr uint8_t unifiedMemErr = 1;
133 static constexpr uint8_t unifiedIioErr = 3;
134 static constexpr uint8_t unifiedPostEvt = 8;
135 static constexpr uint8_t unifiedPcieEvt = 9;
136 static constexpr uint8_t unifiedMemEvt = 10;
137 static constexpr uint8_t unifiedBootGuard = 12;
138 static constexpr uint8_t unifiedPprEvt = 13;
139 
140 /* event sensor name in processing SEL */
141 static constexpr uint8_t memoryEccError = 0x63;
142 static constexpr uint8_t memoryErrLogDIS = 0x87;
143 
144 /** @struct GetSELInfoData
145  *
146  * IPMI response payload data for Get SEL Info request
147  */
148 struct GetSELInfoData
149 {
150     uint8_t selVersion;
151     uint16_t entries;
152     uint16_t freeSpace;
153     uint32_t addTimeStamp;
154     uint32_t eraseTimeStamp;
155     uint8_t operationSupport;
156 };
157 
158 /** @struct GetSELEntryRequest
159  *
160  *  IPMI payload for Get SEL Entry command request.
161  */
162 struct GetSELEntryRequest
163 {
164     uint16_t reservID; //!< Reservation ID.
165     uint16_t recordID; //!< SEL Record ID.
166     uint8_t offset;    //!< Offset into record.
167     uint8_t readLen;   //!< Bytes to read.
168 } __attribute__((packed));
169 
170 /** @struct GetSELEntryResponse
171  *
172  *  IPMI payload for Get SEL Entry command response.
173  */
174 struct GetSELEntryResponse
175 {
176     uint16_t nextRecordID;  //!< Next RecordID.
177     uint8_t recordData[16]; //!< Record Data.
178 } __attribute__((packed));
179 
180 /** @struct AddSELEntryRequest
181  *
182  *  IPMI payload for ADD SEL Entry command request.
183  */
184 struct StdSELEntry
185 {
186     uint16_t recordID;        //!< Record ID.
187     uint8_t recordType;       //!< Record Type.
188     uint32_t timeStamp;       //!< Timestamp.
189     uint16_t generatorID;     //!< Generator ID.
190     uint8_t eventMsgRevision; //!< Event Message Revision.
191     uint8_t sensorType;       //!< Sensor Type.
192     uint8_t sensorNum;        //!< Sensor Number.
193     uint8_t eventType;        //!< Event Dir | Event Type.
194     uint8_t eventData1;       //!< Event Data 1.
195     uint8_t eventData2;       //!< Event Data 2.
196     uint8_t eventData3;       //!< Event Data 3.
197 } __attribute__((packed));
198 
199 struct TsOemSELEntry
200 {
201     uint16_t recordID;  //!< Record ID.
202     uint8_t recordType; //!< Record Type.
203     uint32_t timeStamp; //!< Timestamp.
204     uint8_t mfrId[3];   //!< Manufacturer Id.
205     uint8_t oemData[6]; //!< OEM Data.
206 } __attribute__((packed));
207 
208 struct NtsOemSELEntry
209 {
210     uint16_t recordID;   //!< Record ID.
211     uint8_t recordType;  //!< Record Type.
212     uint8_t oemData[13]; //!< OEM Data.
213 } __attribute__((packed));
214 
215 static constexpr auto initiateErase = 0xAA;
216 static constexpr auto getEraseStatus = 0x00;
217 static constexpr auto eraseComplete = 0x01;
218 
219 } // namespace fb_oem::ipmi::sel
220