1b98ec66cSLawrence Tang /** 2b98ec66cSLawrence Tang * Describes functions for converting CXL protocol error CPER sections from binary and JSON format 3b98ec66cSLawrence Tang * into an intermediate format. 4b98ec66cSLawrence Tang * 5b98ec66cSLawrence Tang * Author: Lawrence.Tang@arm.com 6b98ec66cSLawrence Tang **/ 7b98ec66cSLawrence Tang #include <stdio.h> 8*aec83900SLawrence Tang #include <string.h> 9b98ec66cSLawrence Tang #include "json.h" 10368e0b40SLawrence Tang #include "b64.h" 11b98ec66cSLawrence Tang #include "../edk/Cper.h" 12b98ec66cSLawrence Tang #include "../cper-utils.h" 13b98ec66cSLawrence Tang #include "cper-section-cxl-protocol.h" 14b98ec66cSLawrence Tang 15b98ec66cSLawrence Tang //Converts a single CXL protocol error CPER section into JSON IR. 16b98ec66cSLawrence Tang json_object* cper_section_cxl_protocol_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor) 17b98ec66cSLawrence Tang { 18b98ec66cSLawrence Tang EFI_CXL_PROTOCOL_ERROR_DATA* cxl_protocol_error = (EFI_CXL_PROTOCOL_ERROR_DATA*)section; 19b98ec66cSLawrence Tang json_object* section_ir = json_object_new_object(); 20b98ec66cSLawrence Tang 21b98ec66cSLawrence Tang //Validation bits. 22b98ec66cSLawrence Tang json_object* validation = bitfield_to_ir(cxl_protocol_error->ValidBits, 7, CXL_PROTOCOL_ERROR_VALID_BITFIELD_NAMES); 23b98ec66cSLawrence Tang json_object_object_add(section_ir, "validationBits", validation); 24b98ec66cSLawrence Tang 25b98ec66cSLawrence Tang //Type of detecting agent. 26b98ec66cSLawrence Tang json_object* agent_type = integer_to_readable_pair(cxl_protocol_error->CxlAgentType, 2, 27b98ec66cSLawrence Tang CXL_PROTOCOL_ERROR_AGENT_TYPES_KEYS, 28b98ec66cSLawrence Tang CXL_PROTOCOL_ERROR_AGENT_TYPES_VALUES, 29b98ec66cSLawrence Tang "Unknown (Reserved)"); 30b98ec66cSLawrence Tang json_object_object_add(section_ir, "agentType", agent_type); 31b98ec66cSLawrence Tang 32b98ec66cSLawrence Tang //CXL agent address, depending on the agent type. 33cef2a592SLawrence Tang json_object* agent_address = json_object_new_object(); 34b98ec66cSLawrence Tang if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT) 35b98ec66cSLawrence Tang { 36b98ec66cSLawrence Tang //Address is a CXL1.1 device agent. 37b98ec66cSLawrence Tang json_object_object_add(agent_address, "functionNumber", 38b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.FunctionNumber)); 39b98ec66cSLawrence Tang json_object_object_add(agent_address, "deviceNumber", 40b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.DeviceNumber)); 41b98ec66cSLawrence Tang json_object_object_add(agent_address, "busNumber", 42b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.BusNumber)); 43b98ec66cSLawrence Tang json_object_object_add(agent_address, "segmentNumber", 44b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.SegmentNumber)); 45b98ec66cSLawrence Tang } 46b98ec66cSLawrence Tang else if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT) 47b98ec66cSLawrence Tang { 48b98ec66cSLawrence Tang //Address is a CXL port RCRB base address. 49cef2a592SLawrence Tang json_object_object_add(agent_address, "value", 50b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.PortRcrbBaseAddress)); 51b98ec66cSLawrence Tang } 52cef2a592SLawrence Tang json_object_object_add(section_ir, "cxlAgentAddress", agent_address); 53b98ec66cSLawrence Tang 54b98ec66cSLawrence Tang //Device ID. 55b98ec66cSLawrence Tang json_object* device_id = json_object_new_object(); 56b98ec66cSLawrence Tang json_object_object_add(device_id, "vendorID", 57b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->DeviceId.VendorId)); 58b98ec66cSLawrence Tang json_object_object_add(device_id, "deviceID", 59b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->DeviceId.DeviceId)); 60b98ec66cSLawrence Tang json_object_object_add(device_id, "subsystemVendorID", 61b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->DeviceId.SubsystemVendorId)); 62b98ec66cSLawrence Tang json_object_object_add(device_id, "subsystemDeviceID", 63b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->DeviceId.SubsystemDeviceId)); 64b98ec66cSLawrence Tang json_object_object_add(device_id, "classCode", 65b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->DeviceId.ClassCode)); 66b98ec66cSLawrence Tang json_object_object_add(device_id, "slotNumber", 67b98ec66cSLawrence Tang json_object_new_uint64(cxl_protocol_error->DeviceId.SlotNumber)); 68b98ec66cSLawrence Tang json_object_object_add(section_ir, "deviceID", device_id); 69b98ec66cSLawrence Tang 70b98ec66cSLawrence Tang //Device serial & capability structure (if CXL 1.1 device). 71b98ec66cSLawrence Tang if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT) 72b98ec66cSLawrence Tang { 73b98ec66cSLawrence Tang json_object_object_add(section_ir, "deviceSerial", json_object_new_uint64(cxl_protocol_error->DeviceSerial)); 74368e0b40SLawrence Tang 75368e0b40SLawrence Tang //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure 76368e0b40SLawrence Tang //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem 77368e0b40SLawrence Tang //to be a way to differentiate these, so this is left as a b64 dump. 78368e0b40SLawrence Tang char* encoded = b64_encode(cxl_protocol_error->CapabilityStructure.PcieCap, 60); 79368e0b40SLawrence Tang json_object_object_add(section_ir, "capabilityStructure", json_object_new_uint64(cxl_protocol_error->DeviceSerial)); 80368e0b40SLawrence Tang free(encoded); 81b98ec66cSLawrence Tang } 82b98ec66cSLawrence Tang 83b98ec66cSLawrence Tang //CXL DVSEC & error log length. 84b98ec66cSLawrence Tang json_object_object_add(section_ir, "dvsecLength", json_object_new_int(cxl_protocol_error->CxlDvsecLength)); 85b98ec66cSLawrence Tang json_object_object_add(section_ir, "errorLogLength", json_object_new_int(cxl_protocol_error->CxlErrorLogLength)); 86b98ec66cSLawrence Tang 87b98ec66cSLawrence Tang //CXL DVSEC 88368e0b40SLawrence Tang //For CXL 1.1 devices, this is the "CXL DVSEC For Flex Bus Device" structure as in CXL 1.1 spec. 89cef2a592SLawrence Tang //For CXL 1.1 host downstream ports, this is the "CXL DVSEC For Flex Bus Port" structure as in CXL 1.1 spec. 90368e0b40SLawrence Tang unsigned char* cur_pos = (unsigned char*)(cxl_protocol_error + 1); 91368e0b40SLawrence Tang char* encoded = b64_encode(cur_pos, cxl_protocol_error->CxlDvsecLength); 92cef2a592SLawrence Tang json_object_object_add(section_ir, "cxlDVSEC", json_object_new_string(encoded)); 93368e0b40SLawrence Tang free(encoded); 94368e0b40SLawrence Tang cur_pos += cxl_protocol_error->CxlDvsecLength; 95368e0b40SLawrence Tang 96b98ec66cSLawrence Tang //CXL Error Log 97cef2a592SLawrence Tang //This is the "CXL RAS Capability Structure" as in CXL 1.1 spec. 98cef2a592SLawrence Tang encoded = b64_encode(cur_pos, cxl_protocol_error->CxlErrorLogLength); 99cef2a592SLawrence Tang json_object_object_add(section_ir, "cxlErrorLog", json_object_new_string(encoded)); 100cef2a592SLawrence Tang free(encoded); 101b98ec66cSLawrence Tang 102b98ec66cSLawrence Tang return section_ir; 103b98ec66cSLawrence Tang } 104*aec83900SLawrence Tang 105*aec83900SLawrence Tang //Converts a single CXL protocol CPER-JSON section into CPER binary, outputting to the given stream. 106*aec83900SLawrence Tang void ir_section_cxl_protocol_to_cper(json_object* section, FILE* out) 107*aec83900SLawrence Tang { 108*aec83900SLawrence Tang EFI_CXL_PROTOCOL_ERROR_DATA* section_cper = 109*aec83900SLawrence Tang (EFI_CXL_PROTOCOL_ERROR_DATA*)calloc(1, sizeof(EFI_CXL_PROTOCOL_ERROR_DATA)); 110*aec83900SLawrence Tang 111*aec83900SLawrence Tang //Validation bits. 112*aec83900SLawrence Tang section_cper->ValidBits = ir_to_bitfield(json_object_object_get(section, "validationBits"), 113*aec83900SLawrence Tang 7, CXL_PROTOCOL_ERROR_VALID_BITFIELD_NAMES); 114*aec83900SLawrence Tang 115*aec83900SLawrence Tang //Detecting agent type. 116*aec83900SLawrence Tang section_cper->CxlAgentType = readable_pair_to_integer(json_object_object_get(section, "agentType")); 117*aec83900SLawrence Tang 118*aec83900SLawrence Tang //Based on the agent type, set the address. 119*aec83900SLawrence Tang json_object* address = json_object_object_get(section, "cxlAgentAddress"); 120*aec83900SLawrence Tang if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT) 121*aec83900SLawrence Tang { 122*aec83900SLawrence Tang //Address is split by function, device, bus & segment. 123*aec83900SLawrence Tang UINT64 function = json_object_get_uint64(json_object_object_get(address, "functionNumber")); 124*aec83900SLawrence Tang UINT64 device = json_object_get_uint64(json_object_object_get(address, "deviceNumber")); 125*aec83900SLawrence Tang UINT64 bus = json_object_get_uint64(json_object_object_get(address, "busNumber")); 126*aec83900SLawrence Tang UINT64 segment = json_object_get_uint64(json_object_object_get(address, "segmentNumber")); 127*aec83900SLawrence Tang section_cper->CxlAgentAddress.DeviceAddress.FunctionNumber = function; 128*aec83900SLawrence Tang section_cper->CxlAgentAddress.DeviceAddress.DeviceNumber = device; 129*aec83900SLawrence Tang section_cper->CxlAgentAddress.DeviceAddress.BusNumber = bus; 130*aec83900SLawrence Tang section_cper->CxlAgentAddress.DeviceAddress.SegmentNumber = segment; 131*aec83900SLawrence Tang } 132*aec83900SLawrence Tang else if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT) 133*aec83900SLawrence Tang { 134*aec83900SLawrence Tang //Plain RCRB base address. 135*aec83900SLawrence Tang section_cper->CxlAgentAddress.PortRcrbBaseAddress = 136*aec83900SLawrence Tang json_object_get_uint64(json_object_object_get(address, "value")); 137*aec83900SLawrence Tang } 138*aec83900SLawrence Tang 139*aec83900SLawrence Tang //Device ID information. 140*aec83900SLawrence Tang json_object* device_id = json_object_object_get(section, "deviceID"); 141*aec83900SLawrence Tang section_cper->DeviceId.VendorId = json_object_get_uint64(json_object_object_get(device_id, "vendorID")); 142*aec83900SLawrence Tang section_cper->DeviceId.DeviceId = json_object_get_uint64(json_object_object_get(device_id, "deviceID")); 143*aec83900SLawrence Tang section_cper->DeviceId.SubsystemVendorId = 144*aec83900SLawrence Tang json_object_get_uint64(json_object_object_get(device_id, "subsystemVendorID")); 145*aec83900SLawrence Tang section_cper->DeviceId.SubsystemDeviceId = 146*aec83900SLawrence Tang json_object_get_uint64(json_object_object_get(device_id, "subsystemDeviceID")); 147*aec83900SLawrence Tang section_cper->DeviceId.ClassCode = json_object_get_uint64(json_object_object_get(device_id, "classCode")); 148*aec83900SLawrence Tang section_cper->DeviceId.SlotNumber = json_object_get_uint64(json_object_object_get(device_id, "slotNumber")); 149*aec83900SLawrence Tang 150*aec83900SLawrence Tang //If CXL 1.1 device, the serial number & PCI capability structure. 151*aec83900SLawrence Tang if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT) 152*aec83900SLawrence Tang { 153*aec83900SLawrence Tang section_cper->DeviceSerial = json_object_get_uint64(json_object_object_get(section, "deviceSerial")); 154*aec83900SLawrence Tang 155*aec83900SLawrence Tang json_object* encoded = json_object_object_get(section, "capabilityStructure"); 156*aec83900SLawrence Tang char* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 157*aec83900SLawrence Tang memcpy(section_cper->CapabilityStructure.PcieCap, decoded, 60); 158*aec83900SLawrence Tang free(decoded); 159*aec83900SLawrence Tang } 160*aec83900SLawrence Tang 161*aec83900SLawrence Tang //DVSEC length & error log length. 162*aec83900SLawrence Tang section_cper->CxlDvsecLength = (UINT16)json_object_get_int(json_object_object_get(section, "dvsecLength")); 163*aec83900SLawrence Tang section_cper->CxlErrorLogLength = (UINT16)json_object_get_int(json_object_object_get(section, "errorLogLength")); 164*aec83900SLawrence Tang 165*aec83900SLawrence Tang //Write header to stream. 166*aec83900SLawrence Tang fwrite(section_cper, sizeof(section_cper), 1, out); 167*aec83900SLawrence Tang fflush(out); 168*aec83900SLawrence Tang 169*aec83900SLawrence Tang //DVSEC out to stream. 170*aec83900SLawrence Tang json_object* encoded = json_object_object_get(section, "cxlDVSEC"); 171*aec83900SLawrence Tang char* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 172*aec83900SLawrence Tang fwrite(decoded, section_cper->CxlDvsecLength, 1, out); 173*aec83900SLawrence Tang fflush(out); 174*aec83900SLawrence Tang free(decoded); 175*aec83900SLawrence Tang 176*aec83900SLawrence Tang //Error log out to stream. 177*aec83900SLawrence Tang encoded = json_object_object_get(section, "cxlErrorLog"); 178*aec83900SLawrence Tang decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 179*aec83900SLawrence Tang fwrite(decoded, section_cper->CxlErrorLogLength, 1, out); 180*aec83900SLawrence Tang fflush(out); 181*aec83900SLawrence Tang free(decoded); 182*aec83900SLawrence Tang 183*aec83900SLawrence Tang free(section_cper); 184*aec83900SLawrence Tang }