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"
9*368e0b40SLawrence 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.
32b98ec66cSLawrence Tang     if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
33b98ec66cSLawrence Tang     {
34b98ec66cSLawrence Tang         //Address is a CXL1.1 device agent.
35b98ec66cSLawrence Tang         json_object* agent_address = json_object_new_object();
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         json_object_object_add(section_ir, "cxlAgentAddress", agent_address);
46b98ec66cSLawrence Tang     }
47b98ec66cSLawrence Tang     else if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT)
48b98ec66cSLawrence Tang     {
49b98ec66cSLawrence Tang         //Address is a CXL port RCRB base address.
50b98ec66cSLawrence Tang         json_object_object_add(section_ir, "cxlAgentAddress",
51b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.PortRcrbBaseAddress));
52b98ec66cSLawrence Tang     }
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));
74*368e0b40SLawrence Tang 
75*368e0b40SLawrence Tang         //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure
76*368e0b40SLawrence Tang         //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem
77*368e0b40SLawrence Tang         //to be a way to differentiate these, so this is left as a b64 dump.
78*368e0b40SLawrence Tang         char* encoded = b64_encode(cxl_protocol_error->CapabilityStructure.PcieCap, 60);
79*368e0b40SLawrence Tang         json_object_object_add(section_ir, "capabilityStructure", json_object_new_uint64(cxl_protocol_error->DeviceSerial));
80*368e0b40SLawrence 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
88*368e0b40SLawrence Tang     //For CXL 1.1 devices, this is the "CXL DVSEC For Flex Bus Device" structure as in CXL 1.1 spec.
89*368e0b40SLawrence Tang     unsigned char* cur_pos = (unsigned char*)(cxl_protocol_error + 1);
90*368e0b40SLawrence Tang     char* encoded = b64_encode(cur_pos, cxl_protocol_error->CxlDvsecLength);
91*368e0b40SLawrence Tang     json_object_object_add(section_ir, "capabilityStructure", json_object_new_uint64(cxl_protocol_error->DeviceSerial));
92*368e0b40SLawrence Tang     free(encoded);
93*368e0b40SLawrence Tang     cur_pos += cxl_protocol_error->CxlDvsecLength;
94*368e0b40SLawrence Tang 
95*368e0b40SLawrence Tang     //For CXL 1.1 host downstream ports, this is the "CXL DVSEC For Flex Bus Port" structure as in CXL 1.1 spec.
96b98ec66cSLawrence Tang 
97b98ec66cSLawrence Tang     //CXL Error Log
98*368e0b40SLawrence Tang     //This as the "CXL RAS Capability Structure" as in CXL 1.1 spec.
99b98ec66cSLawrence Tang 
100b98ec66cSLawrence Tang     return section_ir;
101b98ec66cSLawrence Tang }