blob: 60840e73e866fbd0a720c45230294fa2e394b309 [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>
Ed Tanous50b966f2025-03-11 09:06:19 -070013#include <libcper/log.h>
Lawrence Tang214a1542022-07-06 14:11:28 +010014
15//Converts a single PCI/PCI-X bus CPER section into JSON IR.
Ed Tanous12dbd4f2025-03-08 19:05:01 -080016json_object *cper_section_pci_bus_to_ir(const UINT8 *section, UINT32 size)
Lawrence Tang214a1542022-07-06 14:11:28 +010017{
Ed Tanous12dbd4f2025-03-08 19:05:01 -080018 if (size < sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA)) {
19 return NULL;
20 }
21
Lawrence Tange407b4c2022-07-21 13:54:01 +010022 EFI_PCI_PCIX_BUS_ERROR_DATA *bus_error =
23 (EFI_PCI_PCIX_BUS_ERROR_DATA *)section;
24 json_object *section_ir = json_object_new_object();
Lawrence Tang214a1542022-07-06 14:11:28 +010025
Lawrence Tange407b4c2022-07-21 13:54:01 +010026 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080027 ValidationTypes ui64Type = { UINT_64T,
28 .value.ui64 = bus_error->ValidFields };
Lawrence Tang214a1542022-07-06 14:11:28 +010029
Lawrence Tange407b4c2022-07-21 13:54:01 +010030 //Error status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080031 if (isvalid_prop_to_ir(&ui64Type, 0)) {
32 json_object *error_status = cper_generic_error_status_to_ir(
33 &bus_error->ErrorStatus);
34 json_object_object_add(section_ir, "errorStatus", error_status);
35 }
Lawrence Tang214a1542022-07-06 14:11:28 +010036
Lawrence Tange407b4c2022-07-21 13:54:01 +010037 //PCI bus error type.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080038 if (isvalid_prop_to_ir(&ui64Type, 1)) {
39 json_object *error_type = integer_to_readable_pair(
40 bus_error->Type, 8, PCI_BUS_ERROR_TYPES_KEYS,
41 PCI_BUS_ERROR_TYPES_VALUES, "Unknown (Reserved)");
42 json_object_object_add(section_ir, "errorType", error_type);
43 }
Lawrence Tang214a1542022-07-06 14:11:28 +010044
Lawrence Tange407b4c2022-07-21 13:54:01 +010045 //Bus ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080046 if (isvalid_prop_to_ir(&ui64Type, 2)) {
47 json_object *bus_id = json_object_new_object();
48 json_object_object_add(bus_id, "busNumber",
49 json_object_new_int(bus_error->BusId &
50 0xFF));
51 json_object_object_add(bus_id, "segmentNumber",
52 json_object_new_int(bus_error->BusId >>
53 8));
54 json_object_object_add(section_ir, "busID", bus_id);
55 }
Lawrence Tang214a1542022-07-06 14:11:28 +010056
Lawrence Tange407b4c2022-07-21 13:54:01 +010057 //Miscellaneous numeric fields.
Ed Tanous5e2164a2025-03-09 09:20:44 -070058 //Byte 7, bit 0.
59 UINT8 command_type = (bus_error->BusCommand >> 56) & 0x1;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080060 if (isvalid_prop_to_ir(&ui64Type, 3)) {
61 json_object_object_add(
62 section_ir, "busAddress",
63 json_object_new_uint64(bus_error->BusAddress));
64 }
65 if (isvalid_prop_to_ir(&ui64Type, 4)) {
66 json_object_object_add(
67 section_ir, "busData",
68 json_object_new_uint64(bus_error->BusData));
69 }
70 if (isvalid_prop_to_ir(&ui64Type, 5)) {
71 json_object_object_add(
72 section_ir, "busCommandType",
73 json_object_new_string(command_type == 0 ? "PCI" :
74 "PCI-X"));
75 }
Aushim Nagarkatticc367012024-12-05 18:17:27 -080076 char hexstring_buf[EFI_UINT64_HEX_STRING_LEN];
Aushim Nagarkatticc367012024-12-05 18:17:27 -080077
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080078 if (isvalid_prop_to_ir(&ui64Type, 6)) {
79 json_object_object_add(
80 section_ir, "busRequestorID",
81 json_object_new_uint64(bus_error->RequestorId));
Aushim Nagarkatticc367012024-12-05 18:17:27 -080082
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080083 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX",
84 bus_error->RequestorId);
85 json_object_object_add(section_ir, "busRequestorIDHex",
86 json_object_new_string(hexstring_buf));
87 }
88
89 if (isvalid_prop_to_ir(&ui64Type, 7)) {
90 json_object_object_add(
91 section_ir, "busCompleterID",
92 json_object_new_uint64(bus_error->ResponderId));
93 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX",
94 bus_error->ResponderId);
95 json_object_object_add(section_ir, "busCompleterIDHex",
96 json_object_new_string(hexstring_buf));
97 }
98
99 if (isvalid_prop_to_ir(&ui64Type, 8)) {
100 json_object_object_add(
101 section_ir, "targetID",
102 json_object_new_uint64(bus_error->TargetId));
103 }
Lawrence Tang214a1542022-07-06 14:11:28 +0100104
Lawrence Tange407b4c2022-07-21 13:54:01 +0100105 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100106}
107
108//Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the
109//provided stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100110void ir_section_pci_bus_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100111{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100112 EFI_PCI_PCIX_BUS_ERROR_DATA *section_cper =
113 (EFI_PCI_PCIX_BUS_ERROR_DATA *)calloc(
114 1, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100115
Lawrence Tange407b4c2022-07-21 13:54:01 +0100116 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800117 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
118 struct json_object *obj = NULL;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100119
Lawrence Tange407b4c2022-07-21 13:54:01 +0100120 //Error status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800121 if (json_object_object_get_ex(section, "errorStatus", &obj)) {
122 ir_generic_error_status_to_cper(obj,
123 &section_cper->ErrorStatus);
124 add_to_valid_bitfield(&ui64Type, 0);
125 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100126
Lawrence Tange407b4c2022-07-21 13:54:01 +0100127 //Bus ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800128 if (json_object_object_get_ex(section, "busID", &obj)) {
129 json_object *bus_id = json_object_object_get(section, "busID");
130 UINT16 bus_number = (UINT8)json_object_get_int(
131 json_object_object_get(bus_id, "busNumber"));
132 UINT16 segment_number = (UINT8)json_object_get_int(
133 json_object_object_get(bus_id, "segmentNumber"));
134 section_cper->BusId = bus_number + (segment_number << 8);
135 add_to_valid_bitfield(&ui64Type, 2);
136 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100137
Lawrence Tange407b4c2022-07-21 13:54:01 +0100138 //Remaining fields.
139 UINT64 pcix_command = (UINT64)0x1 << 56;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800140
141 if (json_object_object_get_ex(section, "errorType", &obj)) {
142 section_cper->Type = (UINT16)readable_pair_to_integer(obj);
143 add_to_valid_bitfield(&ui64Type, 1);
144 }
145 if (json_object_object_get_ex(section, "busAddress", &obj)) {
146 section_cper->BusAddress = json_object_get_uint64(
147 json_object_object_get(section, "busAddress"));
148 add_to_valid_bitfield(&ui64Type, 3);
149 }
150 if (json_object_object_get_ex(section, "busData", &obj)) {
151 section_cper->BusData = json_object_get_uint64(obj);
152 add_to_valid_bitfield(&ui64Type, 4);
153 }
154 if (json_object_object_get_ex(section, "busCommandType", &obj)) {
155 const char *bus_command = json_object_get_string(obj);
156 section_cper->BusCommand =
157 strcmp(bus_command, "PCI") == 0 ? 0 : pcix_command;
158 add_to_valid_bitfield(&ui64Type, 5);
159 }
160 if (json_object_object_get_ex(section, "busRequestorID", &obj)) {
161 section_cper->RequestorId = json_object_get_uint64(obj);
162 add_to_valid_bitfield(&ui64Type, 6);
163 }
164 if (json_object_object_get_ex(section, "busCompleterID", &obj)) {
165 section_cper->ResponderId = json_object_get_uint64(obj);
166 add_to_valid_bitfield(&ui64Type, 7);
167 }
168 if (json_object_object_get_ex(section, "targetID", &obj)) {
169 section_cper->TargetId = json_object_get_uint64(obj);
170 add_to_valid_bitfield(&ui64Type, 8);
171 }
172 section_cper->ValidFields = ui64Type.value.ui64;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100173
Lawrence Tange407b4c2022-07-21 13:54:01 +0100174 //Write to stream, free resources.
175 fwrite(section_cper, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA), 1, out);
176 fflush(out);
177 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800178}