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 | 5aedbb2 | 2025-03-05 18:02:34 -0800 | [diff] [blame^] | 15 | json_object *cper_section_pci_bus_to_ir(const void *section) |
Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 16 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 17 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 20 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 21 | //Validation bits. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 22 | ValidationTypes ui64Type = { UINT_64T, |
| 23 | .value.ui64 = bus_error->ValidFields }; |
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 | //Error status. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 26 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 31 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 32 | //PCI bus error type. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 33 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 39 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 40 | //Bus ID. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 41 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 51 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 52 | //Miscellaneous numeric fields. |
| 53 | UINT8 command_type = (bus_error->BusCommand >> 56) & |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 54 | 0x1; //Byte 7, bit 0. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 55 | 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 Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 71 | char hexstring_buf[EFI_UINT64_HEX_STRING_LEN]; |
Aushim Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 72 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 73 | 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 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 | 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 Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 99 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 100 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | //Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the |
| 104 | //provided stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 105 | void ir_section_pci_bus_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 106 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 107 | 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 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 | //Validation bits. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 112 | ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 }; |
| 113 | struct json_object *obj = NULL; |
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 | //Error status. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 116 | if (json_object_object_get_ex(section, "errorStatus", &obj)) { |
| 117 | ir_generic_error_status_to_cper(obj, |
| 118 | §ion_cper->ErrorStatus); |
| 119 | add_to_valid_bitfield(&ui64Type, 0); |
| 120 | } |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 121 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 122 | //Bus ID. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 123 | 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 Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 132 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 133 | //Remaining fields. |
| 134 | UINT64 pcix_command = (UINT64)0x1 << 56; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 135 | |
| 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 Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 168 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 169 | //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 Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 173 | } |