blob: d7fbf0ab82b26e835bdcb8a5c9f0c7b146bb0055 [file] [log] [blame]
Lawrence Tangb98ec662022-07-06 16:50:21 +01001/**
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"
Lawrence Tang368e0b42022-07-07 14:31:06 +01009#include "b64.h"
Lawrence Tangb98ec662022-07-06 16:50:21 +010010#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.
15json_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.
Lawrence Tangcef2a592022-07-11 17:00:48 +010032 json_object* agent_address = json_object_new_object();
Lawrence Tangb98ec662022-07-06 16:50:21 +010033 if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT)
34 {
35 //Address is a CXL1.1 device agent.
Lawrence Tangb98ec662022-07-06 16:50:21 +010036 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));
Lawrence Tangb98ec662022-07-06 16:50:21 +010044 }
45 else if (cxl_protocol_error->CxlAgentType == CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT)
46 {
47 //Address is a CXL port RCRB base address.
Lawrence Tangcef2a592022-07-11 17:00:48 +010048 json_object_object_add(agent_address, "value",
Lawrence Tangb98ec662022-07-06 16:50:21 +010049 json_object_new_uint64(cxl_protocol_error->CxlAgentAddress.PortRcrbBaseAddress));
50 }
Lawrence Tangcef2a592022-07-11 17:00:48 +010051 json_object_object_add(section_ir, "cxlAgentAddress", agent_address);
Lawrence Tangb98ec662022-07-06 16:50:21 +010052
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));
Lawrence Tang368e0b42022-07-07 14:31:06 +010073
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);
Lawrence Tangb98ec662022-07-06 16:50:21 +010080 }
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
Lawrence Tang368e0b42022-07-07 14:31:06 +010087 //For CXL 1.1 devices, this is the "CXL DVSEC For Flex Bus Device" structure as in CXL 1.1 spec.
Lawrence Tangcef2a592022-07-11 17:00:48 +010088 //For CXL 1.1 host downstream ports, this is the "CXL DVSEC For Flex Bus Port" structure as in CXL 1.1 spec.
Lawrence Tang368e0b42022-07-07 14:31:06 +010089 unsigned char* cur_pos = (unsigned char*)(cxl_protocol_error + 1);
90 char* encoded = b64_encode(cur_pos, cxl_protocol_error->CxlDvsecLength);
Lawrence Tangcef2a592022-07-11 17:00:48 +010091 json_object_object_add(section_ir, "cxlDVSEC", json_object_new_string(encoded));
Lawrence Tang368e0b42022-07-07 14:31:06 +010092 free(encoded);
93 cur_pos += cxl_protocol_error->CxlDvsecLength;
94
Lawrence Tangb98ec662022-07-06 16:50:21 +010095 //CXL Error Log
Lawrence Tangcef2a592022-07-11 17:00:48 +010096 //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);
Lawrence Tangb98ec662022-07-06 16:50:21 +0100100
101 return section_ir;
102}