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 "json.h"
9 #include "b64.h"
10 #include "../edk/Cper.h"
11 #include "../cper-utils.h"
12 #include "cper-section-pcie.h"
13 
14 //Converts a single PCIe CPER section into JSON IR.
15 json_object* cper_section_pcie_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
16 {
17     EFI_PCIE_ERROR_DATA* pcie_error = (EFI_PCIE_ERROR_DATA*)section;
18     json_object* section_ir = json_object_new_object();
19 
20     //Validation bits.
21     json_object* validation = bitfield_to_ir(pcie_error->ValidFields, 8, PCIE_ERROR_VALID_BITFIELD_NAMES);
22     json_object_object_add(section_ir, "validationBits", validation);
23 
24     //Port type.
25     json_object* port_type = integer_to_readable_pair(pcie_error->PortType, 9,
26         PCIE_ERROR_PORT_TYPES_KEYS,
27         PCIE_ERROR_PORT_TYPES_VALUES,
28         "Unknown");
29     json_object_object_add(section_ir, "portType", port_type);
30 
31     //Version, provided each half in BCD.
32     json_object* version = json_object_new_object();
33     json_object_object_add(version, "minor", json_object_new_int(bcd_to_int(pcie_error->Version & 0xFF)));
34     json_object_object_add(version, "major", json_object_new_int(bcd_to_int(pcie_error->Version >> 8)));
35     json_object_object_add(section_ir, "version", version);
36 
37     //Command & status.
38     json_object* command_status = json_object_new_object();
39     json_object_object_add(command_status, "commandRegister", json_object_new_uint64(pcie_error->CommandStatus & 0xFFFF));
40     json_object_object_add(command_status, "statusRegister", json_object_new_uint64(pcie_error->CommandStatus >> 16));
41     json_object_object_add(section_ir, "commandStatus", command_status);
42 
43     //PCIe Device ID.
44     json_object* device_id = json_object_new_object();
45     UINT64 class_id = (pcie_error->DevBridge.ClassCode[0] << 16) +
46                       (pcie_error->DevBridge.ClassCode[1] << 8) +
47                       pcie_error->DevBridge.ClassCode[2];
48     json_object_object_add(device_id, "vendorID", json_object_new_uint64(pcie_error->DevBridge.VendorId));
49     json_object_object_add(device_id, "deviceID", json_object_new_uint64(pcie_error->DevBridge.DeviceId));
50     json_object_object_add(device_id, "classCode", json_object_new_uint64(class_id));
51     json_object_object_add(device_id, "functionNumber", json_object_new_uint64(pcie_error->DevBridge.Function));
52     json_object_object_add(device_id, "deviceNumber", json_object_new_uint64(pcie_error->DevBridge.Device));
53     json_object_object_add(device_id, "segmentNumber", json_object_new_uint64(pcie_error->DevBridge.Segment));
54     json_object_object_add(device_id, "primaryOrDeviceBusNumber", json_object_new_uint64(pcie_error->DevBridge.PrimaryOrDeviceBus));
55     json_object_object_add(device_id, "secondaryBusNumber", json_object_new_uint64(pcie_error->DevBridge.SecondaryBus));
56     json_object_object_add(device_id, "slotNumber", json_object_new_uint64(pcie_error->DevBridge.Slot.Number));
57     json_object_object_add(section_ir, "deviceID", device_id);
58 
59     //Device serial number.
60     json_object_object_add(section_ir, "deviceSerialNumber", json_object_new_uint64(pcie_error->SerialNo));
61 
62     //Bridge control status.
63     json_object* bridge_control_status = json_object_new_object();
64     json_object_object_add(bridge_control_status, "secondaryStatusRegister",
65         json_object_new_uint64(pcie_error->BridgeControlStatus & 0xFFFF));
66     json_object_object_add(bridge_control_status, "controlRegister",
67         json_object_new_uint64(pcie_error->BridgeControlStatus >> 16));
68     json_object_object_add(section_ir, "bridgeControlStatus", bridge_control_status);
69 
70     //Capability structure.
71     //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure
72     //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem
73     //to be a way to differentiate these, so this is left as a b64 dump.
74     char* encoded = b64_encode((unsigned char*)pcie_error->Capability.PcieCap, 60);
75     json_object* capability = json_object_new_object();
76     json_object_object_add(capability, "data", json_object_new_string(encoded));
77     free(encoded);
78     json_object_object_add(section_ir, "capabilityStructure", capability);
79 
80     //AER information.
81     json_object* aer_capability_ir = json_object_new_object();
82     EFI_PCIE_ADV_ERROR_EXT_CAPABILITY* aer_capability = (EFI_PCIE_ADV_ERROR_EXT_CAPABILITY*)pcie_error->AerInfo.PcieAer;
83     json_object_object_add(aer_capability_ir, "capabilityID",
84         json_object_new_uint64(aer_capability->Header.PcieExtendedCapabilityId));
85     json_object_object_add(aer_capability_ir, "capabilityVersion",
86         json_object_new_uint64(aer_capability->Header.CapabilityVersion));
87     json_object_object_add(aer_capability_ir, "uncorrectableErrorStatusRegister",
88         json_object_new_uint64(aer_capability->UncorrectableErrorStatusReg));
89     json_object_object_add(aer_capability_ir, "uncorrectableErrorMaskRegister",
90         json_object_new_uint64(aer_capability->UncorrectableErrorMaskReg));
91     json_object_object_add(aer_capability_ir, "uncorrectableErrorSeverityRegister",
92         json_object_new_uint64(aer_capability->UncorrectableErrorSeverityReg));
93     json_object_object_add(aer_capability_ir, "correctableErrorStatusRegister",
94         json_object_new_uint64(aer_capability->CorrectableErrorStatusReg));
95     json_object_object_add(aer_capability_ir, "correctableErrorMaskRegister",
96         json_object_new_uint64(aer_capability->CorrectableErrorMaskReg));
97     json_object_object_add(aer_capability_ir, "aeccReg", json_object_new_uint64(aer_capability->AeccReg));
98 
99     //Header log register (b64).
100     encoded = b64_encode((unsigned char*)aer_capability->HeaderLogReg, 16);
101     json_object_object_add(aer_capability_ir, "headerLogRegister", json_object_new_string(encoded));
102     free(encoded);
103 
104     //Remaining AER fields.
105     json_object_object_add(aer_capability_ir, "rootErrorCommand",
106         json_object_new_uint64(aer_capability->RootErrorCommand));
107     json_object_object_add(aer_capability_ir, "rootErrorStatus",
108         json_object_new_uint64(aer_capability->RootErrorStatus));
109     json_object_object_add(aer_capability_ir, "errorSourceIDRegister",
110         json_object_new_uint64(aer_capability->ErrorSourceIdReg));
111     json_object_object_add(aer_capability_ir, "correctableErrorSourceIDRegister",
112         json_object_new_uint64(aer_capability->CorrectableSourceIdReg));
113 
114     json_object_object_add(section_ir, "aerInfo", aer_capability_ir);
115     return section_ir;
116 }