blob: 37bcb9fc9b781605bf1c5a40a9938daf6c5b395e [file] [log] [blame]
Brandon Kimfcbc3db2022-06-06 21:26:18 -07001#pragma once
2
3#include "pci_handler.hpp"
4
Brandon Kim17ee1a92022-06-07 21:04:08 -07005#include <boost/endian/arithmetic.hpp>
6
Brandon Kimfcbc3db2022-06-06 21:26:18 -07007#include <array>
8#include <cstdint>
9#include <memory>
Brandon Kim17ee1a92022-06-07 21:04:08 -070010#include <tuple>
Brandon Kimfcbc3db2022-06-06 21:26:18 -070011
12namespace bios_bmc_smm_error_logger
13{
14
Brandon Kim17ee1a92022-06-07 21:04:08 -070015/* Adding endianness */
16using boost::endian::little_uint16_t;
Brandon Kim271d2312022-09-02 16:34:55 +000017using boost::endian::little_uint24_t;
Brandon Kim17ee1a92022-06-07 21:04:08 -070018using boost::endian::little_uint32_t;
19using boost::endian::little_uint64_t;
20
Brandon Kim40ce08e2022-06-15 16:58:44 -070021// EntryPair.first = QueueEntryHeader
22// EntryPair.second = Error entry in vector of bytes
23using EntryPair = std::pair<struct QueueEntryHeader, std::vector<uint8_t>>;
24
Brandon Kimc49284b2022-06-17 09:55:26 -070025enum class BmcFlags : uint32_t
26{
27 ueSwitch = 1,
28 overflow = 1 << 1,
29 ready = 1 << 2,
30};
31
Brandon Kimfcbc3db2022-06-06 21:26:18 -070032struct CircularBufferHeader
33{
Brandon Kim17ee1a92022-06-07 21:04:08 -070034 little_uint32_t bmcInterfaceVersion; // Offset 0x0
35 little_uint32_t biosInterfaceVersion; // Offset 0x4
36 std::array<little_uint32_t, 4> magicNumber; // Offset 0x8
Brandon Kim271d2312022-09-02 16:34:55 +000037 little_uint24_t queueSize; // Offset 0x18
38 little_uint16_t ueRegionSize; // Offset 0x1b
39 little_uint32_t bmcFlags; // Offset 0x1d
40 little_uint24_t bmcReadPtr; // Offset 0x21
41 std::array<uint8_t, 4> reserved1; // Offset 0x24
Brandon Kim17ee1a92022-06-07 21:04:08 -070042 little_uint32_t biosFlags; // Offset 0x28
Brandon Kim271d2312022-09-02 16:34:55 +000043 little_uint24_t biosWritePtr; // Offset 0x2c
44 uint8_t reserved2; // Offset 0x2f
Brandon Kim17ee1a92022-06-07 21:04:08 -070045 // UE reserved region: Offset 0x30
46 // Error log queue: Offset 0x30 + UE reserved region
Brandon Kimfcbc3db2022-06-06 21:26:18 -070047
48 bool operator==(const CircularBufferHeader& other) const
49 {
Brandon Kim17ee1a92022-06-07 21:04:08 -070050 /* Skip comparing reserved1 and reserved2 */
51 return std::tie(this->bmcInterfaceVersion, this->biosInterfaceVersion,
52 this->magicNumber, this->queueSize, this->ueRegionSize,
53 this->bmcFlags, this->bmcReadPtr, this->biosFlags,
54 this->biosWritePtr) ==
55 std::tie(other.bmcInterfaceVersion, other.biosInterfaceVersion,
56 other.magicNumber, other.queueSize, other.ueRegionSize,
57 other.bmcFlags, other.bmcReadPtr, other.biosFlags,
58 other.biosWritePtr);
Brandon Kimfcbc3db2022-06-06 21:26:18 -070059 }
Brandon Kim17ee1a92022-06-07 21:04:08 -070060};
61static_assert(sizeof(CircularBufferHeader) == 0x30,
62 "Size of CircularBufferHeader struct is incorrect.");
Brandon Kimfcbc3db2022-06-06 21:26:18 -070063
Brandon Kim7bac2d62022-06-07 21:37:51 -070064struct QueueEntryHeader
65{
66 little_uint16_t sequenceId; // Offset 0x0
67 little_uint16_t entrySize; // Offset 0x2
68 uint8_t checksum; // Offset 0x4
69 uint8_t rdeCommandType; // Offset 0x5
70 // RDE Command Offset 0x6
71 bool operator==(const QueueEntryHeader& other) const
72 {
73 return std::tie(this->sequenceId, this->entrySize, this->checksum,
74 this->rdeCommandType) ==
75 std::tie(other.sequenceId, other.entrySize, other.checksum,
76 other.rdeCommandType);
77 }
78};
79static_assert(sizeof(QueueEntryHeader) == 0x6,
80 "Size of QueueEntryHeader struct is incorrect.");
81
Brandon Kimfcbc3db2022-06-06 21:26:18 -070082/**
83 * An interface class for the buffer helper APIs
84 */
85class BufferInterface
86{
87 public:
88 virtual ~BufferInterface() = default;
89
90 /**
91 * Zero out the buffer first before populating the header
92 *
93 * @param[in] bmcInterfaceVersion - Used to initialize the header
94 * @param[in] queueSize - Used to initialize the header
95 * @param[in] ueRegionSize - Used to initialize the header
96 * @param[in] magicNumber - Used to initialize the header
97 * @return true if successful
98 */
99 virtual void initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize,
100 uint16_t ueRegionSize,
101 const std::array<uint32_t, 4>& magicNumber) = 0;
Brandon Kim17ee1a92022-06-07 21:04:08 -0700102
103 /**
104 * Read the buffer header from shared buffer
105 */
106 virtual void readBufferHeader() = 0;
107
108 /**
109 * Getter API for the cached buffer header
110 * @return cached CircularBufferHeader
111 */
112 virtual struct CircularBufferHeader getCachedBufferHeader() const = 0;
Brandon Kimcf0b9752022-06-15 10:32:21 -0700113
114 /**
115 * Write to the bufferHeader and update the read pointer
Brandon Kim35d43352022-06-16 11:13:36 -0700116 * @param[in] newReadPtr - read pointer to update to
Brandon Kimcf0b9752022-06-15 10:32:21 -0700117 */
118 virtual void updateReadPtr(const uint32_t newReadPtr) = 0;
Brandon Kim9836cfa2022-06-15 11:21:11 -0700119
120 /**
Brandon Kimc49284b2022-06-17 09:55:26 -0700121 * Write to the bufferHeader and update the BMC flags
122 * @param[in] newBmcFlags - new flag to update to
123 */
124 virtual void updateBmcFlags(const uint32_t newBmcFlags) = 0;
125
126 /**
Brandon Kim9836cfa2022-06-15 11:21:11 -0700127 * Wrapper for the dataInterface->read, performs wraparound read
128 *
Brandon Kim35d43352022-06-16 11:13:36 -0700129 * @param[in] relativeOffset - offset relative the "Error Log
130 * Queue region" = (sizeof(CircularBufferHeader) + UE reserved region)
Brandon Kim9836cfa2022-06-15 11:21:11 -0700131 * @param[in] length - bytes to read
Brandon Kim9836cfa2022-06-15 11:21:11 -0700132 * @return the bytes read
133 */
Brandon Kim35d43352022-06-16 11:13:36 -0700134 virtual std::vector<uint8_t> wraparoundRead(const uint32_t relativeOffset,
135 const uint32_t length) = 0;
Brandon Kim7bac2d62022-06-07 21:37:51 -0700136 /**
Brandon Kim613ba532022-09-12 20:55:21 +0000137 * Read the entry header from shared buffer from the read pointer
Brandon Kim7bac2d62022-06-07 21:37:51 -0700138 *
Brandon Kim7bac2d62022-06-07 21:37:51 -0700139 * @return the entry header
140 */
Brandon Kim613ba532022-09-12 20:55:21 +0000141 virtual struct QueueEntryHeader readEntryHeader() = 0;
Brandon Kim40ce08e2022-06-15 16:58:44 -0700142
143 /**
Brandon Kim613ba532022-09-12 20:55:21 +0000144 * Read the queue entry from the error log queue from the read pointer
Brandon Kim40ce08e2022-06-15 16:58:44 -0700145 *
Brandon Kim35d43352022-06-16 11:13:36 -0700146 * * @return entry header and entry pair read from buffer
Brandon Kim40ce08e2022-06-15 16:58:44 -0700147 */
Brandon Kim613ba532022-09-12 20:55:21 +0000148 virtual EntryPair readEntry() = 0;
Brandon Kim4662b1b2022-06-16 21:38:02 -0700149
150 /**
151 * Read the buffer - this API should be used instead of individual functions
152 * above
153 *
154 * @return vector of EntryPair which consists of entry header and entry
155 */
156 virtual std::vector<EntryPair> readErrorLogs() = 0;
Brandon Kim3def3c82022-09-13 05:29:20 +0000157
158 /**
159 * Get max offset for the queue
160 *
161 * * @return Queue size - UE region size - Queue header size
162 */
163 virtual size_t getMaxOffset() = 0;
Brandon Kim82ab8322023-08-17 00:50:18 +0000164
165 /** @brief The Error log queue starts after the UE region, which is where
166 * the read and write pointers are offset from relatively
167 * @return relative offset for read and write pointers
168 */
169 virtual size_t getQueueOffset() = 0;
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700170};
171
172/**
173 * Buffer implementation class
174 */
175class BufferImpl : public BufferInterface
176{
177 public:
178 /** @brief Constructor for BufferImpl
179 * @param[in] dataInterface - DataInterface for this object
180 */
181 explicit BufferImpl(std::unique_ptr<DataInterface> dataInterface);
182 void initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize,
183 uint16_t ueRegionSize,
184 const std::array<uint32_t, 4>& magicNumber) override;
Brandon Kim17ee1a92022-06-07 21:04:08 -0700185 void readBufferHeader() override;
186 struct CircularBufferHeader getCachedBufferHeader() const override;
Brandon Kimcf0b9752022-06-15 10:32:21 -0700187 void updateReadPtr(const uint32_t newReadPtr) override;
Brandon Kimc49284b2022-06-17 09:55:26 -0700188 void updateBmcFlags(const uint32_t newBmcFlag) override;
Brandon Kim35d43352022-06-16 11:13:36 -0700189 std::vector<uint8_t> wraparoundRead(const uint32_t relativeOffset,
190 const uint32_t length) override;
Brandon Kim613ba532022-09-12 20:55:21 +0000191 struct QueueEntryHeader readEntryHeader() override;
192 EntryPair readEntry() override;
Brandon Kim4662b1b2022-06-16 21:38:02 -0700193 std::vector<EntryPair> readErrorLogs() override;
Brandon Kim3def3c82022-09-13 05:29:20 +0000194 size_t getMaxOffset() override;
Brandon Kim82ab8322023-08-17 00:50:18 +0000195 size_t getQueueOffset() override;
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700196
197 private:
Brandon Kimf0e4adc2022-06-16 23:14:25 -0700198 /** @brief Calculate the checksum by XOR each bytes in the span
199 * @param[in] entry - Span to calculate the checksum on
200 * @return calculated checksum
201 */
202 uint8_t calculateChecksum(std::span<uint8_t> entry);
Brandon Kim9836cfa2022-06-15 11:21:11 -0700203
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700204 std::unique_ptr<DataInterface> dataInterface;
Brandon Kim17ee1a92022-06-07 21:04:08 -0700205 struct CircularBufferHeader cachedBufferHeader = {};
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700206};
207
208} // namespace bios_bmc_smm_error_logger