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> |
Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 13 | |
| 14 | //Converts a single PCI/PCI-X bus CPER section into JSON IR. |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 15 | 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] | 16 | { |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 17 | if (size < sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA)) { |
| 18 | return NULL; |
| 19 | } |
| 20 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 21 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 24 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 25 | //Validation bits. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 26 | ValidationTypes ui64Type = { UINT_64T, |
| 27 | .value.ui64 = bus_error->ValidFields }; |
Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 28 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 29 | //Error status. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 30 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 35 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 36 | //PCI bus error type. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 37 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 43 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 44 | //Bus ID. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 45 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 55 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 56 | //Miscellaneous numeric fields. |
Ed Tanous | 5e2164a | 2025-03-09 09:20:44 -0700 | [diff] [blame] | 57 | //Byte 7, bit 0. |
| 58 | UINT8 command_type = (bus_error->BusCommand >> 56) & 0x1; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 59 | 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 Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 75 | char hexstring_buf[EFI_UINT64_HEX_STRING_LEN]; |
Aushim Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 76 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 77 | 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 Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 81 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 82 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 103 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 104 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | //Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the |
| 108 | //provided stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 109 | void ir_section_pci_bus_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 110 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 111 | 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 Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 114 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 115 | //Validation bits. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 116 | ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 }; |
| 117 | struct json_object *obj = NULL; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 118 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 119 | //Error status. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 120 | if (json_object_object_get_ex(section, "errorStatus", &obj)) { |
| 121 | ir_generic_error_status_to_cper(obj, |
| 122 | §ion_cper->ErrorStatus); |
| 123 | add_to_valid_bitfield(&ui64Type, 0); |
| 124 | } |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 125 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 126 | //Bus ID. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 127 | 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 Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 136 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 137 | //Remaining fields. |
| 138 | UINT64 pcix_command = (UINT64)0x1 << 56; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 139 | |
| 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 Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 172 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 173 | //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 Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 177 | } |