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 <string.h>
9 #include "json.h"
10 #include "b64.h"
11 #include "../edk/Cper.h"
12 #include "../cper-utils.h"
13 #include "cper-section-cxl-protocol.h"
14 
15 //Converts a single CXL protocol error CPER section into JSON IR.
16 json_object* cper_section_cxl_protocol_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
17 {
18     EFI_CXL_PROTOCOL_ERROR_DATA* cxl_protocol_error = (EFI_CXL_PROTOCOL_ERROR_DATA*)section;
19     json_object* section_ir = json_object_new_object();
20 
21     //Validation bits.
22     json_object* validation = bitfield_to_ir(cxl_protocol_error->ValidBits, 7, CXL_PROTOCOL_ERROR_VALID_BITFIELD_NAMES);
23     json_object_object_add(section_ir, "validationBits", validation);
24 
25     //Type of detecting agent.
26     json_object* agent_type = integer_to_readable_pair(cxl_protocol_error->CxlAgentType, 2,
27         CXL_PROTOCOL_ERROR_AGENT_TYPES_KEYS,
28         CXL_PROTOCOL_ERROR_AGENT_TYPES_VALUES,
29         "Unknown (Reserved)");
30     json_object_object_add(section_ir, "agentType", agent_type);
31 
32     //CXL agent address, depending on the agent type.
33     json_object* agent_address = json_object_new_object();
34     if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
35     {
36         //Address is a CXL1.1 device agent.
37         json_object_object_add(agent_address, "functionNumber",
38             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.FunctionNumber));
39         json_object_object_add(agent_address, "deviceNumber",
40             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.DeviceNumber));
41         json_object_object_add(agent_address, "busNumber",
42             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.BusNumber));
43         json_object_object_add(agent_address, "segmentNumber",
44             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.SegmentNumber));
45     }
46     else if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT)
47     {
48         //Address is a CXL port RCRB base address.
49         json_object_object_add(agent_address, "value",
50             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.PortRcrbBaseAddress));
51     }
52     json_object_object_add(section_ir, "cxlAgentAddress", agent_address);
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     //For CXL 1.1 host downstream ports, this is the "CXL DVSEC For Flex Bus Port" structure as in CXL 1.1 spec.
90     unsigned char* cur_pos = (unsigned char*)(cxl_protocol_error + 1);
91     char* encoded = b64_encode(cur_pos, cxl_protocol_error->CxlDvsecLength);
92     json_object_object_add(section_ir, "cxlDVSEC", json_object_new_string(encoded));
93     free(encoded);
94     cur_pos += cxl_protocol_error->CxlDvsecLength;
95 
96     //CXL Error Log
97     //This is the "CXL RAS Capability Structure" as in CXL 1.1 spec.
98     encoded = b64_encode(cur_pos, cxl_protocol_error->CxlErrorLogLength);
99     json_object_object_add(section_ir, "cxlErrorLog", json_object_new_string(encoded));
100     free(encoded);
101 
102     return section_ir;
103 }
104 
105 //Converts a single CXL protocol CPER-JSON section into CPER binary, outputting to the given stream.
106 void ir_section_cxl_protocol_to_cper(json_object* section, FILE* out)
107 {
108     EFI_CXL_PROTOCOL_ERROR_DATA* section_cper =
109         (EFI_CXL_PROTOCOL_ERROR_DATA*)calloc(1, sizeof(EFI_CXL_PROTOCOL_ERROR_DATA));
110 
111     //Validation bits.
112     section_cper->ValidBits = ir_to_bitfield(json_object_object_get(section, "validationBits"),
113         7, CXL_PROTOCOL_ERROR_VALID_BITFIELD_NAMES);
114 
115     //Detecting agent type.
116     section_cper->CxlAgentType = readable_pair_to_integer(json_object_object_get(section, "agentType"));
117 
118     //Based on the agent type, set the address.
119     json_object* address = json_object_object_get(section, "cxlAgentAddress");
120     if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
121     {
122         //Address is split by function, device, bus & segment.
123         UINT64 function = json_object_get_uint64(json_object_object_get(address, "functionNumber"));
124         UINT64 device = json_object_get_uint64(json_object_object_get(address, "deviceNumber"));
125         UINT64 bus = json_object_get_uint64(json_object_object_get(address, "busNumber"));
126         UINT64 segment = json_object_get_uint64(json_object_object_get(address, "segmentNumber"));
127         section_cper->CxlAgentAddress.DeviceAddress.FunctionNumber = function;
128         section_cper->CxlAgentAddress.DeviceAddress.DeviceNumber = device;
129         section_cper->CxlAgentAddress.DeviceAddress.BusNumber = bus;
130         section_cper->CxlAgentAddress.DeviceAddress.SegmentNumber = segment;
131     }
132     else if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT)
133     {
134         //Plain RCRB base address.
135         section_cper->CxlAgentAddress.PortRcrbBaseAddress =
136             json_object_get_uint64(json_object_object_get(address, "value"));
137     }
138 
139     //Device ID information.
140     json_object* device_id = json_object_object_get(section, "deviceID");
141     section_cper->DeviceId.VendorId = json_object_get_uint64(json_object_object_get(device_id, "vendorID"));
142     section_cper->DeviceId.DeviceId = json_object_get_uint64(json_object_object_get(device_id, "deviceID"));
143     section_cper->DeviceId.SubsystemVendorId =
144         json_object_get_uint64(json_object_object_get(device_id, "subsystemVendorID"));
145     section_cper->DeviceId.SubsystemDeviceId =
146         json_object_get_uint64(json_object_object_get(device_id, "subsystemDeviceID"));
147     section_cper->DeviceId.ClassCode = json_object_get_uint64(json_object_object_get(device_id, "classCode"));
148     section_cper->DeviceId.SlotNumber = json_object_get_uint64(json_object_object_get(device_id, "slotNumber"));
149 
150     //If CXL 1.1 device, the serial number & PCI capability structure.
151     if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
152     {
153         section_cper->DeviceSerial = json_object_get_uint64(json_object_object_get(section, "deviceSerial"));
154 
155         json_object* encoded = json_object_object_get(section, "capabilityStructure");
156         char* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
157         memcpy(section_cper->CapabilityStructure.PcieCap, decoded, 60);
158         free(decoded);
159     }
160 
161     //DVSEC length & error log length.
162     section_cper->CxlDvsecLength = (UINT16)json_object_get_int(json_object_object_get(section, "dvsecLength"));
163     section_cper->CxlErrorLogLength = (UINT16)json_object_get_int(json_object_object_get(section, "errorLogLength"));
164 
165     //Write header to stream.
166     fwrite(section_cper, sizeof(section_cper), 1, out);
167     fflush(out);
168 
169     //DVSEC out to stream.
170     json_object* encoded = json_object_object_get(section, "cxlDVSEC");
171     char* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
172     fwrite(decoded, section_cper->CxlDvsecLength, 1, out);
173     fflush(out);
174     free(decoded);
175 
176     //Error log out to stream.
177     encoded = json_object_object_get(section, "cxlErrorLog");
178     decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
179     fwrite(decoded, section_cper->CxlErrorLogLength, 1, out);
180     fflush(out);
181     free(decoded);
182 
183     free(section_cper);
184 }