blob: ac13187d1cfdd30af7f48d7072c1c82dc80767b5 [file] [log] [blame]
Lawrence Tang214a1542022-07-06 14:11:28 +01001/**
2 * Describes functions for converting PCI/PCI-X bus CPER sections from binary and JSON format
3 * into an intermediate format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang214a1542022-07-06 14:11:28 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang0a4b3f22022-07-21 10:40:10 +01008#include <string.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000010#include <libcper/Cper.h>
11#include <libcper/cper-utils.h>
12#include <libcper/sections/cper-section-pci-bus.h>
Lawrence Tang214a1542022-07-06 14:11:28 +010013
14//Converts a single PCI/PCI-X bus CPER section into JSON IR.
Ed Tanous12dbd4f2025-03-08 19:05:01 -080015json_object *cper_section_pci_bus_to_ir(const UINT8 *section, UINT32 size)
Lawrence Tang214a1542022-07-06 14:11:28 +010016{
Ed Tanous12dbd4f2025-03-08 19:05:01 -080017 if (size < sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA)) {
18 return NULL;
19 }
20
Lawrence Tange407b4c2022-07-21 13:54:01 +010021 EFI_PCI_PCIX_BUS_ERROR_DATA *bus_error =
22 (EFI_PCI_PCIX_BUS_ERROR_DATA *)section;
23 json_object *section_ir = json_object_new_object();
Lawrence Tang214a1542022-07-06 14:11:28 +010024
Lawrence Tange407b4c2022-07-21 13:54:01 +010025 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080026 ValidationTypes ui64Type = { UINT_64T,
27 .value.ui64 = bus_error->ValidFields };
Lawrence Tang214a1542022-07-06 14:11:28 +010028
Lawrence Tange407b4c2022-07-21 13:54:01 +010029 //Error status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080030 if (isvalid_prop_to_ir(&ui64Type, 0)) {
31 json_object *error_status = cper_generic_error_status_to_ir(
32 &bus_error->ErrorStatus);
33 json_object_object_add(section_ir, "errorStatus", error_status);
34 }
Lawrence Tang214a1542022-07-06 14:11:28 +010035
Lawrence Tange407b4c2022-07-21 13:54:01 +010036 //PCI bus error type.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080037 if (isvalid_prop_to_ir(&ui64Type, 1)) {
38 json_object *error_type = integer_to_readable_pair(
39 bus_error->Type, 8, PCI_BUS_ERROR_TYPES_KEYS,
40 PCI_BUS_ERROR_TYPES_VALUES, "Unknown (Reserved)");
41 json_object_object_add(section_ir, "errorType", error_type);
42 }
Lawrence Tang214a1542022-07-06 14:11:28 +010043
Lawrence Tange407b4c2022-07-21 13:54:01 +010044 //Bus ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080045 if (isvalid_prop_to_ir(&ui64Type, 2)) {
46 json_object *bus_id = json_object_new_object();
47 json_object_object_add(bus_id, "busNumber",
48 json_object_new_int(bus_error->BusId &
49 0xFF));
50 json_object_object_add(bus_id, "segmentNumber",
51 json_object_new_int(bus_error->BusId >>
52 8));
53 json_object_object_add(section_ir, "busID", bus_id);
54 }
Lawrence Tang214a1542022-07-06 14:11:28 +010055
Lawrence Tange407b4c2022-07-21 13:54:01 +010056 //Miscellaneous numeric fields.
Ed Tanous5e2164a2025-03-09 09:20:44 -070057 //Byte 7, bit 0.
58 UINT8 command_type = (bus_error->BusCommand >> 56) & 0x1;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080059 if (isvalid_prop_to_ir(&ui64Type, 3)) {
60 json_object_object_add(
61 section_ir, "busAddress",
62 json_object_new_uint64(bus_error->BusAddress));
63 }
64 if (isvalid_prop_to_ir(&ui64Type, 4)) {
65 json_object_object_add(
66 section_ir, "busData",
67 json_object_new_uint64(bus_error->BusData));
68 }
69 if (isvalid_prop_to_ir(&ui64Type, 5)) {
70 json_object_object_add(
71 section_ir, "busCommandType",
72 json_object_new_string(command_type == 0 ? "PCI" :
73 "PCI-X"));
74 }
Aushim Nagarkatticc367012024-12-05 18:17:27 -080075 char hexstring_buf[EFI_UINT64_HEX_STRING_LEN];
Aushim Nagarkatticc367012024-12-05 18:17:27 -080076
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080077 if (isvalid_prop_to_ir(&ui64Type, 6)) {
78 json_object_object_add(
79 section_ir, "busRequestorID",
80 json_object_new_uint64(bus_error->RequestorId));
Aushim Nagarkatticc367012024-12-05 18:17:27 -080081
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080082 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX",
83 bus_error->RequestorId);
84 json_object_object_add(section_ir, "busRequestorIDHex",
85 json_object_new_string(hexstring_buf));
86 }
87
88 if (isvalid_prop_to_ir(&ui64Type, 7)) {
89 json_object_object_add(
90 section_ir, "busCompleterID",
91 json_object_new_uint64(bus_error->ResponderId));
92 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX",
93 bus_error->ResponderId);
94 json_object_object_add(section_ir, "busCompleterIDHex",
95 json_object_new_string(hexstring_buf));
96 }
97
98 if (isvalid_prop_to_ir(&ui64Type, 8)) {
99 json_object_object_add(
100 section_ir, "targetID",
101 json_object_new_uint64(bus_error->TargetId));
102 }
Lawrence Tang214a1542022-07-06 14:11:28 +0100103
Lawrence Tange407b4c2022-07-21 13:54:01 +0100104 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100105}
106
107//Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the
108//provided stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100109void ir_section_pci_bus_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100110{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100111 EFI_PCI_PCIX_BUS_ERROR_DATA *section_cper =
112 (EFI_PCI_PCIX_BUS_ERROR_DATA *)calloc(
113 1, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100114
Lawrence Tange407b4c2022-07-21 13:54:01 +0100115 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800116 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
117 struct json_object *obj = NULL;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100118
Lawrence Tange407b4c2022-07-21 13:54:01 +0100119 //Error status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800120 if (json_object_object_get_ex(section, "errorStatus", &obj)) {
121 ir_generic_error_status_to_cper(obj,
122 &section_cper->ErrorStatus);
123 add_to_valid_bitfield(&ui64Type, 0);
124 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100125
Lawrence Tange407b4c2022-07-21 13:54:01 +0100126 //Bus ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800127 if (json_object_object_get_ex(section, "busID", &obj)) {
128 json_object *bus_id = json_object_object_get(section, "busID");
129 UINT16 bus_number = (UINT8)json_object_get_int(
130 json_object_object_get(bus_id, "busNumber"));
131 UINT16 segment_number = (UINT8)json_object_get_int(
132 json_object_object_get(bus_id, "segmentNumber"));
133 section_cper->BusId = bus_number + (segment_number << 8);
134 add_to_valid_bitfield(&ui64Type, 2);
135 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100136
Lawrence Tange407b4c2022-07-21 13:54:01 +0100137 //Remaining fields.
138 UINT64 pcix_command = (UINT64)0x1 << 56;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800139
140 if (json_object_object_get_ex(section, "errorType", &obj)) {
141 section_cper->Type = (UINT16)readable_pair_to_integer(obj);
142 add_to_valid_bitfield(&ui64Type, 1);
143 }
144 if (json_object_object_get_ex(section, "busAddress", &obj)) {
145 section_cper->BusAddress = json_object_get_uint64(
146 json_object_object_get(section, "busAddress"));
147 add_to_valid_bitfield(&ui64Type, 3);
148 }
149 if (json_object_object_get_ex(section, "busData", &obj)) {
150 section_cper->BusData = json_object_get_uint64(obj);
151 add_to_valid_bitfield(&ui64Type, 4);
152 }
153 if (json_object_object_get_ex(section, "busCommandType", &obj)) {
154 const char *bus_command = json_object_get_string(obj);
155 section_cper->BusCommand =
156 strcmp(bus_command, "PCI") == 0 ? 0 : pcix_command;
157 add_to_valid_bitfield(&ui64Type, 5);
158 }
159 if (json_object_object_get_ex(section, "busRequestorID", &obj)) {
160 section_cper->RequestorId = json_object_get_uint64(obj);
161 add_to_valid_bitfield(&ui64Type, 6);
162 }
163 if (json_object_object_get_ex(section, "busCompleterID", &obj)) {
164 section_cper->ResponderId = json_object_get_uint64(obj);
165 add_to_valid_bitfield(&ui64Type, 7);
166 }
167 if (json_object_object_get_ex(section, "targetID", &obj)) {
168 section_cper->TargetId = json_object_get_uint64(obj);
169 add_to_valid_bitfield(&ui64Type, 8);
170 }
171 section_cper->ValidFields = ui64Type.value.ui64;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100172
Lawrence Tange407b4c2022-07-21 13:54:01 +0100173 //Write to stream, free resources.
174 fwrite(section_cper, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA), 1, out);
175 fflush(out);
176 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800177}