blob: bdc6beb1f2d41238f5e8eceae3c65de42070e0f7 [file] [log] [blame]
Lawrence Tang214a1542022-07-06 14:11:28 +01001/**
2 * Describes functions for converting PCI/PCI-X bus CPER sections from binary and JSON format
3 * into an intermediate format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang214a1542022-07-06 14:11:28 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang0a4b3f22022-07-21 10:40:10 +01008#include <string.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
Lawrence Tang214a1542022-07-06 14:11:28 +010010#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.
John Chungf8fc7052024-05-03 20:05:29 +080015json_object *cper_section_pci_bus_to_ir(void *section)
Lawrence Tang214a1542022-07-06 14:11:28 +010016{
Lawrence Tange407b4c2022-07-21 13:54:01 +010017 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 Tang214a1542022-07-06 14:11:28 +010020
Lawrence Tange407b4c2022-07-21 13:54:01 +010021 //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 Tang214a1542022-07-06 14:11:28 +010025
Lawrence Tange407b4c2022-07-21 13:54:01 +010026 //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 Tang214a1542022-07-06 14:11:28 +010030
Lawrence Tange407b4c2022-07-21 13:54:01 +010031 //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 Tang214a1542022-07-06 14:11:28 +010036
Lawrence Tange407b4c2022-07-21 13:54:01 +010037 //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 Tang214a1542022-07-06 14:11:28 +010044
Lawrence Tange407b4c2022-07-21 13:54:01 +010045 //Miscellaneous numeric fields.
46 UINT8 command_type = (bus_error->BusCommand >> 56) &
John Chungf8fc7052024-05-03 20:05:29 +080047 0x1; //Byte 7, bit 0.
Lawrence Tange407b4c2022-07-21 13:54:01 +010048 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"));
55 json_object_object_add(section_ir, "busRequestorID",
56 json_object_new_uint64(bus_error->RequestorId));
57 json_object_object_add(section_ir, "busCompleterID",
58 json_object_new_uint64(bus_error->ResponderId));
59 json_object_object_add(section_ir, "targetID",
60 json_object_new_uint64(bus_error->TargetId));
Lawrence Tang214a1542022-07-06 14:11:28 +010061
Lawrence Tange407b4c2022-07-21 13:54:01 +010062 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010063}
64
65//Converts a single provided PCI/PCI-X bus CPER-JSON section into CPER binary, outputting to the
66//provided stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010067void ir_section_pci_bus_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010068{
Lawrence Tange407b4c2022-07-21 13:54:01 +010069 EFI_PCI_PCIX_BUS_ERROR_DATA *section_cper =
70 (EFI_PCI_PCIX_BUS_ERROR_DATA *)calloc(
71 1, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010072
Lawrence Tange407b4c2022-07-21 13:54:01 +010073 //Validation bits.
74 section_cper->ValidFields = ir_to_bitfield(
75 json_object_object_get(section, "validationBits"), 9,
76 PCI_BUS_ERROR_VALID_BITFIELD_NAMES);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010077
Lawrence Tange407b4c2022-07-21 13:54:01 +010078 //Error status.
79 ir_generic_error_status_to_cper(json_object_object_get(section,
80 "errorStatus"),
81 &section_cper->ErrorStatus);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010082
Lawrence Tange407b4c2022-07-21 13:54:01 +010083 //Bus ID.
84 json_object *bus_id = json_object_object_get(section, "busID");
85 UINT16 bus_number = (UINT8)json_object_get_int(
86 json_object_object_get(bus_id, "busNumber"));
87 UINT16 segment_number = (UINT8)json_object_get_int(
88 json_object_object_get(bus_id, "segmentNumber"));
89 section_cper->BusId = bus_number + (segment_number << 8);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010090
Lawrence Tange407b4c2022-07-21 13:54:01 +010091 //Remaining fields.
92 UINT64 pcix_command = (UINT64)0x1 << 56;
93 const char *bus_command = json_object_get_string(
94 json_object_object_get(section, "busCommandType"));
95 section_cper->Type = (UINT16)readable_pair_to_integer(
96 json_object_object_get(section, "errorType"));
97 section_cper->BusAddress = json_object_get_uint64(
98 json_object_object_get(section, "busAddress"));
99 section_cper->BusData = json_object_get_uint64(
100 json_object_object_get(section, "busData"));
101 section_cper->BusCommand =
102 strcmp(bus_command, "PCI") == 0 ? 0 : pcix_command;
103 section_cper->RequestorId = json_object_get_uint64(
104 json_object_object_get(section, "busRequestorID"));
105 section_cper->ResponderId = json_object_get_uint64(
106 json_object_object_get(section, "busCompleterID"));
107 section_cper->TargetId = json_object_get_uint64(
108 json_object_object_get(section, "targetID"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100109
Lawrence Tange407b4c2022-07-21 13:54:01 +0100110 //Write to stream, free resources.
111 fwrite(section_cper, sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA), 1, out);
112 fflush(out);
113 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800114}