Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting PCI/PCI-X bus CPER sections from binary and JSON format |
| 3 | * into an intermediate format. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 4 | * |
Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | #include <stdio.h> |
Lawrence Tang | 0a4b3f2 | 2022-07-21 10:40:10 +0100 | [diff] [blame] | 8 | #include <string.h> |
Lawrence Tang | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame] | 9 | #include <json.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 10 | #include <libcper/Cper.h> |
| 11 | #include <libcper/cper-utils.h> |
| 12 | #include <libcper/sections/cper-section-pci-bus.h> |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 13 | #include <libcper/log.h> |
Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 14 | |
| 15 | //Converts a single PCI/PCI-X bus CPER section into JSON IR. |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 16 | json_object *cper_section_pci_bus_to_ir(const UINT8 *section, UINT32 size) |
Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 17 | { |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 18 | if (size < sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA)) { |
| 19 | return NULL; |
| 20 | } |
| 21 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 22 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 25 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 26 | //Validation bits. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 27 | ValidationTypes ui64Type = { UINT_64T, |
| 28 | .value.ui64 = bus_error->ValidFields }; |
Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 29 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 30 | //Error status. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 31 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 36 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 37 | //PCI bus error type. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 38 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 44 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 45 | //Bus ID. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 46 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 56 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 57 | //Miscellaneous numeric fields. |
Ed Tanous | 5e2164a | 2025-03-09 09:20:44 -0700 | [diff] [blame] | 58 | //Byte 7, bit 0. |
| 59 | UINT8 command_type = (bus_error->BusCommand >> 56) & 0x1; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 60 | 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 Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 76 | char hexstring_buf[EFI_UINT64_HEX_STRING_LEN]; |
Aushim Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 77 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 78 | 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 Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 82 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 83 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 104 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 105 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | //Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the |
| 109 | //provided stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 110 | void ir_section_pci_bus_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 111 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 112 | 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 Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 115 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 116 | //Validation bits. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 117 | ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 }; |
| 118 | struct json_object *obj = NULL; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 119 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 120 | //Error status. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 121 | if (json_object_object_get_ex(section, "errorStatus", &obj)) { |
| 122 | ir_generic_error_status_to_cper(obj, |
| 123 | §ion_cper->ErrorStatus); |
| 124 | add_to_valid_bitfield(&ui64Type, 0); |
| 125 | } |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 126 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 127 | //Bus ID. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 128 | 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 Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 137 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 138 | //Remaining fields. |
| 139 | UINT64 pcix_command = (UINT64)0x1 << 56; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 140 | |
| 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 Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 173 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 174 | //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 Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 178 | } |