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