blob: a593aebfa58b055a837853c46c466c706edf67f2 [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>
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -070014#include <string.h>
Lawrence Tang214a1542022-07-06 14:11:28 +010015
16//Converts a single PCI/PCI-X bus CPER section into JSON IR.
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -070017json_object *cper_section_pci_bus_to_ir(const UINT8 *section, UINT32 size,
18 char **desc_string)
Lawrence Tang214a1542022-07-06 14:11:28 +010019{
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -070020 int outstr_len = 0;
21 *desc_string = malloc(SECTION_DESC_STRING_SIZE);
22 outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE,
23 "A PCI/PCI-X Bus Error occurred");
24 if (outstr_len < 0) {
25 cper_print_log(
26 "Error: Could not write to PCI/PCI-X Bus description string\n");
27 } else if (outstr_len > SECTION_DESC_STRING_SIZE) {
28 cper_print_log(
29 "Error: PCI/PCI-X Bus description string truncated\n");
30 }
31
Ed Tanous12dbd4f2025-03-08 19:05:01 -080032 if (size < sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA)) {
33 return NULL;
34 }
35
Lawrence Tange407b4c2022-07-21 13:54:01 +010036 EFI_PCI_PCIX_BUS_ERROR_DATA *bus_error =
37 (EFI_PCI_PCIX_BUS_ERROR_DATA *)section;
38 json_object *section_ir = json_object_new_object();
Lawrence Tang214a1542022-07-06 14:11:28 +010039
Lawrence Tange407b4c2022-07-21 13:54:01 +010040 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080041 ValidationTypes ui64Type = { UINT_64T,
42 .value.ui64 = bus_error->ValidFields };
Lawrence Tang214a1542022-07-06 14:11:28 +010043
Lawrence Tange407b4c2022-07-21 13:54:01 +010044 //Error status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080045 if (isvalid_prop_to_ir(&ui64Type, 0)) {
46 json_object *error_status = cper_generic_error_status_to_ir(
47 &bus_error->ErrorStatus);
48 json_object_object_add(section_ir, "errorStatus", error_status);
49 }
Lawrence Tang214a1542022-07-06 14:11:28 +010050
Lawrence Tange407b4c2022-07-21 13:54:01 +010051 //PCI bus error type.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080052 if (isvalid_prop_to_ir(&ui64Type, 1)) {
53 json_object *error_type = integer_to_readable_pair(
54 bus_error->Type, 8, PCI_BUS_ERROR_TYPES_KEYS,
55 PCI_BUS_ERROR_TYPES_VALUES, "Unknown (Reserved)");
56 json_object_object_add(section_ir, "errorType", error_type);
57 }
Lawrence Tang214a1542022-07-06 14:11:28 +010058
Lawrence Tange407b4c2022-07-21 13:54:01 +010059 //Bus ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080060 if (isvalid_prop_to_ir(&ui64Type, 2)) {
61 json_object *bus_id = json_object_new_object();
62 json_object_object_add(bus_id, "busNumber",
63 json_object_new_int(bus_error->BusId &
64 0xFF));
65 json_object_object_add(bus_id, "segmentNumber",
66 json_object_new_int(bus_error->BusId >>
67 8));
68 json_object_object_add(section_ir, "busID", bus_id);
69 }
Lawrence Tang214a1542022-07-06 14:11:28 +010070
Lawrence Tange407b4c2022-07-21 13:54:01 +010071 //Miscellaneous numeric fields.
Ed Tanous5e2164a2025-03-09 09:20:44 -070072 //Byte 7, bit 0.
73 UINT8 command_type = (bus_error->BusCommand >> 56) & 0x1;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080074 if (isvalid_prop_to_ir(&ui64Type, 3)) {
75 json_object_object_add(
76 section_ir, "busAddress",
77 json_object_new_uint64(bus_error->BusAddress));
78 }
79 if (isvalid_prop_to_ir(&ui64Type, 4)) {
80 json_object_object_add(
81 section_ir, "busData",
82 json_object_new_uint64(bus_error->BusData));
83 }
84 if (isvalid_prop_to_ir(&ui64Type, 5)) {
85 json_object_object_add(
86 section_ir, "busCommandType",
87 json_object_new_string(command_type == 0 ? "PCI" :
88 "PCI-X"));
89 }
Aushim Nagarkatticc367012024-12-05 18:17:27 -080090 char hexstring_buf[EFI_UINT64_HEX_STRING_LEN];
Aushim Nagarkatticc367012024-12-05 18:17:27 -080091
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080092 if (isvalid_prop_to_ir(&ui64Type, 6)) {
93 json_object_object_add(
94 section_ir, "busRequestorID",
95 json_object_new_uint64(bus_error->RequestorId));
Aushim Nagarkatticc367012024-12-05 18:17:27 -080096
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080097 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX",
98 bus_error->RequestorId);
99 json_object_object_add(section_ir, "busRequestorIDHex",
100 json_object_new_string(hexstring_buf));
101 }
102
103 if (isvalid_prop_to_ir(&ui64Type, 7)) {
104 json_object_object_add(
105 section_ir, "busCompleterID",
106 json_object_new_uint64(bus_error->ResponderId));
107 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX",
108 bus_error->ResponderId);
109 json_object_object_add(section_ir, "busCompleterIDHex",
110 json_object_new_string(hexstring_buf));
111 }
112
113 if (isvalid_prop_to_ir(&ui64Type, 8)) {
114 json_object_object_add(
115 section_ir, "targetID",
116 json_object_new_uint64(bus_error->TargetId));
117 }
Lawrence Tang214a1542022-07-06 14:11:28 +0100118
Lawrence Tange407b4c2022-07-21 13:54:01 +0100119 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100120}
121
122//Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the
123//provided stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100124void ir_section_pci_bus_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100125{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100126 EFI_PCI_PCIX_BUS_ERROR_DATA *section_cper =
127 (EFI_PCI_PCIX_BUS_ERROR_DATA *)calloc(
128 1, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100129
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800131 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
132 struct json_object *obj = NULL;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100133
Lawrence Tange407b4c2022-07-21 13:54:01 +0100134 //Error status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800135 if (json_object_object_get_ex(section, "errorStatus", &obj)) {
136 ir_generic_error_status_to_cper(obj,
137 &section_cper->ErrorStatus);
138 add_to_valid_bitfield(&ui64Type, 0);
139 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100140
Lawrence Tange407b4c2022-07-21 13:54:01 +0100141 //Bus ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800142 if (json_object_object_get_ex(section, "busID", &obj)) {
143 json_object *bus_id = json_object_object_get(section, "busID");
144 UINT16 bus_number = (UINT8)json_object_get_int(
145 json_object_object_get(bus_id, "busNumber"));
146 UINT16 segment_number = (UINT8)json_object_get_int(
147 json_object_object_get(bus_id, "segmentNumber"));
148 section_cper->BusId = bus_number + (segment_number << 8);
149 add_to_valid_bitfield(&ui64Type, 2);
150 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100151
Lawrence Tange407b4c2022-07-21 13:54:01 +0100152 //Remaining fields.
153 UINT64 pcix_command = (UINT64)0x1 << 56;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800154
155 if (json_object_object_get_ex(section, "errorType", &obj)) {
156 section_cper->Type = (UINT16)readable_pair_to_integer(obj);
157 add_to_valid_bitfield(&ui64Type, 1);
158 }
159 if (json_object_object_get_ex(section, "busAddress", &obj)) {
160 section_cper->BusAddress = json_object_get_uint64(
161 json_object_object_get(section, "busAddress"));
162 add_to_valid_bitfield(&ui64Type, 3);
163 }
164 if (json_object_object_get_ex(section, "busData", &obj)) {
165 section_cper->BusData = json_object_get_uint64(obj);
166 add_to_valid_bitfield(&ui64Type, 4);
167 }
168 if (json_object_object_get_ex(section, "busCommandType", &obj)) {
169 const char *bus_command = json_object_get_string(obj);
170 section_cper->BusCommand =
171 strcmp(bus_command, "PCI") == 0 ? 0 : pcix_command;
172 add_to_valid_bitfield(&ui64Type, 5);
173 }
174 if (json_object_object_get_ex(section, "busRequestorID", &obj)) {
175 section_cper->RequestorId = json_object_get_uint64(obj);
176 add_to_valid_bitfield(&ui64Type, 6);
177 }
178 if (json_object_object_get_ex(section, "busCompleterID", &obj)) {
179 section_cper->ResponderId = json_object_get_uint64(obj);
180 add_to_valid_bitfield(&ui64Type, 7);
181 }
182 if (json_object_object_get_ex(section, "targetID", &obj)) {
183 section_cper->TargetId = json_object_get_uint64(obj);
184 add_to_valid_bitfield(&ui64Type, 8);
185 }
186 section_cper->ValidFields = ui64Type.value.ui64;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100187
Lawrence Tange407b4c2022-07-21 13:54:01 +0100188 //Write to stream, free resources.
189 fwrite(section_cper, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA), 1, out);
190 fflush(out);
191 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800192}