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