Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 1 | /* Copyright 2018 Intel |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include "ipmbdefines.hpp" |
| 17 | |
Ed Tanous | 1486b8a | 2023-02-28 13:28:59 -0800 | [diff] [blame] | 18 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 09027c0 | 2020-08-31 17:28:36 -0700 | [diff] [blame] | 19 | #include <boost/asio/steady_timer.hpp> |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 20 | #include <boost/container/flat_set.hpp> |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 21 | #include <sdbusplus/asio/object_server.hpp> |
| 22 | #include <sdbusplus/message.hpp> |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 23 | |
| 24 | #include <optional> |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 27 | extern "C" |
| 28 | { |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 29 | #include <i2c/smbus.h> |
| 30 | #include <linux/i2c-dev.h> |
| 31 | } |
| 32 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 33 | #ifndef IPMBBRIDGED_HPP |
| 34 | #define IPMBBRIDGED_HPP |
| 35 | |
| 36 | /** |
| 37 | * @brief Ipmb return status codes (sendRequest API call) |
| 38 | */ |
| 39 | enum class ipmbResponseStatus |
| 40 | { |
| 41 | success = 0, |
| 42 | error = 1, |
| 43 | invalid_param = 2, |
| 44 | busy = 3, |
| 45 | timeout = 4, |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * @brief Ipmb outstanding requests defines |
| 50 | */ |
| 51 | constexpr int ipmbMaxOutstandingRequestsCount = 64; |
| 52 | constexpr int ipmbNumberOfTries = 6; |
| 53 | constexpr uint64_t ipmbRequestRetryTimeout = 250; // ms |
| 54 | |
| 55 | /** |
| 56 | * @brief Ipmb I2C communication |
| 57 | */ |
| 58 | constexpr uint8_t ipmbI2cNumberOfRetries = 2; |
| 59 | |
| 60 | /** |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 61 | * @brief Ipmb boardcast address |
| 62 | */ |
| 63 | constexpr uint8_t broadcastAddress = 0x0; |
| 64 | |
| 65 | /** |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 66 | * @brief Ipmb defines |
| 67 | */ |
| 68 | constexpr size_t ipmbMaxDataSize = 256; |
| 69 | constexpr size_t ipmbConnectionHeaderLength = 3; |
| 70 | constexpr size_t ipmbResponseDataHeaderLength = 4; |
| 71 | constexpr size_t ipmbRequestDataHeaderLength = 3; |
| 72 | constexpr size_t ipmbAddressSize = 1; |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 73 | constexpr size_t ipmbPktLenSize = 1; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 74 | constexpr size_t ipmbChecksumSize = 1; |
| 75 | constexpr size_t ipmbChecksum2StartOffset = 3; |
Patrick Williams | 1f5b24b | 2023-03-30 15:36:03 -0500 | [diff] [blame] | 76 | constexpr ssize_t ipmbMinFrameLength = 7; |
| 77 | constexpr ssize_t ipmbMaxFrameLength = |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 78 | ipmbPktLenSize + ipmbConnectionHeaderLength + ipmbResponseDataHeaderLength + |
| 79 | ipmbChecksumSize + ipmbMaxDataSize; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * @brief Ipmb misc |
| 83 | */ |
| 84 | constexpr uint8_t ipmbNetFnResponseMask = 0x01; |
| 85 | constexpr uint8_t ipmbLunMask = 0x03; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 86 | constexpr uint8_t ipmbRsLun = 0x0; |
| 87 | |
| 88 | /** |
| 89 | * @brief Ipmb setters |
| 90 | */ |
| 91 | constexpr uint8_t ipmbNetFnLunSet(uint8_t netFn, uint8_t lun) |
| 92 | { |
| 93 | return ((netFn << 2) | (lun & ipmbLunMask)); |
| 94 | } |
| 95 | |
| 96 | constexpr uint8_t ipmbSeqLunSet(uint8_t seq, uint8_t lun) |
| 97 | { |
| 98 | return ((seq << 2) | (lun & ipmbLunMask)); |
| 99 | } |
| 100 | |
| 101 | constexpr uint8_t ipmbAddressTo7BitSet(uint8_t address) |
| 102 | { |
| 103 | return address >> 1; |
| 104 | } |
| 105 | |
| 106 | constexpr uint8_t ipmbRespNetFn(uint8_t netFn) |
| 107 | { |
| 108 | return netFn |= 1; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @brief Ipmb getters |
| 113 | */ |
| 114 | constexpr uint8_t ipmbNetFnGet(uint8_t netFnLun) |
| 115 | { |
| 116 | return netFnLun >> 2; |
| 117 | } |
| 118 | |
| 119 | constexpr uint8_t ipmbLunFromNetFnLunGet(uint8_t netFnLun) |
| 120 | { |
| 121 | return netFnLun & ipmbLunMask; |
| 122 | } |
| 123 | |
| 124 | constexpr uint8_t ipmbSeqGet(uint8_t seqNumLun) |
| 125 | { |
| 126 | return seqNumLun >> 2; |
| 127 | } |
| 128 | |
| 129 | constexpr uint8_t ipmbLunFromSeqLunGet(uint8_t seqNumLun) |
| 130 | { |
| 131 | return seqNumLun & ipmbLunMask; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @brief Ipmb checkers |
| 136 | */ |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 137 | constexpr bool ipmbIsResponse(IPMB_HEADER* ipmbHeader) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 138 | { |
| 139 | return ipmbNetFnGet(ipmbHeader->Header.Resp.rqNetFnLUN) & |
| 140 | ipmbNetFnResponseMask; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @brief Ipmb request state |
| 145 | */ |
| 146 | enum class ipmbRequestState |
| 147 | { |
| 148 | invalid, |
| 149 | valid, |
| 150 | matched, |
| 151 | }; |
| 152 | |
| 153 | /** |
| 154 | * @brief Channel types |
| 155 | */ |
| 156 | enum class ipmbChannelType |
| 157 | { |
| 158 | ipmb = 0, |
| 159 | me = 1 |
| 160 | }; |
| 161 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 162 | /** |
| 163 | * @brief IpmbResponse declaration |
| 164 | */ |
| 165 | struct IpmbResponse |
| 166 | { |
| 167 | uint8_t address; |
| 168 | uint8_t netFn; |
| 169 | uint8_t rqLun; |
| 170 | uint8_t rsSA; |
| 171 | uint8_t seq; |
| 172 | uint8_t rsLun; |
| 173 | uint8_t cmd; |
| 174 | uint8_t completionCode; |
| 175 | std::vector<uint8_t> data; |
| 176 | |
| 177 | IpmbResponse(); |
| 178 | |
| 179 | IpmbResponse(uint8_t address, uint8_t netFn, uint8_t rqLun, uint8_t rsSA, |
| 180 | uint8_t seq, uint8_t rsLun, uint8_t cmd, |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 181 | uint8_t completionCode, const std::vector<uint8_t>& inputData); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 182 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 183 | void i2cToIpmbConstruct(IPMB_HEADER* ipmbBuffer, size_t bufferLength); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 184 | |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 185 | std::shared_ptr<std::vector<uint8_t>> ipmbToi2cConstruct(); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | /** |
| 189 | * @brief IpmbRequest declaration |
| 190 | */ |
| 191 | struct IpmbRequest |
| 192 | { |
| 193 | uint8_t address; |
| 194 | uint8_t netFn; |
| 195 | uint8_t rsLun; |
| 196 | uint8_t rqSA; |
| 197 | uint8_t seq; |
| 198 | uint8_t rqLun; |
| 199 | uint8_t cmd; |
| 200 | std::vector<uint8_t> data; |
| 201 | |
| 202 | size_t dataLength; |
| 203 | ipmbRequestState state; |
| 204 | std::optional<boost::asio::steady_timer> timer; |
| 205 | std::unique_ptr<IpmbResponse> matchedResponse; |
| 206 | |
| 207 | // creates empty request with empty timer object |
| 208 | IpmbRequest(); |
| 209 | |
| 210 | IpmbRequest(uint8_t address, uint8_t netFn, uint8_t rsLun, uint8_t rqSA, |
| 211 | uint8_t seq, uint8_t rqLun, uint8_t cmd, |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 212 | const std::vector<uint8_t>& inputData); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 213 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 214 | IpmbRequest(const IpmbRequest&) = delete; |
| 215 | IpmbRequest& operator=(const IpmbRequest&) = delete; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 216 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 217 | std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>> |
| 218 | returnMatchedResponse(); |
| 219 | |
| 220 | std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>> |
| 221 | returnStatusResponse(int status); |
| 222 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 223 | void i2cToIpmbConstruct(IPMB_HEADER* ipmbBuffer, size_t bufferLength); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 224 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 225 | int ipmbToi2cConstruct(std::vector<uint8_t>& buffer); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | /** |
| 229 | * @brief Command filtering class declaration |
| 230 | * |
| 231 | * This feature provides simple mechanism for filtering out commands - which are |
| 232 | * not implemented in IPMI - on IPMB level, in order to reduce DBus traffic |
| 233 | */ |
| 234 | class IpmbCommandFilter |
| 235 | { |
| 236 | public: |
| 237 | // function checking if netFn & cmd combination exist in blocked command |
| 238 | // list |
| 239 | bool isBlocked(const uint8_t reqNetFn, const uint8_t cmd); |
| 240 | // function adding netfFn & cmd combination to the blocked command list |
| 241 | void addFilter(const uint8_t reqNetFn, const uint8_t cmd); |
| 242 | |
| 243 | private: |
| 244 | boost::container::flat_set<std::pair<uint8_t, uint8_t>> unhandledCommands; |
| 245 | }; |
| 246 | |
| 247 | /** |
| 248 | * @brief Command filtering defines |
| 249 | */ |
| 250 | |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 251 | constexpr uint8_t ipmbIpmiInvalidCmd = 0xC1; |
| 252 | constexpr uint8_t ipmbIpmiCmdRespNotProvided = 0xCE; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 253 | |
| 254 | constexpr uint8_t ipmbReqNetFnFromRespNetFn(uint8_t reqNetFn) |
| 255 | { |
| 256 | return reqNetFn & ~ipmbNetFnResponseMask; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * @brief IpmbChannel class declaration |
| 261 | */ |
| 262 | class IpmbChannel |
| 263 | { |
| 264 | public: |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame^] | 265 | IpmbChannel(boost::asio::io_context& io, uint8_t ipmbBmcTargetAddress, |
| 266 | uint8_t ipmbRqTargetAddress, uint8_t channelIdx, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 267 | std::shared_ptr<IpmbCommandFilter> commandFilter); |
| 268 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 269 | IpmbChannel(const IpmbChannel&) = delete; |
| 270 | IpmbChannel& operator=(const IpmbChannel&) = delete; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 271 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame^] | 272 | int ipmbChannelInit(const char* ipmbI2cTarget); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 273 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame^] | 274 | int ipmbChannelUpdateTargetAddress(const uint8_t newBmcTargetAddr); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 275 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 276 | bool seqNumGet(uint8_t& seq); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 277 | |
| 278 | ipmbChannelType getChannelType(); |
| 279 | |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 280 | uint8_t getBusId(); |
| 281 | |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 282 | uint8_t getDevIndex(); |
| 283 | |
| 284 | uint8_t getChannelIdx(); |
| 285 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame^] | 286 | uint8_t getBmcTargetAddress(); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 287 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame^] | 288 | uint8_t getRqTargetAddress(); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 289 | |
| 290 | void addFilter(const uint8_t respNetFn, const uint8_t cmd); |
| 291 | |
| 292 | void processI2cEvent(); |
| 293 | |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 294 | void ipmbSendI2cFrame(std::shared_ptr<std::vector<uint8_t>> buffer, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 295 | size_t retriesAttempted); |
| 296 | |
| 297 | std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>> |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 298 | requestAdd(boost::asio::yield_context& yield, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 299 | std::shared_ptr<IpmbRequest> requestToSend); |
| 300 | |
| 301 | private: |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame^] | 302 | boost::asio::posix::stream_descriptor i2cTargetDescriptor; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 303 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame^] | 304 | int ipmbi2cTargetFd; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 305 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame^] | 306 | uint8_t ipmbBmcTargetAddress; |
| 307 | uint8_t ipmbRqTargetAddress; |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 308 | uint8_t ipmbBusId; |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 309 | uint8_t channelIdx; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 310 | |
| 311 | std::shared_ptr<IpmbCommandFilter> commandFilter; |
| 312 | |
| 313 | // array storing outstanding requests |
| 314 | std::array<std::shared_ptr<IpmbRequest>, ipmbMaxOutstandingRequestsCount> |
| 315 | outstandingRequests; |
| 316 | |
| 317 | void requestTimerCallback(std::shared_ptr<IpmbRequest> request, |
| 318 | std::shared_ptr<std::vector<uint8_t>> buffer); |
| 319 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 320 | void responseMatch(std::unique_ptr<IpmbResponse>& response); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 321 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 322 | void makeRequestInvalid(IpmbRequest& request); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 323 | |
| 324 | void makeRequestValid(std::shared_ptr<IpmbRequest> request); |
| 325 | }; |
| 326 | |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 327 | /** |
| 328 | * @brief ioWrite class declaration |
| 329 | */ |
| 330 | class ioWrite |
| 331 | { |
| 332 | public: |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 333 | ioWrite(std::vector<uint8_t>& buffer) |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 334 | { |
| 335 | i2cmsg[0].addr = ipmbAddressTo7BitSet(buffer[0]); |
| 336 | i2cmsg[0].len = buffer.size() - ipmbAddressSize; |
| 337 | i2cmsg[0].buf = buffer.data() + ipmbAddressSize; |
| 338 | |
| 339 | msgRdwr.msgs = i2cmsg; |
| 340 | msgRdwr.nmsgs = 1; |
| 341 | }; |
| 342 | |
| 343 | int name() |
| 344 | { |
| 345 | return static_cast<int>(I2C_RDWR); |
| 346 | } |
| 347 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 348 | void* data() |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 349 | { |
| 350 | return &msgRdwr; |
| 351 | } |
| 352 | |
| 353 | private: |
Patrick Williams | f094ab3 | 2023-03-30 15:59:16 -0500 | [diff] [blame] | 354 | i2c_rdwr_ioctl_data msgRdwr = {}; |
| 355 | i2c_msg i2cmsg[1] = {}; |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 356 | }; |
| 357 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 358 | #endif |