blob: ee8a1d9808bdabbd4ea1ffdb87f70f9fa7d0edac [file] [log] [blame]
Lawrence Tang4dbe3d72022-07-06 13:51:01 +01001/**
2 * Describes functions for converting PCIe CPER sections from binary and JSON format
3 * into an intermediate format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang4dbe3d72022-07-06 13:51:01 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang3b7f45b2022-07-14 14:14:30 +01008#include <string.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000010#include <libcper/base64.h>
11#include <libcper/Cper.h>
12#include <libcper/cper-utils.h>
13#include <libcper/sections/cper-section-pcie.h>
Ed Tanous50b966f2025-03-11 09:06:19 -070014#include <libcper/log.h>
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010015
Andrew Adriance3cebfc22024-11-20 12:57:04 -080016struct aer_info_registers {
17 UINT32 pcie_capability_header;
18 UINT32 uncorrectable_error_status;
19 UINT32 uncorrectable_error_mask;
20 UINT32 uncorrectable_error_severity;
21 UINT32 correctable_error_status;
22 UINT32 correctable_error_mask;
23 UINT32 aer_capabilites_control;
24 UINT32 tlp_header_log[4];
25};
26
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010027//Converts a single PCIe CPER section into JSON IR.
Ed Tanous12dbd4f2025-03-08 19:05:01 -080028json_object *cper_section_pcie_to_ir(const UINT8 *section, UINT32 size)
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010029{
Ed Tanous12dbd4f2025-03-08 19:05:01 -080030 if (size < sizeof(EFI_PCIE_ERROR_DATA)) {
31 return NULL;
32 }
33
Lawrence Tange407b4c2022-07-21 13:54:01 +010034 EFI_PCIE_ERROR_DATA *pcie_error = (EFI_PCIE_ERROR_DATA *)section;
35 json_object *section_ir = json_object_new_object();
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010036
Lawrence Tange407b4c2022-07-21 13:54:01 +010037 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080038 ValidationTypes ui64Type = { UINT_64T,
39 .value.ui64 = pcie_error->ValidFields };
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010040
Lawrence Tange407b4c2022-07-21 13:54:01 +010041 //Port type.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080042 if (isvalid_prop_to_ir(&ui64Type, 0)) {
43 json_object *port_type = integer_to_readable_pair(
44 pcie_error->PortType, 9, PCIE_ERROR_PORT_TYPES_KEYS,
45 PCIE_ERROR_PORT_TYPES_VALUES, "Unknown");
46 json_object_object_add(section_ir, "portType", port_type);
47 }
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010048
Lawrence Tange407b4c2022-07-21 13:54:01 +010049 //Version, provided each half in BCD.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080050 if (isvalid_prop_to_ir(&ui64Type, 1)) {
51 json_object *version = json_object_new_object();
52 json_object_object_add(version, "minor",
53 json_object_new_int(bcd_to_int(
54 pcie_error->Version & 0xFF)));
55 json_object_object_add(version, "major",
56 json_object_new_int(bcd_to_int(
57 pcie_error->Version >> 8)));
58 json_object_object_add(section_ir, "version", version);
59 }
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010060
Lawrence Tange407b4c2022-07-21 13:54:01 +010061 //Command & status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080062 if (isvalid_prop_to_ir(&ui64Type, 2)) {
63 json_object *command_status = json_object_new_object();
64 json_object_object_add(
65 command_status, "commandRegister",
66 json_object_new_uint64(pcie_error->CommandStatus &
67 0xFFFF));
68 json_object_object_add(
69 command_status, "statusRegister",
70 json_object_new_uint64(pcie_error->CommandStatus >>
71 16));
72 json_object_object_add(section_ir, "commandStatus",
73 command_status);
74 }
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010075
Lawrence Tange407b4c2022-07-21 13:54:01 +010076 //PCIe Device ID.
Aushim Nagarkatticc367012024-12-05 18:17:27 -080077 char hexstring_buf[EFI_UINT64_HEX_STRING_LEN];
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080078 if (isvalid_prop_to_ir(&ui64Type, 3)) {
79 json_object *device_id = json_object_new_object();
80 UINT64 class_id = (pcie_error->DevBridge.ClassCode[0] << 16) +
81 (pcie_error->DevBridge.ClassCode[1] << 8) +
82 pcie_error->DevBridge.ClassCode[2];
83 json_object_object_add(
84 device_id, "vendorID",
85 json_object_new_uint64(pcie_error->DevBridge.VendorId));
86 json_object_object_add(
87 device_id, "deviceID",
88 json_object_new_uint64(pcie_error->DevBridge.DeviceId));
Aushim Nagarkatticc367012024-12-05 18:17:27 -080089
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080090 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%0X",
91 pcie_error->DevBridge.DeviceId);
92 json_object_object_add(device_id, "deviceIDHex",
93 json_object_new_string(hexstring_buf));
94
95 json_object_object_add(device_id, "classCode",
96 json_object_new_uint64(class_id));
97 json_object_object_add(
98 device_id, "functionNumber",
99 json_object_new_uint64(pcie_error->DevBridge.Function));
100 json_object_object_add(
101 device_id, "deviceNumber",
102 json_object_new_uint64(pcie_error->DevBridge.Device));
103 json_object_object_add(
104 device_id, "segmentNumber",
105 json_object_new_uint64(pcie_error->DevBridge.Segment));
106 json_object_object_add(
107 device_id, "primaryOrDeviceBusNumber",
108 json_object_new_uint64(
109 pcie_error->DevBridge.PrimaryOrDeviceBus));
110 json_object_object_add(
111 device_id, "secondaryBusNumber",
112 json_object_new_uint64(
113 pcie_error->DevBridge.SecondaryBus));
114 json_object_object_add(
115 device_id, "slotNumber",
116 json_object_new_uint64(
117 pcie_error->DevBridge.Slot.Number));
118 json_object_object_add(section_ir, "deviceID", device_id);
119 }
Lawrence Tang4dbe3d72022-07-06 13:51:01 +0100120
Lawrence Tange407b4c2022-07-21 13:54:01 +0100121 //Device serial number.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800122 if (isvalid_prop_to_ir(&ui64Type, 4)) {
123 json_object_object_add(
124 section_ir, "deviceSerialNumber",
125 json_object_new_uint64(pcie_error->SerialNo));
126 }
Lawrence Tang4dbe3d72022-07-06 13:51:01 +0100127
Lawrence Tange407b4c2022-07-21 13:54:01 +0100128 //Bridge control status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800129 if (isvalid_prop_to_ir(&ui64Type, 5)) {
130 json_object *bridge_control_status = json_object_new_object();
131 json_object_object_add(
132 bridge_control_status, "secondaryStatusRegister",
133 json_object_new_uint64(pcie_error->BridgeControlStatus &
134 0xFFFF));
135 json_object_object_add(
136 bridge_control_status, "controlRegister",
137 json_object_new_uint64(
138 pcie_error->BridgeControlStatus >> 16));
139 json_object_object_add(section_ir, "bridgeControlStatus",
140 bridge_control_status);
141 }
Lawrence Tang4dbe3d72022-07-06 13:51:01 +0100142
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143 //Capability structure.
144 //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure
145 //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem
146 //to be a way to differentiate these, so this is left as a b64 dump.
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700147 int32_t encoded_len = 0;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800148 char *encoded = NULL;
149 if (isvalid_prop_to_ir(&ui64Type, 6)) {
150 char *encoded =
151 base64_encode((UINT8 *)pcie_error->Capability.PcieCap,
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700152 60, &encoded_len);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800153 if (encoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700154 cper_print_log(
155 "Failed to allocate encode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800156 } else {
157 json_object *capability = json_object_new_object();
158 json_object_object_add(capability, "data",
159 json_object_new_string_len(
160 encoded, encoded_len));
161 free(encoded);
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700162
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800163 json_object_object_add(
164 section_ir, "capabilityStructure", capability);
165 }
John Chungf8fc7052024-05-03 20:05:29 +0800166 }
Lawrence Tang4dbe3d72022-07-06 13:51:01 +0100167
Lawrence Tange407b4c2022-07-21 13:54:01 +0100168 //AER information.
John Chungf8fc7052024-05-03 20:05:29 +0800169 encoded_len = 0;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800170 encoded = NULL;
171 if (isvalid_prop_to_ir(&ui64Type, 7)) {
172 json_object *aer_capability_ir = json_object_new_object();
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700173
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800174 encoded = base64_encode((UINT8 *)pcie_error->AerInfo.PcieAer,
175 96, &encoded_len);
176 if (encoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700177 cper_print_log(
178 "Failed to allocate encode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800179 } else {
180 json_object_object_add(aer_capability_ir, "data",
181 json_object_new_string_len(
182 encoded, encoded_len));
183 free(encoded);
184 }
185
186 struct aer_info_registers *aer_decode;
187 aer_decode = (struct aer_info_registers *)&pcie_error->AerInfo
188 .PcieAer;
189 json_object_object_add(
190 aer_capability_ir, "capability_header",
191 json_object_new_uint64(
192 aer_decode->pcie_capability_header));
193 json_object_object_add(
194 aer_capability_ir, "uncorrectable_error_status",
195 json_object_new_uint64(
196 aer_decode->uncorrectable_error_status));
197
198 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN,
199 "0x%08" PRIX32,
200 aer_decode->uncorrectable_error_status);
201 json_object_object_add(aer_capability_ir,
202 "uncorrectable_error_status_hex",
203 json_object_new_string(hexstring_buf));
204
205 json_object_object_add(
206 aer_capability_ir, "uncorrectable_error_mask",
207 json_object_new_uint64(
208 aer_decode->uncorrectable_error_mask));
209 json_object_object_add(
210 aer_capability_ir, "uncorrectable_error_severity",
211 json_object_new_uint64(
212 aer_decode->uncorrectable_error_severity));
213 json_object_object_add(
214 aer_capability_ir, "correctable_error_status",
215 json_object_new_uint64(
216 aer_decode->correctable_error_status));
217
Ed Tanous5e2164a2025-03-09 09:20:44 -0700218 int len = snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN,
219 "0x%08" PRIX32,
220 aer_decode->correctable_error_status);
221 json_object_object_add(
222 aer_capability_ir, "correctable_error_status_hex",
223 json_object_new_string_len(hexstring_buf, len));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800224
225 json_object_object_add(
226 aer_capability_ir, "correctable_error_mask",
227 json_object_new_uint64(
228 aer_decode->correctable_error_mask));
229 json_object_object_add(
230 aer_capability_ir, "capabilites_control",
231 json_object_new_uint64(
232 aer_decode->aer_capabilites_control));
233 json_object_object_add(
234 aer_capability_ir, "tlp_header_0",
235 json_object_new_uint64(aer_decode->tlp_header_log[0]));
236 json_object_object_add(
237 aer_capability_ir, "tlp_header_1",
238 json_object_new_uint64(aer_decode->tlp_header_log[1]));
239 json_object_object_add(
240 aer_capability_ir, "tlp_header_2",
241 json_object_new_uint64(aer_decode->tlp_header_log[2]));
242 json_object_object_add(
243 aer_capability_ir, "tlp_header_3",
244 json_object_new_uint64(aer_decode->tlp_header_log[3]));
245 json_object_object_add(section_ir, "aerInfo",
246 aer_capability_ir);
John Chungf8fc7052024-05-03 20:05:29 +0800247 }
Andrew Adriance3cebfc22024-11-20 12:57:04 -0800248
Lawrence Tange407b4c2022-07-21 13:54:01 +0100249 return section_ir;
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100250}
251
252//Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100253void ir_section_pcie_to_cper(json_object *section, FILE *out)
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100254{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100255 EFI_PCIE_ERROR_DATA *section_cper =
256 (EFI_PCIE_ERROR_DATA *)calloc(1, sizeof(EFI_PCIE_ERROR_DATA));
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100257
Lawrence Tange407b4c2022-07-21 13:54:01 +0100258 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800259 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
260 struct json_object *obj = NULL;
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100261
Lawrence Tange407b4c2022-07-21 13:54:01 +0100262 //Version.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800263 if (json_object_object_get_ex(section, "version", &obj)) {
264 json_object *version = obj;
265 UINT32 minor = int_to_bcd(json_object_get_int(
266 json_object_object_get(version, "minor")));
267 UINT32 major = int_to_bcd(json_object_get_int(
268 json_object_object_get(version, "major")));
269 section_cper->Version = minor + (major << 8);
270 add_to_valid_bitfield(&ui64Type, 1);
John Chungf8fc7052024-05-03 20:05:29 +0800271 }
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100272
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800273 //Command/status registers.
274 if (json_object_object_get_ex(section, "commandStatus", &obj)) {
275 json_object *command_status = obj;
276 UINT32 command = (UINT16)json_object_get_uint64(
277 json_object_object_get(command_status,
278 "commandRegister"));
279 UINT32 status = (UINT16)json_object_get_uint64(
280 json_object_object_get(command_status,
281 "statusRegister"));
282 section_cper->CommandStatus = command + (status << 16);
283 add_to_valid_bitfield(&ui64Type, 2);
284 }
285
286 //Device ID.
287 if (json_object_object_get_ex(section, "deviceID", &obj)) {
288 json_object *device_id = obj;
289 UINT64 class_id = json_object_get_uint64(
290 json_object_object_get(device_id, "classCode"));
291 section_cper->DevBridge.VendorId =
292 (UINT16)json_object_get_uint64(
293 json_object_object_get(device_id, "vendorID"));
294 section_cper->DevBridge.DeviceId =
295 (UINT16)json_object_get_uint64(
296 json_object_object_get(device_id, "deviceID"));
297 section_cper->DevBridge.ClassCode[0] = class_id >> 16;
298 section_cper->DevBridge.ClassCode[1] = (class_id >> 8) & 0xFF;
299 section_cper->DevBridge.ClassCode[2] = class_id & 0xFF;
300 section_cper->DevBridge.Function =
301 (UINT8)json_object_get_uint64(json_object_object_get(
302 device_id, "functionNumber"));
303 section_cper->DevBridge.Device = (UINT8)json_object_get_uint64(
304 json_object_object_get(device_id, "deviceNumber"));
305 section_cper->DevBridge.Segment =
306 (UINT16)json_object_get_uint64(json_object_object_get(
307 device_id, "segmentNumber"));
308 section_cper->DevBridge.PrimaryOrDeviceBus =
309 (UINT8)json_object_get_uint64(json_object_object_get(
310 device_id, "primaryOrDeviceBusNumber"));
311 section_cper->DevBridge.SecondaryBus =
312 (UINT8)json_object_get_uint64(json_object_object_get(
313 device_id, "secondaryBusNumber"));
314 section_cper->DevBridge.Slot.Number =
315 (UINT16)json_object_get_uint64(json_object_object_get(
316 device_id, "slotNumber"));
317 add_to_valid_bitfield(&ui64Type, 3);
318 }
319
320 //Bridge/control status.
321 if (json_object_object_get_ex(section, "bridgeControlStatus", &obj)) {
322 json_object *bridge_control = obj;
323 UINT32 bridge_status = (UINT16)json_object_get_uint64(
324 json_object_object_get(bridge_control,
325 "secondaryStatusRegister"));
326 UINT32 control_status = (UINT16)json_object_get_uint64(
327 json_object_object_get(bridge_control,
328 "controlRegister"));
329 section_cper->BridgeControlStatus =
330 bridge_status + (control_status << 16);
331 add_to_valid_bitfield(&ui64Type, 5);
332 }
333
334 //Capability structure.
335 int32_t decoded_len = 0;
336 UINT8 *decoded = NULL;
337 json_object *encoded = NULL;
338 if (json_object_object_get_ex(section, "capabilityStructure", &obj)) {
339 json_object *capability = obj;
340 json_object *encoded =
341 json_object_object_get(capability, "data");
342
343 UINT8 *decoded = base64_decode(
344 json_object_get_string(encoded),
345 json_object_get_string_len(encoded), &decoded_len);
346 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700347 cper_print_log(
348 "Failed to allocate decode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800349 } else {
350 memcpy(section_cper->Capability.PcieCap, decoded,
351 decoded_len);
352 free(decoded);
353 }
354 add_to_valid_bitfield(&ui64Type, 6);
355 }
356
357 decoded = NULL;
358 encoded = NULL;
Lawrence Tange407b4c2022-07-21 13:54:01 +0100359 //AER capability structure.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800360 if (json_object_object_get_ex(section, "aerInfo", &obj)) {
361 json_object *aer_info = obj;
362 encoded = json_object_object_get(aer_info, "data");
363 decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700364
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800365 decoded = base64_decode(json_object_get_string(encoded),
366 json_object_get_string_len(encoded),
367 &decoded_len);
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700368
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800369 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700370 cper_print_log(
371 "Failed to allocate decode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800372 } else {
373 memcpy(section_cper->AerInfo.PcieAer, decoded,
374 decoded_len);
375 free(decoded);
376 }
377 add_to_valid_bitfield(&ui64Type, 7);
John Chungf8fc7052024-05-03 20:05:29 +0800378 }
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100379
Lawrence Tange407b4c2022-07-21 13:54:01 +0100380 //Miscellaneous value fields.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800381 if (json_object_object_get_ex(section, "portType", &obj)) {
382 section_cper->PortType = (UINT32)readable_pair_to_integer(obj);
383 add_to_valid_bitfield(&ui64Type, 0);
384 }
385 if (json_object_object_get_ex(section, "deviceSerialNumber", &obj)) {
386 section_cper->SerialNo = json_object_get_uint64(obj);
387 add_to_valid_bitfield(&ui64Type, 4);
388 }
389
390 section_cper->ValidFields = ui64Type.value.ui64;
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100391
Lawrence Tange407b4c2022-07-21 13:54:01 +0100392 //Write out to stream, free resources.
393 fwrite(section_cper, sizeof(EFI_PCIE_ERROR_DATA), 1, out);
394 fflush(out);
395 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800396}