Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting PCI/PCI-X device 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 | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame] | 8 | #include <json.h> |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 9 | #include "../edk/Cper.h" |
| 10 | #include "../cper-utils.h" |
| 11 | #include "cper-section-pci-dev.h" |
| 12 | |
| 13 | //Converts a single PCI/PCI-X device CPER section into JSON IR. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 14 | json_object * |
| 15 | cper_section_pci_dev_to_ir(void *section, |
| 16 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor) |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 17 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 18 | EFI_PCI_PCIX_DEVICE_ERROR_DATA *dev_error = |
| 19 | (EFI_PCI_PCIX_DEVICE_ERROR_DATA *)section; |
| 20 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 21 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 22 | //Validation bits. |
| 23 | json_object *validation = bitfield_to_ir( |
| 24 | dev_error->ValidFields, 5, PCI_DEV_ERROR_VALID_BITFIELD_NAMES); |
| 25 | json_object_object_add(section_ir, "validationBits", validation); |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 26 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 27 | //Error status. |
| 28 | json_object *error_status = |
| 29 | cper_generic_error_status_to_ir(&dev_error->ErrorStatus); |
| 30 | json_object_object_add(section_ir, "errorStatus", error_status); |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 31 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 32 | //ID information. |
| 33 | json_object *id_info = json_object_new_object(); |
| 34 | json_object_object_add( |
| 35 | id_info, "vendorID", |
| 36 | json_object_new_uint64(dev_error->IdInfo.VendorId)); |
| 37 | json_object_object_add( |
| 38 | id_info, "deviceID", |
| 39 | json_object_new_uint64(dev_error->IdInfo.DeviceId)); |
| 40 | json_object_object_add( |
| 41 | id_info, "classCode", |
| 42 | json_object_new_uint64(dev_error->IdInfo.ClassCode)); |
| 43 | json_object_object_add( |
| 44 | id_info, "functionNumber", |
| 45 | json_object_new_uint64(dev_error->IdInfo.FunctionNumber)); |
| 46 | json_object_object_add( |
| 47 | id_info, "deviceNumber", |
| 48 | json_object_new_uint64(dev_error->IdInfo.DeviceNumber)); |
| 49 | json_object_object_add( |
| 50 | id_info, "busNumber", |
| 51 | json_object_new_uint64(dev_error->IdInfo.BusNumber)); |
| 52 | json_object_object_add( |
| 53 | id_info, "segmentNumber", |
| 54 | json_object_new_uint64(dev_error->IdInfo.SegmentNumber)); |
| 55 | json_object_object_add(section_ir, "idInfo", id_info); |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 56 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 57 | //Number of following register data pairs. |
| 58 | json_object_object_add(section_ir, "memoryNumber", |
| 59 | json_object_new_uint64(dev_error->MemoryNumber)); |
| 60 | json_object_object_add(section_ir, "ioNumber", |
| 61 | json_object_new_uint64(dev_error->IoNumber)); |
| 62 | int num_data_pairs = dev_error->MemoryNumber + dev_error->IoNumber; |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 63 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 64 | //Register pairs, described by the numeric fields. |
| 65 | //The actual "pairs" of address and data aren't necessarily 8 bytes long, so can't assume the contents. |
| 66 | //Hence the naming "firstHalf" and "secondHalf" rather than "address" and "data". |
| 67 | json_object *register_data_pair_array = json_object_new_array(); |
| 68 | UINT64 *cur_pos = (UINT64 *)(dev_error + 1); |
| 69 | for (int i = 0; i < num_data_pairs; i++) { |
| 70 | //Save current pair to array. |
| 71 | json_object *register_data_pair = json_object_new_object(); |
| 72 | json_object_object_add(register_data_pair, "firstHalf", |
| 73 | json_object_new_uint64(*cur_pos)); |
| 74 | json_object_object_add(register_data_pair, "secondHalf", |
| 75 | json_object_new_uint64(*(cur_pos + 1))); |
| 76 | json_object_array_add(register_data_pair_array, |
| 77 | register_data_pair); |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 78 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 79 | //Move to next pair. |
| 80 | cur_pos += 2; |
| 81 | } |
| 82 | json_object_object_add(section_ir, "registerDataPairs", |
| 83 | register_data_pair_array); |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 84 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 85 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 88 | void ir_section_pci_dev_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 89 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 90 | EFI_PCI_PCIX_DEVICE_ERROR_DATA *section_cper = |
| 91 | (EFI_PCI_PCIX_DEVICE_ERROR_DATA *)calloc( |
| 92 | 1, sizeof(EFI_PCI_PCIX_DEVICE_ERROR_DATA)); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 93 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 94 | //Validation bits. |
| 95 | section_cper->ValidFields = ir_to_bitfield( |
| 96 | json_object_object_get(section, "validationBits"), 5, |
| 97 | PCI_DEV_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 98 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 99 | //Error status. |
| 100 | ir_generic_error_status_to_cper(json_object_object_get(section, |
| 101 | "errorStatus"), |
| 102 | §ion_cper->ErrorStatus); |
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 | //Device ID information. |
| 105 | json_object *id_info = json_object_object_get(section, "idInfo"); |
| 106 | section_cper->IdInfo.VendorId = json_object_get_uint64( |
| 107 | json_object_object_get(id_info, "vendorID")); |
| 108 | section_cper->IdInfo.DeviceId = json_object_get_uint64( |
| 109 | json_object_object_get(id_info, "deviceID")); |
| 110 | section_cper->IdInfo.ClassCode = json_object_get_uint64( |
| 111 | json_object_object_get(id_info, "classCode")); |
| 112 | section_cper->IdInfo.FunctionNumber = json_object_get_uint64( |
| 113 | json_object_object_get(id_info, "functionNumber")); |
| 114 | section_cper->IdInfo.DeviceNumber = json_object_get_uint64( |
| 115 | json_object_object_get(id_info, "deviceNumber")); |
| 116 | section_cper->IdInfo.BusNumber = json_object_get_uint64( |
| 117 | json_object_object_get(id_info, "busNumber")); |
| 118 | section_cper->IdInfo.SegmentNumber = json_object_get_uint64( |
| 119 | json_object_object_get(id_info, "segmentNumber")); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 120 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 121 | //Amount of following data pairs. |
| 122 | section_cper->MemoryNumber = (UINT32)json_object_get_uint64( |
| 123 | json_object_object_get(section, "memoryNumber")); |
| 124 | section_cper->IoNumber = (UINT32)json_object_get_uint64( |
| 125 | json_object_object_get(section, "ioNumber")); |
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 | //Write header out to stream, free it. |
| 128 | fwrite(section_cper, sizeof(EFI_PCI_PCIX_DEVICE_ERROR_DATA), 1, out); |
| 129 | fflush(out); |
| 130 | free(section_cper); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 131 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 132 | //Begin writing register pairs. |
| 133 | json_object *register_pairs = |
| 134 | json_object_object_get(section, "registerDataPairs"); |
| 135 | int num_pairs = json_object_array_length(register_pairs); |
| 136 | for (int i = 0; i < num_pairs; i++) { |
| 137 | //Get the pair array item out. |
| 138 | json_object *register_pair = |
| 139 | json_object_array_get_idx(register_pairs, i); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 140 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 141 | //Create the pair array. |
| 142 | UINT64 pair[2]; |
| 143 | pair[0] = json_object_get_uint64( |
| 144 | json_object_object_get(register_pair, "firstHalf")); |
| 145 | pair[1] = json_object_get_uint64( |
| 146 | json_object_object_get(register_pair, "secondHalf")); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 147 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 148 | //Push to stream. |
| 149 | fwrite(pair, sizeof(UINT64), 2, out); |
| 150 | fflush(out); |
| 151 | } |
Lawrence Tang | a416ec9 | 2022-07-06 14:34:40 +0100 | [diff] [blame] | 152 | } |