14dbe3d72SLawrence Tang /** 24dbe3d72SLawrence Tang * Describes functions for converting PCIe CPER sections from binary and JSON format 34dbe3d72SLawrence Tang * into an intermediate format. 44dbe3d72SLawrence Tang * 54dbe3d72SLawrence Tang * Author: Lawrence.Tang@arm.com 64dbe3d72SLawrence Tang **/ 74dbe3d72SLawrence Tang #include <stdio.h> 83b7f45b5SLawrence Tang #include <string.h> 94dbe3d72SLawrence Tang #include "json.h" 106c461e90SLawrence Tang #include "b64.h" 114dbe3d72SLawrence Tang #include "../edk/Cper.h" 124dbe3d72SLawrence Tang #include "../cper-utils.h" 134dbe3d72SLawrence Tang #include "cper-section-pcie.h" 144dbe3d72SLawrence Tang 154dbe3d72SLawrence Tang //Converts a single PCIe CPER section into JSON IR. 164dbe3d72SLawrence Tang json_object* cper_section_pcie_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor) 174dbe3d72SLawrence Tang { 184dbe3d72SLawrence Tang EFI_PCIE_ERROR_DATA* pcie_error = (EFI_PCIE_ERROR_DATA*)section; 194dbe3d72SLawrence Tang json_object* section_ir = json_object_new_object(); 204dbe3d72SLawrence Tang 214dbe3d72SLawrence Tang //Validation bits. 224dbe3d72SLawrence Tang json_object* validation = bitfield_to_ir(pcie_error->ValidFields, 8, PCIE_ERROR_VALID_BITFIELD_NAMES); 234dbe3d72SLawrence Tang json_object_object_add(section_ir, "validationBits", validation); 244dbe3d72SLawrence Tang 254dbe3d72SLawrence Tang //Port type. 264dbe3d72SLawrence Tang json_object* port_type = integer_to_readable_pair(pcie_error->PortType, 9, 274dbe3d72SLawrence Tang PCIE_ERROR_PORT_TYPES_KEYS, 284dbe3d72SLawrence Tang PCIE_ERROR_PORT_TYPES_VALUES, 294dbe3d72SLawrence Tang "Unknown"); 304dbe3d72SLawrence Tang json_object_object_add(section_ir, "portType", port_type); 314dbe3d72SLawrence Tang 324dbe3d72SLawrence Tang //Version, provided each half in BCD. 334dbe3d72SLawrence Tang json_object* version = json_object_new_object(); 344dbe3d72SLawrence Tang json_object_object_add(version, "minor", json_object_new_int(bcd_to_int(pcie_error->Version & 0xFF))); 354dbe3d72SLawrence Tang json_object_object_add(version, "major", json_object_new_int(bcd_to_int(pcie_error->Version >> 8))); 364dbe3d72SLawrence Tang json_object_object_add(section_ir, "version", version); 374dbe3d72SLawrence Tang 384dbe3d72SLawrence Tang //Command & status. 394dbe3d72SLawrence Tang json_object* command_status = json_object_new_object(); 404dbe3d72SLawrence Tang json_object_object_add(command_status, "commandRegister", json_object_new_uint64(pcie_error->CommandStatus & 0xFFFF)); 414dbe3d72SLawrence Tang json_object_object_add(command_status, "statusRegister", json_object_new_uint64(pcie_error->CommandStatus >> 16)); 424dbe3d72SLawrence Tang json_object_object_add(section_ir, "commandStatus", command_status); 434dbe3d72SLawrence Tang 444dbe3d72SLawrence Tang //PCIe Device ID. 454dbe3d72SLawrence Tang json_object* device_id = json_object_new_object(); 46db1b7ce2SLawrence Tang UINT64 class_id = (pcie_error->DevBridge.ClassCode[0] << 16) + 474dbe3d72SLawrence Tang (pcie_error->DevBridge.ClassCode[1] << 8) + 48db1b7ce2SLawrence Tang pcie_error->DevBridge.ClassCode[2]; 494dbe3d72SLawrence Tang json_object_object_add(device_id, "vendorID", json_object_new_uint64(pcie_error->DevBridge.VendorId)); 504dbe3d72SLawrence Tang json_object_object_add(device_id, "deviceID", json_object_new_uint64(pcie_error->DevBridge.DeviceId)); 514dbe3d72SLawrence Tang json_object_object_add(device_id, "classCode", json_object_new_uint64(class_id)); 524dbe3d72SLawrence Tang json_object_object_add(device_id, "functionNumber", json_object_new_uint64(pcie_error->DevBridge.Function)); 534dbe3d72SLawrence Tang json_object_object_add(device_id, "deviceNumber", json_object_new_uint64(pcie_error->DevBridge.Device)); 544dbe3d72SLawrence Tang json_object_object_add(device_id, "segmentNumber", json_object_new_uint64(pcie_error->DevBridge.Segment)); 554dbe3d72SLawrence Tang json_object_object_add(device_id, "primaryOrDeviceBusNumber", json_object_new_uint64(pcie_error->DevBridge.PrimaryOrDeviceBus)); 564dbe3d72SLawrence Tang json_object_object_add(device_id, "secondaryBusNumber", json_object_new_uint64(pcie_error->DevBridge.SecondaryBus)); 574dbe3d72SLawrence Tang json_object_object_add(device_id, "slotNumber", json_object_new_uint64(pcie_error->DevBridge.Slot.Number)); 584dbe3d72SLawrence Tang json_object_object_add(section_ir, "deviceID", device_id); 594dbe3d72SLawrence Tang 604dbe3d72SLawrence Tang //Device serial number. 614dbe3d72SLawrence Tang json_object_object_add(section_ir, "deviceSerialNumber", json_object_new_uint64(pcie_error->SerialNo)); 624dbe3d72SLawrence Tang 634dbe3d72SLawrence Tang //Bridge control status. 644dbe3d72SLawrence Tang json_object* bridge_control_status = json_object_new_object(); 654dbe3d72SLawrence Tang json_object_object_add(bridge_control_status, "secondaryStatusRegister", 664dbe3d72SLawrence Tang json_object_new_uint64(pcie_error->BridgeControlStatus & 0xFFFF)); 674dbe3d72SLawrence Tang json_object_object_add(bridge_control_status, "controlRegister", 684dbe3d72SLawrence Tang json_object_new_uint64(pcie_error->BridgeControlStatus >> 16)); 694dbe3d72SLawrence Tang json_object_object_add(section_ir, "bridgeControlStatus", bridge_control_status); 704dbe3d72SLawrence Tang 714dbe3d72SLawrence Tang //Capability structure. 726c461e90SLawrence Tang //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure 736c461e90SLawrence Tang //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem 746c461e90SLawrence Tang //to be a way to differentiate these, so this is left as a b64 dump. 756c461e90SLawrence Tang char* encoded = b64_encode((unsigned char*)pcie_error->Capability.PcieCap, 60); 766c461e90SLawrence Tang json_object* capability = json_object_new_object(); 776c461e90SLawrence Tang json_object_object_add(capability, "data", json_object_new_string(encoded)); 786c461e90SLawrence Tang free(encoded); 796c461e90SLawrence Tang json_object_object_add(section_ir, "capabilityStructure", capability); 804dbe3d72SLawrence Tang 814dbe3d72SLawrence Tang //AER information. 826c461e90SLawrence Tang json_object* aer_capability_ir = json_object_new_object(); 83*3ab351feSLawrence Tang encoded = b64_encode((unsigned char*)pcie_error->AerInfo.PcieAer, 96); 84*3ab351feSLawrence Tang json_object_object_add(aer_capability_ir, "data", json_object_new_string(encoded)); 856c461e90SLawrence Tang free(encoded); 866c461e90SLawrence Tang json_object_object_add(section_ir, "aerInfo", aer_capability_ir); 874dbe3d72SLawrence Tang return section_ir; 884dbe3d72SLawrence Tang } 893b7f45b5SLawrence Tang 903b7f45b5SLawrence Tang //Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream. 913b7f45b5SLawrence Tang void ir_section_pcie_to_cper(json_object* section, FILE* out) 923b7f45b5SLawrence Tang { 933b7f45b5SLawrence Tang EFI_PCIE_ERROR_DATA* section_cper = (EFI_PCIE_ERROR_DATA*)calloc(1, sizeof(EFI_PCIE_ERROR_DATA)); 943b7f45b5SLawrence Tang 953b7f45b5SLawrence Tang //Validation bits. 963b7f45b5SLawrence Tang section_cper->ValidFields = ir_to_bitfield(json_object_object_get(section, "validationBits"), 973b7f45b5SLawrence Tang 8, PCIE_ERROR_VALID_BITFIELD_NAMES); 983b7f45b5SLawrence Tang 993b7f45b5SLawrence Tang //Version. 1003b7f45b5SLawrence Tang json_object* version = json_object_object_get(section, "version"); 101*3ab351feSLawrence Tang UINT32 minor = int_to_bcd(json_object_get_int(json_object_object_get(version, "minor"))); 102*3ab351feSLawrence Tang UINT32 major = int_to_bcd(json_object_get_int(json_object_object_get(version, "major"))); 103*3ab351feSLawrence Tang section_cper->Version = minor + (major << 8); 1043b7f45b5SLawrence Tang 1053b7f45b5SLawrence Tang //Command/status registers. 1063b7f45b5SLawrence Tang json_object* command_status = json_object_object_get(section, "commandStatus"); 1073b7f45b5SLawrence Tang UINT32 command = (UINT16)json_object_get_uint64(json_object_object_get(command_status, "commandRegister")); 1083b7f45b5SLawrence Tang UINT32 status = (UINT16)json_object_get_uint64(json_object_object_get(command_status, "statusRegister")); 1093b7f45b5SLawrence Tang section_cper->CommandStatus = command + (status << 16); 1103b7f45b5SLawrence Tang 1113b7f45b5SLawrence Tang //Device ID. 1123b7f45b5SLawrence Tang json_object* device_id = json_object_object_get(section, "deviceID"); 1133b7f45b5SLawrence Tang UINT64 class_id = json_object_get_uint64(json_object_object_get(device_id, "classCode")); 1143b7f45b5SLawrence Tang section_cper->DevBridge.VendorId = 1153b7f45b5SLawrence Tang (UINT16)json_object_get_uint64(json_object_object_get(device_id, "vendorID")); 1163b7f45b5SLawrence Tang section_cper->DevBridge.DeviceId = 1173b7f45b5SLawrence Tang (UINT16)json_object_get_uint64(json_object_object_get(device_id, "deviceID")); 1183b7f45b5SLawrence Tang section_cper->DevBridge.ClassCode[0] = class_id >> 16; 1193b7f45b5SLawrence Tang section_cper->DevBridge.ClassCode[1] = (class_id >> 8) & 0xFF; 120*3ab351feSLawrence Tang section_cper->DevBridge.ClassCode[2] = class_id & 0xFF; 1213b7f45b5SLawrence Tang section_cper->DevBridge.Function = 1223b7f45b5SLawrence Tang (UINT8)json_object_get_uint64(json_object_object_get(device_id, "functionNumber")); 1233b7f45b5SLawrence Tang section_cper->DevBridge.Device = 1243b7f45b5SLawrence Tang (UINT8)json_object_get_uint64(json_object_object_get(device_id, "deviceNumber")); 1253b7f45b5SLawrence Tang section_cper->DevBridge.Segment = 1263b7f45b5SLawrence Tang (UINT16)json_object_get_uint64(json_object_object_get(device_id, "segmentNumber")); 1273b7f45b5SLawrence Tang section_cper->DevBridge.PrimaryOrDeviceBus = 1283b7f45b5SLawrence Tang (UINT8)json_object_get_uint64(json_object_object_get(device_id, "primaryOrDeviceBusNumber")); 1293b7f45b5SLawrence Tang section_cper->DevBridge.SecondaryBus = 1303b7f45b5SLawrence Tang (UINT8)json_object_get_uint64(json_object_object_get(device_id, "secondaryBusNumber")); 1313b7f45b5SLawrence Tang section_cper->DevBridge.Slot.Number = 1323b7f45b5SLawrence Tang (UINT16)json_object_get_uint64(json_object_object_get(device_id, "slotNumber")); 1333b7f45b5SLawrence Tang 1343b7f45b5SLawrence Tang //Bridge/control status. 1353b7f45b5SLawrence Tang json_object* bridge_control = json_object_object_get(section, "bridgeControlStatus"); 1363b7f45b5SLawrence Tang UINT32 bridge_status = (UINT16)json_object_get_uint64(json_object_object_get(bridge_control, "secondaryStatusRegister")); 1373b7f45b5SLawrence Tang UINT32 control_status = (UINT16)json_object_get_uint64(json_object_object_get(bridge_control, "controlRegister")); 1383b7f45b5SLawrence Tang section_cper->BridgeControlStatus = bridge_status + (control_status << 16); 1393b7f45b5SLawrence Tang 1403b7f45b5SLawrence Tang //Capability structure. 1413b7f45b5SLawrence Tang json_object* capability = json_object_object_get(section, "capabilityStructure"); 1423b7f45b5SLawrence Tang json_object* encoded = json_object_object_get(capability, "data"); 1433b7f45b5SLawrence Tang UINT8* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 1443b7f45b5SLawrence Tang memcpy(section_cper->Capability.PcieCap, decoded, 60); 1453b7f45b5SLawrence Tang free(decoded); 1463b7f45b5SLawrence Tang 1473b7f45b5SLawrence Tang //AER capability structure. 1483b7f45b5SLawrence Tang json_object* aer_info = json_object_object_get(section, "aerInfo"); 149*3ab351feSLawrence Tang encoded = json_object_object_get(aer_info, "data"); 1503b7f45b5SLawrence Tang decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 151*3ab351feSLawrence Tang memcpy(section_cper->AerInfo.PcieAer, decoded, 96); 1523b7f45b5SLawrence Tang free(decoded); 1533b7f45b5SLawrence Tang 1543b7f45b5SLawrence Tang //Miscellaneous value fields. 1553b7f45b5SLawrence Tang section_cper->PortType = (UINT32)readable_pair_to_integer(json_object_object_get(section, "portType")); 1563b7f45b5SLawrence Tang section_cper->SerialNo = json_object_get_uint64(json_object_object_get(section, "deviceSerialNumber")); 1573b7f45b5SLawrence Tang 1583b7f45b5SLawrence Tang //Write out to stream, free resources. 159*3ab351feSLawrence Tang fwrite(section_cper, sizeof(EFI_PCIE_ERROR_DATA), 1, out); 1603b7f45b5SLawrence Tang fflush(out); 1613b7f45b5SLawrence Tang free(section_cper); 1623b7f45b5SLawrence Tang }