blob: 8c70734b6139a63f371f672f0d7e495d482c48f0 [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.
4 *
5 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang3b7f45b2022-07-14 14:14:30 +01008#include <string.h>
Lawrence Tang4dbe3d72022-07-06 13:51:01 +01009#include "json.h"
Lawrence Tang6c461e92022-07-07 15:25:49 +010010#include "b64.h"
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010011#include "../edk/Cper.h"
12#include "../cper-utils.h"
13#include "cper-section-pcie.h"
14
15//Converts a single PCIe CPER section into JSON IR.
16json_object* cper_section_pcie_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
17{
18 EFI_PCIE_ERROR_DATA* pcie_error = (EFI_PCIE_ERROR_DATA*)section;
19 json_object* section_ir = json_object_new_object();
20
21 //Validation bits.
22 json_object* validation = bitfield_to_ir(pcie_error->ValidFields, 8, PCIE_ERROR_VALID_BITFIELD_NAMES);
23 json_object_object_add(section_ir, "validationBits", validation);
24
25 //Port type.
26 json_object* port_type = integer_to_readable_pair(pcie_error->PortType, 9,
27 PCIE_ERROR_PORT_TYPES_KEYS,
28 PCIE_ERROR_PORT_TYPES_VALUES,
29 "Unknown");
30 json_object_object_add(section_ir, "portType", port_type);
31
32 //Version, provided each half in BCD.
33 json_object* version = json_object_new_object();
34 json_object_object_add(version, "minor", json_object_new_int(bcd_to_int(pcie_error->Version & 0xFF)));
35 json_object_object_add(version, "major", json_object_new_int(bcd_to_int(pcie_error->Version >> 8)));
36 json_object_object_add(section_ir, "version", version);
37
38 //Command & status.
39 json_object* command_status = json_object_new_object();
40 json_object_object_add(command_status, "commandRegister", json_object_new_uint64(pcie_error->CommandStatus & 0xFFFF));
41 json_object_object_add(command_status, "statusRegister", json_object_new_uint64(pcie_error->CommandStatus >> 16));
42 json_object_object_add(section_ir, "commandStatus", command_status);
43
44 //PCIe Device ID.
45 json_object* device_id = json_object_new_object();
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010046 UINT64 class_id = (pcie_error->DevBridge.ClassCode[0] << 16) +
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010047 (pcie_error->DevBridge.ClassCode[1] << 8) +
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010048 pcie_error->DevBridge.ClassCode[2];
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010049 json_object_object_add(device_id, "vendorID", json_object_new_uint64(pcie_error->DevBridge.VendorId));
50 json_object_object_add(device_id, "deviceID", json_object_new_uint64(pcie_error->DevBridge.DeviceId));
51 json_object_object_add(device_id, "classCode", json_object_new_uint64(class_id));
52 json_object_object_add(device_id, "functionNumber", json_object_new_uint64(pcie_error->DevBridge.Function));
53 json_object_object_add(device_id, "deviceNumber", json_object_new_uint64(pcie_error->DevBridge.Device));
54 json_object_object_add(device_id, "segmentNumber", json_object_new_uint64(pcie_error->DevBridge.Segment));
55 json_object_object_add(device_id, "primaryOrDeviceBusNumber", json_object_new_uint64(pcie_error->DevBridge.PrimaryOrDeviceBus));
56 json_object_object_add(device_id, "secondaryBusNumber", json_object_new_uint64(pcie_error->DevBridge.SecondaryBus));
57 json_object_object_add(device_id, "slotNumber", json_object_new_uint64(pcie_error->DevBridge.Slot.Number));
58 json_object_object_add(section_ir, "deviceID", device_id);
59
60 //Device serial number.
61 json_object_object_add(section_ir, "deviceSerialNumber", json_object_new_uint64(pcie_error->SerialNo));
62
63 //Bridge control status.
64 json_object* bridge_control_status = json_object_new_object();
65 json_object_object_add(bridge_control_status, "secondaryStatusRegister",
66 json_object_new_uint64(pcie_error->BridgeControlStatus & 0xFFFF));
67 json_object_object_add(bridge_control_status, "controlRegister",
68 json_object_new_uint64(pcie_error->BridgeControlStatus >> 16));
69 json_object_object_add(section_ir, "bridgeControlStatus", bridge_control_status);
70
71 //Capability structure.
Lawrence Tang6c461e92022-07-07 15:25:49 +010072 //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure
73 //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem
74 //to be a way to differentiate these, so this is left as a b64 dump.
75 char* encoded = b64_encode((unsigned char*)pcie_error->Capability.PcieCap, 60);
76 json_object* capability = json_object_new_object();
77 json_object_object_add(capability, "data", json_object_new_string(encoded));
78 free(encoded);
79 json_object_object_add(section_ir, "capabilityStructure", capability);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010080
81 //AER information.
Lawrence Tang6c461e92022-07-07 15:25:49 +010082 json_object* aer_capability_ir = json_object_new_object();
83 EFI_PCIE_ADV_ERROR_EXT_CAPABILITY* aer_capability = (EFI_PCIE_ADV_ERROR_EXT_CAPABILITY*)pcie_error->AerInfo.PcieAer;
84 json_object_object_add(aer_capability_ir, "capabilityID",
85 json_object_new_uint64(aer_capability->Header.PcieExtendedCapabilityId));
86 json_object_object_add(aer_capability_ir, "capabilityVersion",
87 json_object_new_uint64(aer_capability->Header.CapabilityVersion));
88 json_object_object_add(aer_capability_ir, "uncorrectableErrorStatusRegister",
89 json_object_new_uint64(aer_capability->UncorrectableErrorStatusReg));
90 json_object_object_add(aer_capability_ir, "uncorrectableErrorMaskRegister",
91 json_object_new_uint64(aer_capability->UncorrectableErrorMaskReg));
92 json_object_object_add(aer_capability_ir, "uncorrectableErrorSeverityRegister",
93 json_object_new_uint64(aer_capability->UncorrectableErrorSeverityReg));
94 json_object_object_add(aer_capability_ir, "correctableErrorStatusRegister",
95 json_object_new_uint64(aer_capability->CorrectableErrorStatusReg));
96 json_object_object_add(aer_capability_ir, "correctableErrorMaskRegister",
97 json_object_new_uint64(aer_capability->CorrectableErrorMaskReg));
98 json_object_object_add(aer_capability_ir, "aeccReg", json_object_new_uint64(aer_capability->AeccReg));
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010099
Lawrence Tang6c461e92022-07-07 15:25:49 +0100100 //Header log register (b64).
101 encoded = b64_encode((unsigned char*)aer_capability->HeaderLogReg, 16);
102 json_object_object_add(aer_capability_ir, "headerLogRegister", json_object_new_string(encoded));
103 free(encoded);
104
105 //Remaining AER fields.
106 json_object_object_add(aer_capability_ir, "rootErrorCommand",
107 json_object_new_uint64(aer_capability->RootErrorCommand));
108 json_object_object_add(aer_capability_ir, "rootErrorStatus",
109 json_object_new_uint64(aer_capability->RootErrorStatus));
110 json_object_object_add(aer_capability_ir, "errorSourceIDRegister",
111 json_object_new_uint64(aer_capability->ErrorSourceIdReg));
112 json_object_object_add(aer_capability_ir, "correctableErrorSourceIDRegister",
113 json_object_new_uint64(aer_capability->CorrectableSourceIdReg));
114
115 json_object_object_add(section_ir, "aerInfo", aer_capability_ir);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +0100116 return section_ir;
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100117}
118
119//Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream.
120void ir_section_pcie_to_cper(json_object* section, FILE* out)
121{
122 EFI_PCIE_ERROR_DATA* section_cper = (EFI_PCIE_ERROR_DATA*)calloc(1, sizeof(EFI_PCIE_ERROR_DATA));
123
124 //Validation bits.
125 section_cper->ValidFields = ir_to_bitfield(json_object_object_get(section, "validationBits"),
126 8, PCIE_ERROR_VALID_BITFIELD_NAMES);
127
128 //Version.
129 json_object* version = json_object_object_get(section, "version");
130 int minor = json_object_get_int(json_object_object_get(version, "minor"));
131 int major = json_object_get_int(json_object_object_get(version, "major"));
132 section_cper->Version = int_to_bcd(minor) + ((UINT16)(int_to_bcd(major)) << 8);
133
134 //Command/status registers.
135 json_object* command_status = json_object_object_get(section, "commandStatus");
136 UINT32 command = (UINT16)json_object_get_uint64(json_object_object_get(command_status, "commandRegister"));
137 UINT32 status = (UINT16)json_object_get_uint64(json_object_object_get(command_status, "statusRegister"));
138 section_cper->CommandStatus = command + (status << 16);
139
140 //Device ID.
141 json_object* device_id = json_object_object_get(section, "deviceID");
142 UINT64 class_id = json_object_get_uint64(json_object_object_get(device_id, "classCode"));
143 section_cper->DevBridge.VendorId =
144 (UINT16)json_object_get_uint64(json_object_object_get(device_id, "vendorID"));
145 section_cper->DevBridge.DeviceId =
146 (UINT16)json_object_get_uint64(json_object_object_get(device_id, "deviceID"));
147 section_cper->DevBridge.ClassCode[0] = class_id >> 16;
148 section_cper->DevBridge.ClassCode[1] = (class_id >> 8) & 0xFF;
149 section_cper->DevBridge.ClassCode[1] = class_id & 0xFF;
150 section_cper->DevBridge.Function =
151 (UINT8)json_object_get_uint64(json_object_object_get(device_id, "functionNumber"));
152 section_cper->DevBridge.Device =
153 (UINT8)json_object_get_uint64(json_object_object_get(device_id, "deviceNumber"));
154 section_cper->DevBridge.Segment =
155 (UINT16)json_object_get_uint64(json_object_object_get(device_id, "segmentNumber"));
156 section_cper->DevBridge.PrimaryOrDeviceBus =
157 (UINT8)json_object_get_uint64(json_object_object_get(device_id, "primaryOrDeviceBusNumber"));
158 section_cper->DevBridge.SecondaryBus =
159 (UINT8)json_object_get_uint64(json_object_object_get(device_id, "secondaryBusNumber"));
160 section_cper->DevBridge.Slot.Number =
161 (UINT16)json_object_get_uint64(json_object_object_get(device_id, "slotNumber"));
162
163 //Bridge/control status.
164 json_object* bridge_control = json_object_object_get(section, "bridgeControlStatus");
165 UINT32 bridge_status = (UINT16)json_object_get_uint64(json_object_object_get(bridge_control, "secondaryStatusRegister"));
166 UINT32 control_status = (UINT16)json_object_get_uint64(json_object_object_get(bridge_control, "controlRegister"));
167 section_cper->BridgeControlStatus = bridge_status + (control_status << 16);
168
169 //Capability structure.
170 json_object* capability = json_object_object_get(section, "capabilityStructure");
171 json_object* encoded = json_object_object_get(capability, "data");
172 UINT8* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
173 memcpy(section_cper->Capability.PcieCap, decoded, 60);
174 free(decoded);
175
176 //AER capability structure.
177 json_object* aer_info = json_object_object_get(section, "aerInfo");
178 EFI_PCIE_ADV_ERROR_EXT_CAPABILITY* aer_capability =
179 (EFI_PCIE_ADV_ERROR_EXT_CAPABILITY*)section_cper->AerInfo.PcieAer;
180 aer_capability->Header.PcieExtendedCapabilityId =
181 json_object_get_uint64(json_object_object_get(aer_info, "capabilityID"));
182 aer_capability->Header.CapabilityVersion =
183 json_object_get_uint64(json_object_object_get(aer_info, "capabilityVersion"));
184 aer_capability->UncorrectableErrorStatusReg =
185 (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "uncorrectableErrorStatusRegister"));
186 aer_capability->UncorrectableErrorMaskReg =
187 (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "uncorrectableErrorMaskRegister"));
188 aer_capability->UncorrectableErrorSeverityReg =
189 (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "uncorrectableErrorSeverityRegister"));
190 aer_capability->CorrectableErrorStatusReg =
191 (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "correctableErrorStatusRegister"));
192 aer_capability->CorrectableErrorMaskReg =
193 (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "correctableErrorMaskRegister"));
194 aer_capability->AeccReg =
195 (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "aeccReg"));
196
197 //AER header log register.
198 encoded = json_object_object_get(aer_info, "headerLogRegister");
199 decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
200 memcpy(aer_capability->HeaderLogReg, decoded, 16);
201 free(decoded);
202
203 //Remaining AER fields.
204 aer_capability->RootErrorCommand =
205 (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "rootErrorCommand"));
206 aer_capability->RootErrorStatus =
207 (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "rootErrorStatus"));
208 aer_capability->ErrorSourceIdReg =
209 (UINT16)json_object_get_uint64(json_object_object_get(aer_info, "errorSourceIDRegister"));
210 aer_capability->CorrectableSourceIdReg =
211 (UINT16)json_object_get_uint64(json_object_object_get(aer_info, "correctableErrorSourceIDRegister"));
212
213
214 //Miscellaneous value fields.
215 section_cper->PortType = (UINT32)readable_pair_to_integer(json_object_object_get(section, "portType"));
216 section_cper->SerialNo = json_object_get_uint64(json_object_object_get(section, "deviceSerialNumber"));
217
218 //Write out to stream, free resources.
219 fwrite(&section_cper, sizeof(section_cper), 1, out);
220 fflush(out);
221 free(section_cper);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +0100222}