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     json_object* agent_address = json_object_new_object();
33     if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
34     {
35         //Address is a CXL1.1 device agent.
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     else if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT)
46     {
47         //Address is a CXL port RCRB base address.
48         json_object_object_add(agent_address, "value",
49             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.PortRcrbBaseAddress));
50     }
51     json_object_object_add(section_ir, "cxlAgentAddress", agent_address);
52 
53     //Device ID.
54     json_object* device_id = json_object_new_object();
55     json_object_object_add(device_id, "vendorID",
56         json_object_new_uint64(cxl_protocol_error->DeviceId.VendorId));
57     json_object_object_add(device_id, "deviceID",
58         json_object_new_uint64(cxl_protocol_error->DeviceId.DeviceId));
59     json_object_object_add(device_id, "subsystemVendorID",
60         json_object_new_uint64(cxl_protocol_error->DeviceId.SubsystemVendorId));
61     json_object_object_add(device_id, "subsystemDeviceID",
62         json_object_new_uint64(cxl_protocol_error->DeviceId.SubsystemDeviceId));
63     json_object_object_add(device_id, "classCode",
64         json_object_new_uint64(cxl_protocol_error->DeviceId.ClassCode));
65     json_object_object_add(device_id, "slotNumber",
66         json_object_new_uint64(cxl_protocol_error->DeviceId.SlotNumber));
67     json_object_object_add(section_ir, "deviceID", device_id);
68 
69     //Device serial & capability structure (if CXL 1.1 device).
70     if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
71     {
72         json_object_object_add(section_ir, "deviceSerial", json_object_new_uint64(cxl_protocol_error->DeviceSerial));
73 
74         //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure
75         //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem
76         //to be a way to differentiate these, so this is left as a b64 dump.
77         char* encoded = b64_encode(cxl_protocol_error->CapabilityStructure.PcieCap, 60);
78         json_object_object_add(section_ir, "capabilityStructure", json_object_new_uint64(cxl_protocol_error->DeviceSerial));
79         free(encoded);
80     }
81 
82     //CXL DVSEC & error log length.
83     json_object_object_add(section_ir, "dvsecLength", json_object_new_int(cxl_protocol_error->CxlDvsecLength));
84     json_object_object_add(section_ir, "errorLogLength", json_object_new_int(cxl_protocol_error->CxlErrorLogLength));
85 
86     //CXL DVSEC
87     //For CXL 1.1 devices, this is the "CXL DVSEC For Flex Bus Device" structure as in CXL 1.1 spec.
88     //For CXL 1.1 host downstream ports, this is the "CXL DVSEC For Flex Bus Port" 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, "cxlDVSEC", json_object_new_string(encoded));
92     free(encoded);
93     cur_pos += cxl_protocol_error->CxlDvsecLength;
94 
95     //CXL Error Log
96     //This is the "CXL RAS Capability Structure" as in CXL 1.1 spec.
97     encoded = b64_encode(cur_pos, cxl_protocol_error->CxlErrorLogLength);
98     json_object_object_add(section_ir, "cxlErrorLog", json_object_new_string(encoded));
99     free(encoded);
100 
101     return section_ir;
102 }