1*b98ec66cSLawrence Tang /**
2*b98ec66cSLawrence Tang  * Describes functions for converting CXL protocol error CPER sections from binary and JSON format
3*b98ec66cSLawrence Tang  * into an intermediate format.
4*b98ec66cSLawrence Tang  *
5*b98ec66cSLawrence Tang  * Author: Lawrence.Tang@arm.com
6*b98ec66cSLawrence Tang  **/
7*b98ec66cSLawrence Tang #include <stdio.h>
8*b98ec66cSLawrence Tang #include "json.h"
9*b98ec66cSLawrence Tang #include "../edk/Cper.h"
10*b98ec66cSLawrence Tang #include "../cper-utils.h"
11*b98ec66cSLawrence Tang #include "cper-section-cxl-protocol.h"
12*b98ec66cSLawrence Tang 
13*b98ec66cSLawrence Tang //Converts a single CXL protocol error CPER section into JSON IR.
14*b98ec66cSLawrence Tang json_object* cper_section_cxl_protocol_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
15*b98ec66cSLawrence Tang {
16*b98ec66cSLawrence Tang     EFI_CXL_PROTOCOL_ERROR_DATA* cxl_protocol_error = (EFI_CXL_PROTOCOL_ERROR_DATA*)section;
17*b98ec66cSLawrence Tang     json_object* section_ir = json_object_new_object();
18*b98ec66cSLawrence Tang 
19*b98ec66cSLawrence Tang     //Validation bits.
20*b98ec66cSLawrence Tang     json_object* validation = bitfield_to_ir(cxl_protocol_error->ValidBits, 7, CXL_PROTOCOL_ERROR_VALID_BITFIELD_NAMES);
21*b98ec66cSLawrence Tang     json_object_object_add(section_ir, "validationBits", validation);
22*b98ec66cSLawrence Tang 
23*b98ec66cSLawrence Tang     //Type of detecting agent.
24*b98ec66cSLawrence Tang     json_object* agent_type = integer_to_readable_pair(cxl_protocol_error->CxlAgentType, 2,
25*b98ec66cSLawrence Tang         CXL_PROTOCOL_ERROR_AGENT_TYPES_KEYS,
26*b98ec66cSLawrence Tang         CXL_PROTOCOL_ERROR_AGENT_TYPES_VALUES,
27*b98ec66cSLawrence Tang         "Unknown (Reserved)");
28*b98ec66cSLawrence Tang     json_object_object_add(section_ir, "agentType", agent_type);
29*b98ec66cSLawrence Tang 
30*b98ec66cSLawrence Tang     //CXL agent address, depending on the agent type.
31*b98ec66cSLawrence Tang     if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
32*b98ec66cSLawrence Tang     {
33*b98ec66cSLawrence Tang         //Address is a CXL1.1 device agent.
34*b98ec66cSLawrence Tang         json_object* agent_address = json_object_new_object();
35*b98ec66cSLawrence Tang         json_object_object_add(agent_address, "functionNumber",
36*b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.FunctionNumber));
37*b98ec66cSLawrence Tang         json_object_object_add(agent_address, "deviceNumber",
38*b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.DeviceNumber));
39*b98ec66cSLawrence Tang         json_object_object_add(agent_address, "busNumber",
40*b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.BusNumber));
41*b98ec66cSLawrence Tang         json_object_object_add(agent_address, "segmentNumber",
42*b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.DeviceAddress.SegmentNumber));
43*b98ec66cSLawrence Tang 
44*b98ec66cSLawrence Tang         json_object_object_add(section_ir, "cxlAgentAddress", agent_address);
45*b98ec66cSLawrence Tang     }
46*b98ec66cSLawrence Tang     else if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT)
47*b98ec66cSLawrence Tang     {
48*b98ec66cSLawrence Tang         //Address is a CXL port RCRB base address.
49*b98ec66cSLawrence Tang         json_object_object_add(section_ir, "cxlAgentAddress",
50*b98ec66cSLawrence Tang             json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.PortRcrbBaseAddress));
51*b98ec66cSLawrence Tang     }
52*b98ec66cSLawrence Tang 
53*b98ec66cSLawrence Tang     //Device ID.
54*b98ec66cSLawrence Tang     json_object* device_id = json_object_new_object();
55*b98ec66cSLawrence Tang     json_object_object_add(device_id, "vendorID",
56*b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.VendorId));
57*b98ec66cSLawrence Tang     json_object_object_add(device_id, "deviceID",
58*b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.DeviceId));
59*b98ec66cSLawrence Tang     json_object_object_add(device_id, "subsystemVendorID",
60*b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.SubsystemVendorId));
61*b98ec66cSLawrence Tang     json_object_object_add(device_id, "subsystemDeviceID",
62*b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.SubsystemDeviceId));
63*b98ec66cSLawrence Tang     json_object_object_add(device_id, "classCode",
64*b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.ClassCode));
65*b98ec66cSLawrence Tang     json_object_object_add(device_id, "slotNumber",
66*b98ec66cSLawrence Tang         json_object_new_uint64(cxl_protocol_error->DeviceId.SlotNumber));
67*b98ec66cSLawrence Tang     json_object_object_add(section_ir, "deviceID", device_id);
68*b98ec66cSLawrence Tang 
69*b98ec66cSLawrence Tang     //Device serial & capability structure (if CXL 1.1 device).
70*b98ec66cSLawrence Tang     if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
71*b98ec66cSLawrence Tang     {
72*b98ec66cSLawrence Tang         json_object_object_add(section_ir, "deviceSerial", json_object_new_uint64(cxl_protocol_error->DeviceSerial));
73*b98ec66cSLawrence Tang         //todo: add generic parser for PCI capability structure (see Cper.h)
74*b98ec66cSLawrence Tang     }
75*b98ec66cSLawrence Tang 
76*b98ec66cSLawrence Tang     //CXL DVSEC & error log length.
77*b98ec66cSLawrence Tang     json_object_object_add(section_ir, "dvsecLength", json_object_new_int(cxl_protocol_error->CxlDvsecLength));
78*b98ec66cSLawrence Tang     json_object_object_add(section_ir, "errorLogLength", json_object_new_int(cxl_protocol_error->CxlErrorLogLength));
79*b98ec66cSLawrence Tang 
80*b98ec66cSLawrence Tang     //CXL DVSEC
81*b98ec66cSLawrence Tang     //todo: for CXL 1.1 devices, implement this as the "CXL DVSEC For Flex Bus Device" structure as in CXL 1.1 spec.
82*b98ec66cSLawrence Tang     //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*b98ec66cSLawrence Tang 
84*b98ec66cSLawrence Tang     //CXL Error Log
85*b98ec66cSLawrence Tang     //todo: implement this as the "CXL RAS Capability Structure" as in CXL 1.1 spec.
86*b98ec66cSLawrence Tang 
87*b98ec66cSLawrence Tang     return section_ir;
88*b98ec66cSLawrence Tang }