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> 8aec83900SLawrence 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); 794237584eSLawrence Tang json_object_object_add(section_ir, "capabilityStructure", json_object_new_string(encoded)); 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 } 104aec83900SLawrence Tang 105aec83900SLawrence Tang //Converts a single CXL protocol CPER-JSON section into CPER binary, outputting to the given stream. 106aec83900SLawrence Tang void ir_section_cxl_protocol_to_cper(json_object* section, FILE* out) 107aec83900SLawrence Tang { 108aec83900SLawrence Tang EFI_CXL_PROTOCOL_ERROR_DATA* section_cper = 109aec83900SLawrence Tang (EFI_CXL_PROTOCOL_ERROR_DATA*)calloc(1, sizeof(EFI_CXL_PROTOCOL_ERROR_DATA)); 110aec83900SLawrence Tang 111aec83900SLawrence Tang //Validation bits. 112aec83900SLawrence Tang section_cper->ValidBits = ir_to_bitfield(json_object_object_get(section, "validationBits"), 113aec83900SLawrence Tang 7, CXL_PROTOCOL_ERROR_VALID_BITFIELD_NAMES); 114aec83900SLawrence Tang 115aec83900SLawrence Tang //Detecting agent type. 116aec83900SLawrence Tang section_cper->CxlAgentType = readable_pair_to_integer(json_object_object_get(section, "agentType")); 117aec83900SLawrence Tang 118aec83900SLawrence Tang //Based on the agent type, set the address. 119aec83900SLawrence Tang json_object* address = json_object_object_get(section, "cxlAgentAddress"); 120aec83900SLawrence Tang if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT) 121aec83900SLawrence Tang { 122aec83900SLawrence Tang //Address is split by function, device, bus & segment. 123aec83900SLawrence Tang UINT64 function = json_object_get_uint64(json_object_object_get(address, "functionNumber")); 124aec83900SLawrence Tang UINT64 device = json_object_get_uint64(json_object_object_get(address, "deviceNumber")); 125aec83900SLawrence Tang UINT64 bus = json_object_get_uint64(json_object_object_get(address, "busNumber")); 126aec83900SLawrence Tang UINT64 segment = json_object_get_uint64(json_object_object_get(address, "segmentNumber")); 127aec83900SLawrence Tang section_cper->CxlAgentAddress.DeviceAddress.FunctionNumber = function; 128aec83900SLawrence Tang section_cper->CxlAgentAddress.DeviceAddress.DeviceNumber = device; 129aec83900SLawrence Tang section_cper->CxlAgentAddress.DeviceAddress.BusNumber = bus; 130aec83900SLawrence Tang section_cper->CxlAgentAddress.DeviceAddress.SegmentNumber = segment; 131aec83900SLawrence Tang } 132aec83900SLawrence Tang else if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT) 133aec83900SLawrence Tang { 134aec83900SLawrence Tang //Plain RCRB base address. 135aec83900SLawrence Tang section_cper->CxlAgentAddress.PortRcrbBaseAddress = 136aec83900SLawrence Tang json_object_get_uint64(json_object_object_get(address, "value")); 137aec83900SLawrence Tang } 138aec83900SLawrence Tang 139aec83900SLawrence Tang //Device ID information. 140aec83900SLawrence Tang json_object* device_id = json_object_object_get(section, "deviceID"); 141aec83900SLawrence Tang section_cper->DeviceId.VendorId = json_object_get_uint64(json_object_object_get(device_id, "vendorID")); 142aec83900SLawrence Tang section_cper->DeviceId.DeviceId = json_object_get_uint64(json_object_object_get(device_id, "deviceID")); 143aec83900SLawrence Tang section_cper->DeviceId.SubsystemVendorId = 144aec83900SLawrence Tang json_object_get_uint64(json_object_object_get(device_id, "subsystemVendorID")); 145aec83900SLawrence Tang section_cper->DeviceId.SubsystemDeviceId = 146aec83900SLawrence Tang json_object_get_uint64(json_object_object_get(device_id, "subsystemDeviceID")); 147aec83900SLawrence Tang section_cper->DeviceId.ClassCode = json_object_get_uint64(json_object_object_get(device_id, "classCode")); 148aec83900SLawrence Tang section_cper->DeviceId.SlotNumber = json_object_get_uint64(json_object_object_get(device_id, "slotNumber")); 149aec83900SLawrence Tang 150aec83900SLawrence Tang //If CXL 1.1 device, the serial number & PCI capability structure. 151aec83900SLawrence Tang if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT) 152aec83900SLawrence Tang { 153aec83900SLawrence Tang section_cper->DeviceSerial = json_object_get_uint64(json_object_object_get(section, "deviceSerial")); 154aec83900SLawrence Tang 155aec83900SLawrence Tang json_object* encoded = json_object_object_get(section, "capabilityStructure"); 156aec83900SLawrence Tang char* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 157aec83900SLawrence Tang memcpy(section_cper->CapabilityStructure.PcieCap, decoded, 60); 158aec83900SLawrence Tang free(decoded); 159aec83900SLawrence Tang } 160aec83900SLawrence Tang 161aec83900SLawrence Tang //DVSEC length & error log length. 162aec83900SLawrence Tang section_cper->CxlDvsecLength = (UINT16)json_object_get_int(json_object_object_get(section, "dvsecLength")); 163aec83900SLawrence Tang section_cper->CxlErrorLogLength = (UINT16)json_object_get_int(json_object_object_get(section, "errorLogLength")); 164aec83900SLawrence Tang 165aec83900SLawrence Tang //Write header to stream. 166*3ab351feSLawrence Tang fwrite(section_cper, sizeof(EFI_CXL_PROTOCOL_ERROR_DATA), 1, out); 167aec83900SLawrence Tang fflush(out); 168aec83900SLawrence Tang 169aec83900SLawrence Tang //DVSEC out to stream. 170aec83900SLawrence Tang json_object* encoded = json_object_object_get(section, "cxlDVSEC"); 171aec83900SLawrence Tang char* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 172aec83900SLawrence Tang fwrite(decoded, section_cper->CxlDvsecLength, 1, out); 173aec83900SLawrence Tang fflush(out); 174aec83900SLawrence Tang free(decoded); 175aec83900SLawrence Tang 176aec83900SLawrence Tang //Error log out to stream. 177aec83900SLawrence Tang encoded = json_object_object_get(section, "cxlErrorLog"); 178aec83900SLawrence Tang decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 179aec83900SLawrence Tang fwrite(decoded, section_cper->CxlErrorLogLength, 1, out); 180aec83900SLawrence Tang fflush(out); 181aec83900SLawrence Tang free(decoded); 182aec83900SLawrence Tang 183aec83900SLawrence Tang free(section_cper); 184aec83900SLawrence Tang }