xref: /openbmc/libcper/sections/cper-section-pcie.c (revision 50b966f7afa31fe39fda70e10eb9139cce39e025)
1 /**
2  * Describes functions for converting PCIe CPER sections from binary and JSON format
3  * into an intermediate format.
4  *
5  * Author: Lawrence.Tang@arm.com
6  **/
7 #include <stdio.h>
8 #include <string.h>
9 #include <json.h>
10 #include <libcper/base64.h>
11 #include <libcper/Cper.h>
12 #include <libcper/cper-utils.h>
13 #include <libcper/sections/cper-section-pcie.h>
14 #include <libcper/log.h>
15 
16 struct 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 
27 //Converts a single PCIe CPER section into JSON IR.
cper_section_pcie_to_ir(const UINT8 * section,UINT32 size)28 json_object *cper_section_pcie_to_ir(const UINT8 *section, UINT32 size)
29 {
30 	if (size < sizeof(EFI_PCIE_ERROR_DATA)) {
31 		return NULL;
32 	}
33 
34 	EFI_PCIE_ERROR_DATA *pcie_error = (EFI_PCIE_ERROR_DATA *)section;
35 	json_object *section_ir = json_object_new_object();
36 
37 	//Validation bits.
38 	ValidationTypes ui64Type = { UINT_64T,
39 				     .value.ui64 = pcie_error->ValidFields };
40 
41 	//Port type.
42 	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 	}
48 
49 	//Version, provided each half in BCD.
50 	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 	}
60 
61 	//Command & status.
62 	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 	}
75 
76 	//PCIe Device ID.
77 	char hexstring_buf[EFI_UINT64_HEX_STRING_LEN];
78 	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));
89 
90 		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 	}
120 
121 	//Device serial number.
122 	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 	}
127 
128 	//Bridge control status.
129 	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 	}
142 
143 	//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.
147 	int32_t encoded_len = 0;
148 	char *encoded = NULL;
149 	if (isvalid_prop_to_ir(&ui64Type, 6)) {
150 		char *encoded =
151 			base64_encode((UINT8 *)pcie_error->Capability.PcieCap,
152 				      60, &encoded_len);
153 		if (encoded == NULL) {
154 			cper_print_log(
155 				"Failed to allocate encode output buffer. \n");
156 		} 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);
162 
163 			json_object_object_add(
164 				section_ir, "capabilityStructure", capability);
165 		}
166 	}
167 
168 	//AER information.
169 	encoded_len = 0;
170 	encoded = NULL;
171 	if (isvalid_prop_to_ir(&ui64Type, 7)) {
172 		json_object *aer_capability_ir = json_object_new_object();
173 
174 		encoded = base64_encode((UINT8 *)pcie_error->AerInfo.PcieAer,
175 					96, &encoded_len);
176 		if (encoded == NULL) {
177 			cper_print_log(
178 				"Failed to allocate encode output buffer. \n");
179 		} 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 
218 		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));
224 
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);
247 	}
248 
249 	return section_ir;
250 }
251 
252 //Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream.
ir_section_pcie_to_cper(json_object * section,FILE * out)253 void ir_section_pcie_to_cper(json_object *section, FILE *out)
254 {
255 	EFI_PCIE_ERROR_DATA *section_cper =
256 		(EFI_PCIE_ERROR_DATA *)calloc(1, sizeof(EFI_PCIE_ERROR_DATA));
257 
258 	//Validation bits.
259 	ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
260 	struct json_object *obj = NULL;
261 
262 	//Version.
263 	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);
271 	}
272 
273 	//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) {
347 			cper_print_log(
348 				"Failed to allocate decode output buffer. \n");
349 		} 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;
359 	//AER capability structure.
360 	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;
364 
365 		decoded = base64_decode(json_object_get_string(encoded),
366 					json_object_get_string_len(encoded),
367 					&decoded_len);
368 
369 		if (decoded == NULL) {
370 			cper_print_log(
371 				"Failed to allocate decode output buffer. \n");
372 		} else {
373 			memcpy(section_cper->AerInfo.PcieAer, decoded,
374 			       decoded_len);
375 			free(decoded);
376 		}
377 		add_to_valid_bitfield(&ui64Type, 7);
378 	}
379 
380 	//Miscellaneous value fields.
381 	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;
391 
392 	//Write out to stream, free resources.
393 	fwrite(section_cper, sizeof(EFI_PCIE_ERROR_DATA), 1, out);
394 	fflush(out);
395 	free(section_cper);
396 }
397