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>
8b98ec66cSLawrence Tang #include "json.h"
9368e0b40SLawrence Tang #include "b64.h"
10b98ec66cSLawrence Tang #include "../edk/Cper.h"
11b98ec66cSLawrence Tang #include "../cper-utils.h"
12b98ec66cSLawrence Tang #include "cper-section-cxl-protocol.h"
13b98ec66cSLawrence Tang 
14b98ec66cSLawrence Tang //Converts a single CXL protocol error CPER section into JSON IR.
15b98ec66cSLawrence Tang json_object* cper_section_cxl_protocol_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
16b98ec66cSLawrence Tang {
17b98ec66cSLawrence Tang     EFI_CXL_PROTOCOL_ERROR_DATA* cxl_protocol_error = (EFI_CXL_PROTOCOL_ERROR_DATA*)section;
18b98ec66cSLawrence Tang     json_object* section_ir = json_object_new_object();
19b98ec66cSLawrence Tang 
20b98ec66cSLawrence Tang     //Validation bits.
21b98ec66cSLawrence Tang     json_object* validation = bitfield_to_ir(cxl_protocol_error->ValidBits, 7, CXL_PROTOCOL_ERROR_VALID_BITFIELD_NAMES);
22b98ec66cSLawrence Tang     json_object_object_add(section_ir, "validationBits", validation);
23b98ec66cSLawrence Tang 
24b98ec66cSLawrence Tang     //Type of detecting agent.
25b98ec66cSLawrence Tang     json_object* agent_type = integer_to_readable_pair(cxl_protocol_error->CxlAgentType, 2,
26b98ec66cSLawrence Tang         CXL_PROTOCOL_ERROR_AGENT_TYPES_KEYS,
27b98ec66cSLawrence Tang         CXL_PROTOCOL_ERROR_AGENT_TYPES_VALUES,
28b98ec66cSLawrence Tang         "Unknown (Reserved)");
29b98ec66cSLawrence Tang     json_object_object_add(section_ir, "agentType", agent_type);
30b98ec66cSLawrence Tang 
31b98ec66cSLawrence Tang     //CXL agent address, depending on the agent type.
32*cef2a592SLawrence Tang     json_object* agent_address = json_object_new_object();
33b98ec66cSLawrence Tang     if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
34b98ec66cSLawrence Tang     {
35b98ec66cSLawrence Tang         //Address is a CXL1.1 device agent.
36b98ec66cSLawrence Tang         json_object_object_add(agent_address, "functionNumber",
37b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.FunctionNumber));
38b98ec66cSLawrence Tang         json_object_object_add(agent_address, "deviceNumber",
39b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.DeviceNumber));
40b98ec66cSLawrence Tang         json_object_object_add(agent_address, "busNumber",
41b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.BusNumber));
42b98ec66cSLawrence Tang         json_object_object_add(agent_address, "segmentNumber",
43b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.SegmentNumber));
44b98ec66cSLawrence Tang     }
45b98ec66cSLawrence Tang     else if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT)
46b98ec66cSLawrence Tang     {
47b98ec66cSLawrence Tang         //Address is a CXL port RCRB base address.
48*cef2a592SLawrence Tang         json_object_object_add(agent_address, "value",
49b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.PortRcrbBaseAddress));
50b98ec66cSLawrence Tang     }
51*cef2a592SLawrence Tang     json_object_object_add(section_ir, "cxlAgentAddress", agent_address);
52b98ec66cSLawrence Tang 
53b98ec66cSLawrence Tang     //Device ID.
54b98ec66cSLawrence Tang     json_object* device_id = json_object_new_object();
55b98ec66cSLawrence Tang     json_object_object_add(device_id, "vendorID",
56b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.VendorId));
57b98ec66cSLawrence Tang     json_object_object_add(device_id, "deviceID",
58b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.DeviceId));
59b98ec66cSLawrence Tang     json_object_object_add(device_id, "subsystemVendorID",
60b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.SubsystemVendorId));
61b98ec66cSLawrence Tang     json_object_object_add(device_id, "subsystemDeviceID",
62b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.SubsystemDeviceId));
63b98ec66cSLawrence Tang     json_object_object_add(device_id, "classCode",
64b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.ClassCode));
65b98ec66cSLawrence Tang     json_object_object_add(device_id, "slotNumber",
66b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.SlotNumber));
67b98ec66cSLawrence Tang     json_object_object_add(section_ir, "deviceID", device_id);
68b98ec66cSLawrence Tang 
69b98ec66cSLawrence Tang     //Device serial & capability structure (if CXL 1.1 device).
70b98ec66cSLawrence Tang     if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
71b98ec66cSLawrence Tang     {
72b98ec66cSLawrence Tang         json_object_object_add(section_ir, "deviceSerial", json_object_new_uint64(cxl_protocol_error->DeviceSerial));
73368e0b40SLawrence Tang 
74368e0b40SLawrence Tang         //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure
75368e0b40SLawrence Tang         //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem
76368e0b40SLawrence Tang         //to be a way to differentiate these, so this is left as a b64 dump.
77368e0b40SLawrence Tang         char* encoded = b64_encode(cxl_protocol_error->CapabilityStructure.PcieCap, 60);
78368e0b40SLawrence Tang         json_object_object_add(section_ir, "capabilityStructure", json_object_new_uint64(cxl_protocol_error->DeviceSerial));
79368e0b40SLawrence Tang         free(encoded);
80b98ec66cSLawrence Tang     }
81b98ec66cSLawrence Tang 
82b98ec66cSLawrence Tang     //CXL DVSEC & error log length.
83b98ec66cSLawrence Tang     json_object_object_add(section_ir, "dvsecLength", json_object_new_int(cxl_protocol_error->CxlDvsecLength));
84b98ec66cSLawrence Tang     json_object_object_add(section_ir, "errorLogLength", json_object_new_int(cxl_protocol_error->CxlErrorLogLength));
85b98ec66cSLawrence Tang 
86b98ec66cSLawrence Tang     //CXL DVSEC
87368e0b40SLawrence Tang     //For CXL 1.1 devices, this is the "CXL DVSEC For Flex Bus Device" structure as in CXL 1.1 spec.
88*cef2a592SLawrence Tang     //For CXL 1.1 host downstream ports, this is the "CXL DVSEC For Flex Bus Port" structure as in CXL 1.1 spec.
89368e0b40SLawrence Tang     unsigned char* cur_pos = (unsigned char*)(cxl_protocol_error + 1);
90368e0b40SLawrence Tang     char* encoded = b64_encode(cur_pos, cxl_protocol_error->CxlDvsecLength);
91*cef2a592SLawrence Tang     json_object_object_add(section_ir, "cxlDVSEC", json_object_new_string(encoded));
92368e0b40SLawrence Tang     free(encoded);
93368e0b40SLawrence Tang     cur_pos += cxl_protocol_error->CxlDvsecLength;
94368e0b40SLawrence Tang 
95b98ec66cSLawrence Tang     //CXL Error Log
96*cef2a592SLawrence Tang     //This is the "CXL RAS Capability Structure" as in CXL 1.1 spec.
97*cef2a592SLawrence Tang     encoded = b64_encode(cur_pos, cxl_protocol_error->CxlErrorLogLength);
98*cef2a592SLawrence Tang     json_object_object_add(section_ir, "cxlErrorLog", json_object_new_string(encoded));
99*cef2a592SLawrence Tang     free(encoded);
100b98ec66cSLawrence Tang 
101b98ec66cSLawrence Tang     return section_ir;
102b98ec66cSLawrence Tang }