blob: 2884f61d2482cc1328684e26b56ca4bd9d277089 [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.
John Chungf8fc7052024-05-03 20:05:29 +080015json_object *cper_section_pci_bus_to_ir(void *section)
Lawrence Tang214a1542022-07-06 14:11:28 +010016{
Lawrence Tange407b4c2022-07-21 13:54:01 +010017 EFI_PCI_PCIX_BUS_ERROR_DATA *bus_error =
18 (EFI_PCI_PCIX_BUS_ERROR_DATA *)section;
19 json_object *section_ir = json_object_new_object();
Lawrence Tang214a1542022-07-06 14:11:28 +010020
Lawrence Tange407b4c2022-07-21 13:54:01 +010021 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080022 ValidationTypes ui64Type = { UINT_64T,
23 .value.ui64 = bus_error->ValidFields };
Lawrence Tang214a1542022-07-06 14:11:28 +010024
Lawrence Tange407b4c2022-07-21 13:54:01 +010025 //Error status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080026 if (isvalid_prop_to_ir(&ui64Type, 0)) {
27 json_object *error_status = cper_generic_error_status_to_ir(
28 &bus_error->ErrorStatus);
29 json_object_object_add(section_ir, "errorStatus", error_status);
30 }
Lawrence Tang214a1542022-07-06 14:11:28 +010031
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 //PCI bus error type.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080033 if (isvalid_prop_to_ir(&ui64Type, 1)) {
34 json_object *error_type = integer_to_readable_pair(
35 bus_error->Type, 8, PCI_BUS_ERROR_TYPES_KEYS,
36 PCI_BUS_ERROR_TYPES_VALUES, "Unknown (Reserved)");
37 json_object_object_add(section_ir, "errorType", error_type);
38 }
Lawrence Tang214a1542022-07-06 14:11:28 +010039
Lawrence Tange407b4c2022-07-21 13:54:01 +010040 //Bus ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080041 if (isvalid_prop_to_ir(&ui64Type, 2)) {
42 json_object *bus_id = json_object_new_object();
43 json_object_object_add(bus_id, "busNumber",
44 json_object_new_int(bus_error->BusId &
45 0xFF));
46 json_object_object_add(bus_id, "segmentNumber",
47 json_object_new_int(bus_error->BusId >>
48 8));
49 json_object_object_add(section_ir, "busID", bus_id);
50 }
Lawrence Tang214a1542022-07-06 14:11:28 +010051
Lawrence Tange407b4c2022-07-21 13:54:01 +010052 //Miscellaneous numeric fields.
53 UINT8 command_type = (bus_error->BusCommand >> 56) &
John Chungf8fc7052024-05-03 20:05:29 +080054 0x1; //Byte 7, bit 0.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080055 if (isvalid_prop_to_ir(&ui64Type, 3)) {
56 json_object_object_add(
57 section_ir, "busAddress",
58 json_object_new_uint64(bus_error->BusAddress));
59 }
60 if (isvalid_prop_to_ir(&ui64Type, 4)) {
61 json_object_object_add(
62 section_ir, "busData",
63 json_object_new_uint64(bus_error->BusData));
64 }
65 if (isvalid_prop_to_ir(&ui64Type, 5)) {
66 json_object_object_add(
67 section_ir, "busCommandType",
68 json_object_new_string(command_type == 0 ? "PCI" :
69 "PCI-X"));
70 }
Aushim Nagarkatticc367012024-12-05 18:17:27 -080071 char hexstring_buf[EFI_UINT64_HEX_STRING_LEN];
Aushim Nagarkatticc367012024-12-05 18:17:27 -080072
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080073 if (isvalid_prop_to_ir(&ui64Type, 6)) {
74 json_object_object_add(
75 section_ir, "busRequestorID",
76 json_object_new_uint64(bus_error->RequestorId));
Aushim Nagarkatticc367012024-12-05 18:17:27 -080077
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080078 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX",
79 bus_error->RequestorId);
80 json_object_object_add(section_ir, "busRequestorIDHex",
81 json_object_new_string(hexstring_buf));
82 }
83
84 if (isvalid_prop_to_ir(&ui64Type, 7)) {
85 json_object_object_add(
86 section_ir, "busCompleterID",
87 json_object_new_uint64(bus_error->ResponderId));
88 snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX",
89 bus_error->ResponderId);
90 json_object_object_add(section_ir, "busCompleterIDHex",
91 json_object_new_string(hexstring_buf));
92 }
93
94 if (isvalid_prop_to_ir(&ui64Type, 8)) {
95 json_object_object_add(
96 section_ir, "targetID",
97 json_object_new_uint64(bus_error->TargetId));
98 }
Lawrence Tang214a1542022-07-06 14:11:28 +010099
Lawrence Tange407b4c2022-07-21 13:54:01 +0100100 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100101}
102
103//Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the
104//provided stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100105void ir_section_pci_bus_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100106{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100107 EFI_PCI_PCIX_BUS_ERROR_DATA *section_cper =
108 (EFI_PCI_PCIX_BUS_ERROR_DATA *)calloc(
109 1, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100110
Lawrence Tange407b4c2022-07-21 13:54:01 +0100111 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800112 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
113 struct json_object *obj = NULL;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100114
Lawrence Tange407b4c2022-07-21 13:54:01 +0100115 //Error status.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800116 if (json_object_object_get_ex(section, "errorStatus", &obj)) {
117 ir_generic_error_status_to_cper(obj,
118 &section_cper->ErrorStatus);
119 add_to_valid_bitfield(&ui64Type, 0);
120 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100121
Lawrence Tange407b4c2022-07-21 13:54:01 +0100122 //Bus ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800123 if (json_object_object_get_ex(section, "busID", &obj)) {
124 json_object *bus_id = json_object_object_get(section, "busID");
125 UINT16 bus_number = (UINT8)json_object_get_int(
126 json_object_object_get(bus_id, "busNumber"));
127 UINT16 segment_number = (UINT8)json_object_get_int(
128 json_object_object_get(bus_id, "segmentNumber"));
129 section_cper->BusId = bus_number + (segment_number << 8);
130 add_to_valid_bitfield(&ui64Type, 2);
131 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100132
Lawrence Tange407b4c2022-07-21 13:54:01 +0100133 //Remaining fields.
134 UINT64 pcix_command = (UINT64)0x1 << 56;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800135
136 if (json_object_object_get_ex(section, "errorType", &obj)) {
137 section_cper->Type = (UINT16)readable_pair_to_integer(obj);
138 add_to_valid_bitfield(&ui64Type, 1);
139 }
140 if (json_object_object_get_ex(section, "busAddress", &obj)) {
141 section_cper->BusAddress = json_object_get_uint64(
142 json_object_object_get(section, "busAddress"));
143 add_to_valid_bitfield(&ui64Type, 3);
144 }
145 if (json_object_object_get_ex(section, "busData", &obj)) {
146 section_cper->BusData = json_object_get_uint64(obj);
147 add_to_valid_bitfield(&ui64Type, 4);
148 }
149 if (json_object_object_get_ex(section, "busCommandType", &obj)) {
150 const char *bus_command = json_object_get_string(obj);
151 section_cper->BusCommand =
152 strcmp(bus_command, "PCI") == 0 ? 0 : pcix_command;
153 add_to_valid_bitfield(&ui64Type, 5);
154 }
155 if (json_object_object_get_ex(section, "busRequestorID", &obj)) {
156 section_cper->RequestorId = json_object_get_uint64(obj);
157 add_to_valid_bitfield(&ui64Type, 6);
158 }
159 if (json_object_object_get_ex(section, "busCompleterID", &obj)) {
160 section_cper->ResponderId = json_object_get_uint64(obj);
161 add_to_valid_bitfield(&ui64Type, 7);
162 }
163 if (json_object_object_get_ex(section, "targetID", &obj)) {
164 section_cper->TargetId = json_object_get_uint64(obj);
165 add_to_valid_bitfield(&ui64Type, 8);
166 }
167 section_cper->ValidFields = ui64Type.value.ui64;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100168
Lawrence Tange407b4c2022-07-21 13:54:01 +0100169 //Write to stream, free resources.
170 fwrite(section_cper, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA), 1, out);
171 fflush(out);
172 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800173}