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. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 15 | json_object *cper_section_pci_bus_to_ir(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. |
| 22 | json_object *validation = bitfield_to_ir( |
| 23 | bus_error->ValidFields, 9, PCI_BUS_ERROR_VALID_BITFIELD_NAMES); |
| 24 | json_object_object_add(section_ir, "validationBits", validation); |
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 | //Error status. |
| 27 | json_object *error_status = |
| 28 | cper_generic_error_status_to_ir(&bus_error->ErrorStatus); |
| 29 | json_object_object_add(section_ir, "errorStatus", error_status); |
Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 30 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 31 | //PCI bus error type. |
| 32 | json_object *error_type = integer_to_readable_pair( |
| 33 | bus_error->Type, 8, PCI_BUS_ERROR_TYPES_KEYS, |
| 34 | PCI_BUS_ERROR_TYPES_VALUES, "Unknown (Reserved)"); |
| 35 | json_object_object_add(section_ir, "errorType", error_type); |
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 | //Bus ID. |
| 38 | json_object *bus_id = json_object_new_object(); |
| 39 | json_object_object_add(bus_id, "busNumber", |
| 40 | json_object_new_int(bus_error->BusId & 0xFF)); |
| 41 | json_object_object_add(bus_id, "segmentNumber", |
| 42 | json_object_new_int(bus_error->BusId >> 8)); |
| 43 | json_object_object_add(section_ir, "busID", bus_id); |
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 | //Miscellaneous numeric fields. |
| 46 | UINT8 command_type = (bus_error->BusCommand >> 56) & |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 47 | 0x1; //Byte 7, bit 0. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 48 | json_object_object_add(section_ir, "busAddress", |
| 49 | json_object_new_uint64(bus_error->BusAddress)); |
| 50 | json_object_object_add(section_ir, "busData", |
| 51 | json_object_new_uint64(bus_error->BusData)); |
| 52 | json_object_object_add( |
| 53 | section_ir, "busCommandType", |
| 54 | json_object_new_string(command_type == 0 ? "PCI" : "PCI-X")); |
Aushim Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 55 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 56 | json_object_object_add(section_ir, "busRequestorID", |
| 57 | json_object_new_uint64(bus_error->RequestorId)); |
Aushim Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 58 | |
| 59 | char hexstring_buf[EFI_UINT64_HEX_STRING_LEN]; |
| 60 | snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX", |
| 61 | bus_error->RequestorId); |
| 62 | json_object_object_add(section_ir, "busRequestorIDHex", |
| 63 | json_object_new_string(hexstring_buf)); |
| 64 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 65 | json_object_object_add(section_ir, "busCompleterID", |
| 66 | json_object_new_uint64(bus_error->ResponderId)); |
Aushim Nagarkatti | cc36701 | 2024-12-05 18:17:27 -0800 | [diff] [blame] | 67 | snprintf(hexstring_buf, EFI_UINT64_HEX_STRING_LEN, "0x%016llX", |
| 68 | bus_error->ResponderId); |
| 69 | json_object_object_add(section_ir, "busCompleterIDHex", |
| 70 | json_object_new_string(hexstring_buf)); |
| 71 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 72 | json_object_object_add(section_ir, "targetID", |
| 73 | json_object_new_uint64(bus_error->TargetId)); |
Lawrence Tang | 214a154 | 2022-07-06 14:11:28 +0100 | [diff] [blame] | 74 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 75 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | //Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the |
| 79 | //provided stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 80 | void ir_section_pci_bus_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 81 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 82 | EFI_PCI_PCIX_BUS_ERROR_DATA *section_cper = |
| 83 | (EFI_PCI_PCIX_BUS_ERROR_DATA *)calloc( |
| 84 | 1, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA)); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 85 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 86 | //Validation bits. |
| 87 | section_cper->ValidFields = ir_to_bitfield( |
| 88 | json_object_object_get(section, "validationBits"), 9, |
| 89 | PCI_BUS_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 90 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 91 | //Error status. |
| 92 | ir_generic_error_status_to_cper(json_object_object_get(section, |
| 93 | "errorStatus"), |
| 94 | §ion_cper->ErrorStatus); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 95 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 96 | //Bus ID. |
| 97 | json_object *bus_id = json_object_object_get(section, "busID"); |
| 98 | UINT16 bus_number = (UINT8)json_object_get_int( |
| 99 | json_object_object_get(bus_id, "busNumber")); |
| 100 | UINT16 segment_number = (UINT8)json_object_get_int( |
| 101 | json_object_object_get(bus_id, "segmentNumber")); |
| 102 | section_cper->BusId = bus_number + (segment_number << 8); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 103 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 104 | //Remaining fields. |
| 105 | UINT64 pcix_command = (UINT64)0x1 << 56; |
| 106 | const char *bus_command = json_object_get_string( |
| 107 | json_object_object_get(section, "busCommandType")); |
| 108 | section_cper->Type = (UINT16)readable_pair_to_integer( |
| 109 | json_object_object_get(section, "errorType")); |
| 110 | section_cper->BusAddress = json_object_get_uint64( |
| 111 | json_object_object_get(section, "busAddress")); |
| 112 | section_cper->BusData = json_object_get_uint64( |
| 113 | json_object_object_get(section, "busData")); |
| 114 | section_cper->BusCommand = |
| 115 | strcmp(bus_command, "PCI") == 0 ? 0 : pcix_command; |
| 116 | section_cper->RequestorId = json_object_get_uint64( |
| 117 | json_object_object_get(section, "busRequestorID")); |
| 118 | section_cper->ResponderId = json_object_get_uint64( |
| 119 | json_object_object_get(section, "busCompleterID")); |
| 120 | section_cper->TargetId = json_object_get_uint64( |
| 121 | json_object_object_get(section, "targetID")); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 122 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 123 | //Write to stream, free resources. |
| 124 | fwrite(section_cper, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA), 1, out); |
| 125 | fflush(out); |
| 126 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 127 | } |