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 "../edk/Cper.h"
10 #include "../cper-utils.h"
11 #include "cper-section-cxl-protocol.h"
12 
13 //Converts a single CXL protocol error CPER section into JSON IR.
14 json_object* cper_section_cxl_protocol_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
15 {
16     EFI_CXL_PROTOCOL_ERROR_DATA* cxl_protocol_error = (EFI_CXL_PROTOCOL_ERROR_DATA*)section;
17     json_object* section_ir = json_object_new_object();
18 
19     //Validation bits.
20     json_object* validation = bitfield_to_ir(cxl_protocol_error->ValidBits, 7, CXL_PROTOCOL_ERROR_VALID_BITFIELD_NAMES);
21     json_object_object_add(section_ir, "validationBits", validation);
22 
23     //Type of detecting agent.
24     json_object* agent_type = integer_to_readable_pair(cxl_protocol_error->CxlAgentType, 2,
25         CXL_PROTOCOL_ERROR_AGENT_TYPES_KEYS,
26         CXL_PROTOCOL_ERROR_AGENT_TYPES_VALUES,
27         "Unknown (Reserved)");
28     json_object_object_add(section_ir, "agentType", agent_type);
29 
30     //CXL agent address, depending on the agent type.
31     if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
32     {
33         //Address is a CXL1.1 device agent.
34         json_object* agent_address = json_object_new_object();
35         json_object_object_add(agent_address, "functionNumber",
36             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.FunctionNumber));
37         json_object_object_add(agent_address, "deviceNumber",
38             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.DeviceNumber));
39         json_object_object_add(agent_address, "busNumber",
40             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.BusNumber));
41         json_object_object_add(agent_address, "segmentNumber",
42             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.SegmentNumber));
43 
44         json_object_object_add(section_ir, "cxlAgentAddress", agent_address);
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(section_ir, "cxlAgentAddress",
50             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.PortRcrbBaseAddress));
51     }
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         //todo: add generic parser for PCI capability structure (see Cper.h)
74     }
75 
76     //CXL DVSEC & error log length.
77     json_object_object_add(section_ir, "dvsecLength", json_object_new_int(cxl_protocol_error->CxlDvsecLength));
78     json_object_object_add(section_ir, "errorLogLength", json_object_new_int(cxl_protocol_error->CxlErrorLogLength));
79 
80     //CXL DVSEC
81     //todo: for CXL 1.1 devices, implement this as the "CXL DVSEC For Flex Bus Device" structure as in CXL 1.1 spec.
82     //todo: for CXL 1.1 host downstream port, implement this as "CXL DVSEC For Flex Bus Port" structure as in CXL 1.1 spec.
83 
84     //CXL Error Log
85     //todo: implement this as the "CXL RAS Capability Structure" as in CXL 1.1 spec.
86 
87     return section_ir;
88 }