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 "ipmbbridged.hpp" |
| 17 | |
| 18 | #include "ipmbdefines.hpp" |
| 19 | #include "ipmbutils.hpp" |
| 20 | |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 21 | #include <boost/algorithm/string/replace.hpp> |
Ed Tanous | 1486b8a | 2023-02-28 13:28:59 -0800 | [diff] [blame] | 22 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 09027c0 | 2020-08-31 17:28:36 -0700 | [diff] [blame] | 23 | #include <boost/asio/write.hpp> |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 24 | #include <nlohmann/json.hpp> |
| 25 | #include <phosphor-logging/log.hpp> |
| 26 | |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 27 | #include <filesystem> |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 28 | #include <fstream> |
Jason M. Bills | 7867cd7 | 2023-03-29 07:58:02 -0700 | [diff] [blame] | 29 | #include <list> |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 30 | #include <tuple> |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 31 | #include <unordered_map> |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 32 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 33 | /** |
| 34 | * @brief Dbus |
| 35 | */ |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 36 | static constexpr const char* ipmbBus = "xyz.openbmc_project.Ipmi.Channel.Ipmb"; |
| 37 | static constexpr const char* ipmbObj = "/xyz/openbmc_project/Ipmi/Channel/Ipmb"; |
| 38 | static constexpr const char* ipmbDbusIntf = "org.openbmc.Ipmb"; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 39 | |
Ed Tanous | 1486b8a | 2023-02-28 13:28:59 -0800 | [diff] [blame] | 40 | boost::asio::io_context io; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 41 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 42 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 43 | static std::list<IpmbChannel> ipmbChannels; |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 44 | static const std::unordered_map<std::string, ipmbChannelType> |
| 45 | ipmbChannelTypeMap = {{"me", ipmbChannelType::me}, |
| 46 | {"ipmb", ipmbChannelType::ipmb}}; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * @brief Ipmb request class methods |
| 50 | */ |
| 51 | IpmbRequest::IpmbRequest() |
| 52 | { |
| 53 | data.reserve(ipmbMaxDataSize); |
| 54 | } |
| 55 | |
| 56 | IpmbRequest::IpmbRequest(uint8_t address, uint8_t netFn, uint8_t rsLun, |
| 57 | uint8_t rqSA, uint8_t seq, uint8_t rqLun, uint8_t cmd, |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 58 | const std::vector<uint8_t>& inputData) : |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 59 | address(address), netFn(netFn), rsLun(rsLun), rqSA(rqSA), seq(seq), |
| 60 | rqLun(rqLun), cmd(cmd), timer(io) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 61 | { |
| 62 | data.reserve(ipmbMaxDataSize); |
| 63 | state = ipmbRequestState::invalid; |
| 64 | |
| 65 | if (inputData.size() > 0) |
| 66 | { |
| 67 | data = std::move(inputData); |
| 68 | } |
| 69 | } |
| 70 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 71 | void IpmbRequest::i2cToIpmbConstruct(IPMB_HEADER* ipmbBuffer, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 72 | size_t bufferLength) |
| 73 | { |
| 74 | // constructing ipmb request from i2c buffer |
| 75 | netFn = ipmbNetFnGet(ipmbBuffer->Header.Req.rsNetFnLUN); |
| 76 | rsLun = ipmbLunFromNetFnLunGet(ipmbBuffer->Header.Req.rsNetFnLUN); |
| 77 | rqSA = ipmbBuffer->Header.Req.rqSA; |
| 78 | seq = ipmbSeqGet(ipmbBuffer->Header.Req.rqSeqLUN); |
| 79 | rqLun = ipmbLunFromSeqLunGet(ipmbBuffer->Header.Req.rqSeqLUN); |
| 80 | cmd = ipmbBuffer->Header.Req.cmd; |
| 81 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 82 | size_t dataLength = |
| 83 | bufferLength - (ipmbConnectionHeaderLength + |
| 84 | ipmbRequestDataHeaderLength + ipmbChecksumSize); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 85 | |
| 86 | if (dataLength > 0) |
| 87 | { |
| 88 | data.insert(data.end(), ipmbBuffer->Header.Req.data, |
| 89 | &ipmbBuffer->Header.Req.data[dataLength]); |
| 90 | } |
| 91 | } |
| 92 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 93 | int IpmbRequest::ipmbToi2cConstruct(std::vector<uint8_t>& buffer) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 94 | { |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 95 | /* Add one byte for length byte as per required by driver */ |
| 96 | size_t bufferLength = 1 + data.size() + ipmbRequestDataHeaderLength + |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 97 | ipmbConnectionHeaderLength + ipmbChecksumSize; |
| 98 | |
| 99 | if (bufferLength > ipmbMaxFrameLength) |
| 100 | { |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | buffer.resize(bufferLength); |
| 105 | static_assert(ipmbMaxFrameLength >= sizeof(IPMB_HEADER)); |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 106 | IPMB_PKT* ipmbPkt = reinterpret_cast<IPMB_PKT*>(buffer.data()); |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 107 | ipmbPkt->len = bufferLength - 1; |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 108 | IPMB_HEADER* ipmbBuffer = &(ipmbPkt->hdr); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 109 | |
| 110 | // constructing buffer from ipmb request |
| 111 | ipmbBuffer->Header.Req.address = address; |
| 112 | ipmbBuffer->Header.Req.rsNetFnLUN = ipmbNetFnLunSet(netFn, rsLun); |
| 113 | ipmbBuffer->Header.Req.rqSA = rqSA; |
| 114 | ipmbBuffer->Header.Req.rqSeqLUN = ipmbSeqLunSet(seq, rqLun); |
| 115 | ipmbBuffer->Header.Req.cmd = cmd; |
| 116 | |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 117 | ipmbBuffer->Header.Req.checksum1 = ipmbChecksumCompute( |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 118 | (uint8_t*)ipmbBuffer, ipmbConnectionHeaderLength - ipmbChecksumSize); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 119 | |
| 120 | if (data.size() > 0) |
| 121 | { |
| 122 | std::copy(data.begin(), data.end(), ipmbBuffer->Header.Req.data); |
| 123 | } |
| 124 | |
| 125 | buffer[bufferLength - ipmbChecksumSize] = |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 126 | ipmbChecksumCompute((uint8_t*)ipmbBuffer + ipmbChecksum2StartOffset, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 127 | (ipmbRequestDataHeaderLength + data.size())); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>> |
| 133 | IpmbRequest::returnMatchedResponse() |
| 134 | { |
| 135 | return std::make_tuple( |
| 136 | static_cast<int>(ipmbResponseStatus::success), matchedResponse->netFn, |
| 137 | matchedResponse->rsLun, matchedResponse->cmd, |
| 138 | matchedResponse->completionCode, matchedResponse->data); |
| 139 | } |
| 140 | |
| 141 | static std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>> |
| 142 | returnStatus(ipmbResponseStatus status) |
| 143 | { |
| 144 | // we only want to send status here, other fields are not relevant |
| 145 | return std::make_tuple(static_cast<int>(status), 0, 0, 0, 0, |
| 146 | std::vector<uint8_t>(0)); |
| 147 | } |
| 148 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 149 | /** |
| 150 | * @brief Ipmb response class methods |
| 151 | */ |
| 152 | IpmbResponse::IpmbResponse() |
| 153 | { |
| 154 | data.reserve(ipmbMaxDataSize); |
| 155 | } |
| 156 | |
| 157 | IpmbResponse::IpmbResponse(uint8_t address, uint8_t netFn, uint8_t rqLun, |
| 158 | uint8_t rsSA, uint8_t seq, uint8_t rsLun, |
| 159 | uint8_t cmd, uint8_t completionCode, |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 160 | const std::vector<uint8_t>& inputData) : |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 161 | address(address), netFn(netFn), rqLun(rqLun), rsSA(rsSA), seq(seq), |
| 162 | rsLun(rsLun), cmd(cmd), completionCode(completionCode) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 163 | { |
| 164 | data.reserve(ipmbMaxDataSize); |
| 165 | |
| 166 | if (inputData.size() > 0) |
| 167 | { |
| 168 | data = std::move(inputData); |
| 169 | } |
| 170 | } |
| 171 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 172 | void IpmbResponse::i2cToIpmbConstruct(IPMB_HEADER* ipmbBuffer, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 173 | size_t bufferLength) |
| 174 | { |
| 175 | netFn = ipmbNetFnGet(ipmbBuffer->Header.Resp.rqNetFnLUN); |
| 176 | rqLun = ipmbLunFromNetFnLunGet(ipmbBuffer->Header.Resp.rqNetFnLUN); |
| 177 | rsSA = ipmbBuffer->Header.Resp.rsSA; |
| 178 | seq = ipmbSeqGet(ipmbBuffer->Header.Resp.rsSeqLUN); |
| 179 | rsLun = ipmbLunFromSeqLunGet(ipmbBuffer->Header.Resp.rsSeqLUN); |
| 180 | cmd = ipmbBuffer->Header.Resp.cmd; |
| 181 | completionCode = ipmbBuffer->Header.Resp.completionCode; |
| 182 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 183 | size_t dataLength = |
| 184 | bufferLength - (ipmbConnectionHeaderLength + |
| 185 | ipmbResponseDataHeaderLength + ipmbChecksumSize); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 186 | |
| 187 | if (dataLength > 0) |
| 188 | { |
| 189 | data.insert(data.end(), ipmbBuffer->Header.Resp.data, |
| 190 | &ipmbBuffer->Header.Resp.data[dataLength]); |
| 191 | } |
| 192 | } |
| 193 | |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 194 | std::shared_ptr<std::vector<uint8_t>> IpmbResponse::ipmbToi2cConstruct() |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 195 | { |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 196 | /* Add one byte for length byte as per required by driver */ |
| 197 | size_t bufferLength = 1 + data.size() + ipmbResponseDataHeaderLength + |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 198 | ipmbConnectionHeaderLength + ipmbChecksumSize; |
| 199 | |
| 200 | if (bufferLength > ipmbMaxFrameLength) |
| 201 | { |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 202 | return nullptr; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 205 | std::shared_ptr<std::vector<uint8_t>> buffer = |
| 206 | std::make_shared<std::vector<uint8_t>>(bufferLength); |
| 207 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 208 | IPMB_PKT* ipmbPkt = reinterpret_cast<IPMB_PKT*>(buffer->data()); |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 209 | ipmbPkt->len = bufferLength - 1; |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 210 | IPMB_HEADER* ipmbBuffer = &(ipmbPkt->hdr); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 211 | |
| 212 | ipmbBuffer->Header.Resp.address = address; |
| 213 | ipmbBuffer->Header.Resp.rqNetFnLUN = ipmbNetFnLunSet(netFn, rqLun); |
| 214 | ipmbBuffer->Header.Resp.rsSA = rsSA; |
| 215 | ipmbBuffer->Header.Resp.rsSeqLUN = ipmbSeqLunSet(seq, rsLun); |
| 216 | ipmbBuffer->Header.Resp.cmd = cmd; |
| 217 | ipmbBuffer->Header.Resp.completionCode = completionCode; |
| 218 | |
| 219 | ipmbBuffer->Header.Resp.checksum1 = ipmbChecksumCompute( |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 220 | (uint8_t*)ipmbBuffer, ipmbConnectionHeaderLength - ipmbChecksumSize); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 221 | |
| 222 | if (data.size() > 0) |
| 223 | { |
| 224 | std::copy(data.begin(), data.end(), ipmbBuffer->Header.Resp.data); |
| 225 | } |
| 226 | |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 227 | (*buffer)[bufferLength - ipmbChecksumSize] = |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 228 | ipmbChecksumCompute((uint8_t*)ipmbBuffer + ipmbChecksum2StartOffset, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 229 | (ipmbResponseDataHeaderLength + data.size())); |
| 230 | |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 231 | return buffer; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | bool IpmbCommandFilter::isBlocked(const uint8_t reqNetFn, const uint8_t cmd) |
| 235 | { |
| 236 | auto blockedCmd = unhandledCommands.find({reqNetFn, cmd}); |
| 237 | |
| 238 | if (blockedCmd != unhandledCommands.end()) |
| 239 | { |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | void IpmbCommandFilter::addFilter(const uint8_t reqNetFn, const uint8_t cmd) |
| 247 | { |
| 248 | if (unhandledCommands.insert({reqNetFn, cmd}).second) |
| 249 | { |
| 250 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 251 | "addFilter: added command to filter", |
| 252 | phosphor::logging::entry("netFn = %d", reqNetFn), |
| 253 | phosphor::logging::entry("cmd = %d", cmd)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @brief Ipmb channel |
| 259 | */ |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 260 | void IpmbChannel::ipmbSendI2cFrame(std::shared_ptr<std::vector<uint8_t>> buffer, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 261 | size_t retriesAttempted = 0) |
| 262 | { |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 263 | IPMB_PKT* ipmbPkt = reinterpret_cast<IPMB_PKT*>(buffer->data()); |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 264 | uint8_t targetAddr = ipmbIsResponse(&(ipmbPkt->hdr)) |
| 265 | ? ipmbPkt->hdr.Header.Resp.address |
| 266 | : ipmbPkt->hdr.Header.Req.address; |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 267 | boost::asio::async_write( |
| 268 | i2cTargetDescriptor, boost::asio::buffer(*buffer), |
| 269 | [this, buffer, retriesAttempted, targetAddr]( |
| 270 | const boost::system::error_code& ec, size_t /* bytesSent */) { |
| 271 | if (ec) |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 272 | { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 273 | size_t currentRetryCnt = retriesAttempted; |
| 274 | |
| 275 | if (currentRetryCnt > ipmbI2cNumberOfRetries) |
| 276 | { |
| 277 | std::string msgToLog = |
| 278 | "ipmbSendI2cFrame: send to I2C failed after retries." |
| 279 | " busId=" + |
| 280 | std::to_string(ipmbBusId) + |
| 281 | ", targetAddr=" + std::to_string(targetAddr) + |
| 282 | ", error=" + ec.message(); |
| 283 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 284 | msgToLog.c_str()); |
| 285 | return; |
| 286 | } |
| 287 | currentRetryCnt++; |
| 288 | ipmbSendI2cFrame(buffer, currentRetryCnt); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 289 | } |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 290 | }); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @brief Ipmb Outstanding Requests |
| 295 | */ |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 296 | void IpmbChannel::makeRequestInvalid(IpmbRequest& request) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 297 | { |
| 298 | // change request state to invalid and remove it from outstanding requests |
| 299 | // list |
| 300 | request.state = ipmbRequestState::invalid; |
| 301 | outstandingRequests[request.seq] = nullptr; |
| 302 | } |
| 303 | |
| 304 | void IpmbChannel::makeRequestValid(std::shared_ptr<IpmbRequest> request) |
| 305 | { |
| 306 | // change request state to valid and add it to outstanding requests list |
| 307 | request->state = ipmbRequestState::valid; |
| 308 | outstandingRequests[request->seq] = request; |
| 309 | } |
| 310 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 311 | bool IpmbChannel::seqNumGet(uint8_t& seq) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 312 | { |
| 313 | static uint8_t seqNum = 0; |
| 314 | |
| 315 | for (int i = 0; i < ipmbMaxOutstandingRequestsCount; i++) |
| 316 | { |
Patrick Williams | e523af3 | 2023-03-30 15:32:06 -0500 | [diff] [blame] | 317 | seqNum = (seqNum + 1) % ipmbMaxOutstandingRequestsCount; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 318 | |
| 319 | if (outstandingRequests[seqNum] == nullptr) |
| 320 | { |
| 321 | seq = seqNum; |
| 322 | return true; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return false; |
| 327 | } |
| 328 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 329 | void IpmbChannel::responseMatch(std::unique_ptr<IpmbResponse>& response) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 330 | { |
| 331 | std::shared_ptr<IpmbRequest> request = outstandingRequests[response->seq]; |
| 332 | |
| 333 | if (request != nullptr) |
| 334 | { |
| 335 | if (((ipmbRespNetFn(request->netFn)) == (response->netFn)) && |
| 336 | ((request->rqLun) == (response->rqLun)) && |
| 337 | ((request->rsLun) == (response->rsLun)) && |
| 338 | ((request->cmd) == (response->cmd))) |
| 339 | { |
| 340 | // match, response is corresponding to previously sent request |
| 341 | request->state = ipmbRequestState::matched; |
| 342 | request->timer->cancel(); |
| 343 | request->matchedResponse = std::move(response); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void IpmbChannel::processI2cEvent() |
| 349 | { |
| 350 | std::array<uint8_t, ipmbMaxFrameLength> buffer{}; |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 351 | IPMB_PKT* ipmbPkt = reinterpret_cast<IPMB_PKT*>(buffer.data()); |
| 352 | IPMB_HEADER* ipmbFrame = &(ipmbPkt->hdr); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 353 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 354 | lseek(ipmbi2cTargetFd, 0, SEEK_SET); |
| 355 | ssize_t r = read(ipmbi2cTargetFd, buffer.data(), ipmbMaxFrameLength); |
Patrick Williams | 1f5b24b | 2023-03-30 15:36:03 -0500 | [diff] [blame] | 356 | |
| 357 | // Handle error cases. |
| 358 | if (r < 0) |
| 359 | { |
| 360 | goto end; |
| 361 | } |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 362 | |
Manojkiran Eda | 524f753 | 2024-06-17 14:22:24 +0530 | [diff] [blame] | 363 | /* Subtract first byte len size from total frame length */ |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 364 | r--; |
| 365 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 366 | if ((r < ipmbMinFrameLength) || (r > ipmbMaxFrameLength)) |
| 367 | { |
| 368 | goto end; |
| 369 | } |
| 370 | |
Manojkiran Eda | 524f753 | 2024-06-17 14:22:24 +0530 | [diff] [blame] | 371 | // validate the frame |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 372 | if (!isFrameValid(ipmbFrame, r)) |
| 373 | { |
| 374 | goto end; |
| 375 | } |
| 376 | |
Chen Yugang | 15185ff | 2020-09-01 09:20:33 +0800 | [diff] [blame] | 377 | // if it is message received from ipmb channel, send out dbus signal |
| 378 | if (getChannelType() == ipmbChannelType::ipmb) |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 379 | { |
| 380 | auto ipmbMessageReceived = IpmbRequest(); |
| 381 | ipmbMessageReceived.i2cToIpmbConstruct(ipmbFrame, r); |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 382 | sdbusplus::message_t msg = |
| 383 | conn->new_signal(ipmbObj, ipmbDbusIntf, "receiveBroadcast"); |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 384 | msg.append(ipmbMessageReceived.netFn, ipmbMessageReceived.cmd, |
| 385 | ipmbMessageReceived.data); |
| 386 | msg.signal_send(); |
| 387 | } |
| 388 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 389 | // copy frame to ipmib message buffer |
Chen Yugang | 3e07b9e | 2020-10-13 16:14:04 +0800 | [diff] [blame] | 390 | if (ipmbIsResponse(ipmbFrame)) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 391 | { |
| 392 | std::unique_ptr<IpmbResponse> ipmbMessageReceived = |
| 393 | std::make_unique<IpmbResponse>(); |
| 394 | |
| 395 | ipmbMessageReceived->i2cToIpmbConstruct(ipmbFrame, r); |
| 396 | |
| 397 | // try to match response with outstanding request |
| 398 | responseMatch(ipmbMessageReceived); |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | // if command is blocked - respond with 'invalid command' |
| 403 | // completion code |
| 404 | if (commandFilter) |
| 405 | { |
| 406 | uint8_t netFn = ipmbNetFnGet(ipmbFrame->Header.Req.rsNetFnLUN); |
| 407 | uint8_t cmd = ipmbFrame->Header.Req.cmd; |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 408 | uint8_t rqSA = ipmbFrame->Header.Req.rqSA; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 409 | |
| 410 | if (commandFilter->isBlocked(netFn, cmd)) |
| 411 | { |
| 412 | uint8_t seq = ipmbSeqGet(ipmbFrame->Header.Req.rqSeqLUN); |
| 413 | uint8_t lun = |
| 414 | ipmbLunFromSeqLunGet(ipmbFrame->Header.Req.rqSeqLUN); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 415 | |
| 416 | // prepare generic response |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 417 | auto ipmbResponse = IpmbResponse( |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 418 | rqSA, ipmbRespNetFn(netFn), lun, ipmbBmcTargetAddress, seq, |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 419 | ipmbRsLun, cmd, ipmbIpmiInvalidCmd, {}); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 420 | |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 421 | auto buffer = ipmbResponse.ipmbToi2cConstruct(); |
| 422 | if (buffer) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 423 | { |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 424 | ipmbSendI2cFrame(buffer); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | goto end; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | auto ipmbMessageReceived = IpmbRequest(); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 432 | ipmbMessageReceived.i2cToIpmbConstruct(ipmbFrame, r); |
| 433 | |
Kumar Thangavel | 8fe0abe | 2020-08-19 21:42:23 +0530 | [diff] [blame] | 434 | int devId = getDevIndex(); |
| 435 | |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 436 | std::map<std::string, std::variant<int>> options{ |
Kumar Thangavel | 8fe0abe | 2020-08-19 21:42:23 +0530 | [diff] [blame] | 437 | {"rqSA", ipmbAddressTo7BitSet(ipmbMessageReceived.rqSA)}, |
| 438 | {"hostId", devId}}; |
| 439 | |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 440 | using IpmiDbusRspType = std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, |
| 441 | std::vector<uint8_t>>; |
| 442 | conn->async_method_call( |
| 443 | [this, rqLun{ipmbMessageReceived.rqLun}, |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 444 | seq{ipmbMessageReceived.seq}, address{ipmbMessageReceived.rqSA}]( |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 445 | const boost::system::error_code& ec, |
| 446 | const IpmiDbusRspType& response) { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 447 | const auto& [netfn, lun, cmd, cc, payload] = response; |
| 448 | if (ec) |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 449 | { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 450 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 451 | "processI2cEvent: error getting response from IPMI"); |
| 452 | return; |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 455 | uint8_t bmcTargetAddress = getBmcTargetAddress(); |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 456 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 457 | if (payload.size() > ipmbMaxDataSize) |
| 458 | { |
| 459 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 460 | "processI2cEvent: response exceeding maximum size"); |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 461 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 462 | // prepare generic response |
| 463 | auto ipmbResponse = IpmbResponse( |
| 464 | address, netfn, rqLun, bmcTargetAddress, seq, ipmbRsLun, |
| 465 | cmd, ipmbIpmiCmdRespNotProvided, {}); |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 466 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 467 | auto buffer = ipmbResponse.ipmbToi2cConstruct(); |
| 468 | if (buffer) |
| 469 | { |
| 470 | ipmbSendI2cFrame(buffer); |
| 471 | } |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 472 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 473 | return; |
| 474 | } |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 475 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 476 | if (!(netfn & ipmbNetFnResponseMask)) |
| 477 | { |
| 478 | // we are not expecting request here |
| 479 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 480 | "processI2cEvent: got a request instead of response"); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | // if command is not supported, add it to filter |
| 485 | if (cc == ipmbIpmiInvalidCmd) |
| 486 | { |
| 487 | addFilter(ipmbReqNetFnFromRespNetFn(netfn), cmd); |
| 488 | } |
| 489 | |
| 490 | // payload is empty after constructor invocation |
| 491 | auto ipmbResponse = |
| 492 | IpmbResponse(address, netfn, rqLun, bmcTargetAddress, seq, |
| 493 | lun, cmd, cc, payload); |
| 494 | |
| 495 | auto buffer = ipmbResponse.ipmbToi2cConstruct(); |
| 496 | if (!buffer) |
| 497 | { |
| 498 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 499 | "processI2cEvent: error constructing a request"); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | ipmbSendI2cFrame(buffer); |
| 504 | }, |
Dawid Frycki | 8188d76 | 2019-04-01 18:03:48 -0700 | [diff] [blame] | 505 | "xyz.openbmc_project.Ipmi.Host", "/xyz/openbmc_project/Ipmi", |
| 506 | "xyz.openbmc_project.Ipmi.Server", "execute", |
| 507 | ipmbMessageReceived.netFn, ipmbMessageReceived.rsLun, |
| 508 | ipmbMessageReceived.cmd, ipmbMessageReceived.data, options); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | end: |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 512 | i2cTargetDescriptor.async_wait( |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 513 | boost::asio::posix::descriptor_base::wait_read, |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 514 | [this](const boost::system::error_code& ec) { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 515 | if (ec) |
| 516 | { |
| 517 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 518 | "Error: processI2cEvent()"); |
| 519 | return; |
| 520 | } |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 521 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 522 | processI2cEvent(); |
| 523 | }); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 526 | IpmbChannel::IpmbChannel(boost::asio::io_context& io, |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 527 | uint8_t ipmbBmcTargetAddress, |
| 528 | uint8_t ipmbRqTargetAddress, uint8_t channelIdx, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 529 | std::shared_ptr<IpmbCommandFilter> commandFilter) : |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 530 | i2cTargetDescriptor(io), ipmbBmcTargetAddress(ipmbBmcTargetAddress), |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 531 | ipmbRqTargetAddress(ipmbRqTargetAddress), channelIdx(channelIdx), |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 532 | commandFilter(commandFilter) |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 533 | {} |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 534 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 535 | int IpmbChannel::ipmbChannelInit(const char* ipmbI2cTarget) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 536 | { |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 537 | // extract bus id from target path and save |
| 538 | std::string ipmbI2cTargetStr(ipmbI2cTarget); |
| 539 | auto findHyphen = ipmbI2cTargetStr.find("-"); |
| 540 | std::string busStr = ipmbI2cTargetStr.substr(findHyphen + 1); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 541 | try |
| 542 | { |
| 543 | ipmbBusId = std::stoi(busStr); |
| 544 | } |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 545 | catch (const std::invalid_argument&) |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 546 | { |
| 547 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 548 | "ipmbChannelInit: invalid bus id in target-path config"); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 549 | return -1; |
| 550 | } |
| 551 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 552 | // Check if sysfs has device. If not, enable I2C target driver by command |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 553 | // echo "ipmb-dev 0x1010" > /sys/bus/i2c/devices/i2c-0/new_device |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 554 | bool hasSysfs = std::filesystem::exists(ipmbI2cTarget); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 555 | if (!hasSysfs) |
| 556 | { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 557 | std::string deviceFileName = |
| 558 | "/sys/bus/i2c/devices/i2c-" + busStr + "/new_device"; |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 559 | std::string para = "ipmb-dev 0x1010"; // init with BMC addr 0x20 |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 560 | std::fstream deviceFile; |
| 561 | deviceFile.open(deviceFileName, std::ios::out); |
| 562 | if (!deviceFile.good()) |
| 563 | { |
| 564 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 565 | "ipmbChannelInit: error opening deviceFile"); |
| 566 | return -1; |
| 567 | } |
| 568 | deviceFile << para; |
| 569 | deviceFile.close(); |
| 570 | } |
| 571 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 572 | // open fd to i2c target device for read write |
| 573 | ipmbi2cTargetFd = open(ipmbI2cTarget, O_RDWR | O_NONBLOCK | O_CLOEXEC); |
| 574 | if (ipmbi2cTargetFd < 0) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 575 | { |
| 576 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 577 | "ipmbChannelInit: error opening ipmbI2cTarget"); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 578 | return -1; |
| 579 | } |
| 580 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 581 | i2cTargetDescriptor.assign(ipmbi2cTargetFd); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 582 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 583 | i2cTargetDescriptor.async_wait( |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 584 | boost::asio::posix::descriptor_base::wait_read, |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 585 | [this](const boost::system::error_code& ec) { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 586 | if (ec) |
| 587 | { |
| 588 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 589 | "Error: processI2cEvent()"); |
| 590 | return; |
| 591 | } |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 592 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 593 | processI2cEvent(); |
| 594 | }); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 599 | int IpmbChannel::ipmbChannelUpdateTargetAddress(const uint8_t newBmcTargetAddr) |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 600 | { |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 601 | if (ipmbi2cTargetFd > 0) |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 602 | { |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 603 | i2cTargetDescriptor.close(); |
| 604 | close(ipmbi2cTargetFd); |
| 605 | ipmbi2cTargetFd = 0; |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 606 | } |
| 607 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 608 | // disable old I2C target driver by command: |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 609 | // echo "0x1010" > /sys/bus/i2c/devices/i2c-0/delete_device |
| 610 | std::string deviceFileName; |
| 611 | std::string para; |
| 612 | std::fstream deviceFile; |
| 613 | deviceFileName = "/sys/bus/i2c/devices/i2c-" + std::to_string(ipmbBusId) + |
| 614 | "/delete_device"; |
| 615 | para = "0x1010"; // align with removed ipmb0 definition in dts file |
| 616 | deviceFile.open(deviceFileName, std::ios::out); |
| 617 | if (!deviceFile.good()) |
| 618 | { |
| 619 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 620 | "ipmbChannelUpdateTargetAddress: error opening deviceFile to delete " |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 621 | "sysfs"); |
| 622 | return -1; |
| 623 | } |
| 624 | deviceFile << para; |
| 625 | deviceFile.close(); |
| 626 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 627 | // enable new I2C target driver by command: |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 628 | // echo "ipmb-dev 0x1012" > /sys/bus/i2c/devices/i2c-0/new_device |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 629 | deviceFileName = |
| 630 | "/sys/bus/i2c/devices/i2c-" + std::to_string(ipmbBusId) + "/new_device"; |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 631 | std::ostringstream hex; |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 632 | uint16_t addr = 0x1000 + (newBmcTargetAddr >> 1); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 633 | hex << std::hex << static_cast<uint16_t>(addr); |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 634 | const std::string& addressHexStr = hex.str(); |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 635 | para = "ipmb-dev 0x" + addressHexStr; |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 636 | deviceFile.open(deviceFileName, std::ios::out); |
| 637 | if (!deviceFile.good()) |
| 638 | { |
| 639 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 640 | "ipmbChannelUpdateTargetAddress: error opening deviceFile to create " |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 641 | "sysfs"); |
| 642 | return -1; |
| 643 | } |
| 644 | deviceFile << para; |
| 645 | deviceFile.close(); |
| 646 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 647 | // open fd to i2c target device |
| 648 | std::string ipmbI2cTargetStr = "/dev/ipmb-" + std::to_string(ipmbBusId); |
| 649 | ipmbi2cTargetFd = open(ipmbI2cTargetStr.c_str(), O_RDWR | O_NONBLOCK); |
| 650 | if (ipmbi2cTargetFd < 0) |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 651 | { |
| 652 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 653 | "ipmbChannelInit: error opening ipmbI2cTarget"); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 654 | return -1; |
| 655 | } |
| 656 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 657 | // start to receive i2c data as target |
| 658 | i2cTargetDescriptor.assign(ipmbi2cTargetFd); |
| 659 | i2cTargetDescriptor.async_wait( |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 660 | boost::asio::posix::descriptor_base::wait_read, |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 661 | [this](const boost::system::error_code& ec) { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 662 | if (ec) |
| 663 | { |
| 664 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 665 | "Error: processI2cEvent()"); |
| 666 | return; |
| 667 | } |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 668 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 669 | processI2cEvent(); |
| 670 | }); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 671 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 672 | ipmbBmcTargetAddress = newBmcTargetAddr; |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 673 | |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | uint8_t IpmbChannel::getBusId() |
| 678 | { |
| 679 | return ipmbBusId; |
| 680 | } |
| 681 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 682 | uint8_t IpmbChannel::getBmcTargetAddress() |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 683 | { |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 684 | return ipmbBmcTargetAddress; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 687 | uint8_t IpmbChannel::getRqTargetAddress() |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 688 | { |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 689 | return ipmbRqTargetAddress; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 692 | uint8_t IpmbChannel::getDevIndex() |
| 693 | { |
| 694 | return channelIdx >> 2; |
| 695 | } |
| 696 | |
| 697 | uint8_t IpmbChannel::getChannelIdx() |
| 698 | { |
| 699 | return channelIdx; |
| 700 | } |
| 701 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 702 | ipmbChannelType IpmbChannel::getChannelType() |
| 703 | { |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 704 | return static_cast<ipmbChannelType>((channelIdx & 3)); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | void IpmbChannel::addFilter(const uint8_t respNetFn, const uint8_t cmd) |
| 708 | { |
| 709 | if (commandFilter) |
| 710 | { |
| 711 | commandFilter->addFilter(respNetFn, cmd); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | 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] | 716 | IpmbChannel::requestAdd(boost::asio::yield_context& yield, |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 717 | std::shared_ptr<IpmbRequest> request) |
| 718 | { |
| 719 | makeRequestValid(request); |
| 720 | |
Patrick Williams | 9ae8893 | 2024-07-13 15:39:58 -0500 | [diff] [blame] | 721 | std::vector<uint8_t> buffer{}; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 722 | if (request->ipmbToi2cConstruct(buffer) != 0) |
| 723 | { |
| 724 | return returnStatus(ipmbResponseStatus::error); |
| 725 | } |
| 726 | |
| 727 | for (int i = 0; i < ipmbNumberOfTries; i++) |
| 728 | { |
| 729 | boost::system::error_code ec; |
Jae Hyun Yoo | 25e85c7 | 2019-03-05 14:28:13 -0800 | [diff] [blame] | 730 | int i2cRetryCnt = 0; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 731 | |
Jae Hyun Yoo | 25e85c7 | 2019-03-05 14:28:13 -0800 | [diff] [blame] | 732 | for (; i2cRetryCnt < ipmbI2cNumberOfRetries; i2cRetryCnt++) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 733 | { |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 734 | boost::asio::async_write(i2cTargetDescriptor, |
Vijay Khemka | 37a7eac | 2019-12-06 13:52:28 -0800 | [diff] [blame] | 735 | boost::asio::buffer(buffer), yield[ec]); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 736 | |
| 737 | if (ec) |
| 738 | { |
Jae Hyun Yoo | 25e85c7 | 2019-03-05 14:28:13 -0800 | [diff] [blame] | 739 | continue; // retry |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 740 | } |
| 741 | break; |
| 742 | } |
| 743 | |
Jae Hyun Yoo | 25e85c7 | 2019-03-05 14:28:13 -0800 | [diff] [blame] | 744 | if (i2cRetryCnt == ipmbI2cNumberOfRetries) |
| 745 | { |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 746 | std::string msgToLog = |
| 747 | "requestAdd: Sent to I2C failed after retries." |
| 748 | " busId=" + |
| 749 | std::to_string(ipmbBusId) + ", error=" + ec.message(); |
Jae Hyun Yoo | 25e85c7 | 2019-03-05 14:28:13 -0800 | [diff] [blame] | 750 | phosphor::logging::log<phosphor::logging::level::INFO>( |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 751 | msgToLog.c_str()); |
Jae Hyun Yoo | 25e85c7 | 2019-03-05 14:28:13 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 754 | request->timer->expires_after( |
| 755 | std::chrono::milliseconds(ipmbRequestRetryTimeout)); |
| 756 | request->timer->async_wait(yield[ec]); |
| 757 | |
| 758 | if (ec && ec != boost::asio::error::operation_aborted) |
| 759 | { |
| 760 | // unexpected error - invalidate request and return generic error |
| 761 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 762 | "requestAdd: async_wait error"); |
| 763 | makeRequestInvalid(*request); |
| 764 | return returnStatus(ipmbResponseStatus::error); |
| 765 | } |
| 766 | |
| 767 | if (request->state == ipmbRequestState::matched) |
| 768 | { |
| 769 | // matched response, send it to client application |
| 770 | makeRequestInvalid(*request); |
| 771 | return request->returnMatchedResponse(); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | makeRequestInvalid(*request); |
| 776 | return returnStatus(ipmbResponseStatus::timeout); |
| 777 | } |
| 778 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 779 | static IpmbChannel* getChannel(uint8_t reqChannel) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 780 | { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 781 | auto channel = |
| 782 | std::find_if(ipmbChannels.begin(), ipmbChannels.end(), |
| 783 | [reqChannel](IpmbChannel& channel) { |
| 784 | return channel.getChannelIdx() == reqChannel; |
| 785 | }); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 786 | if (channel != ipmbChannels.end()) |
| 787 | { |
| 788 | return &(*channel); |
| 789 | } |
| 790 | |
| 791 | return nullptr; |
| 792 | } |
| 793 | |
| 794 | static int initializeChannels() |
| 795 | { |
| 796 | std::shared_ptr<IpmbCommandFilter> commandFilter = |
| 797 | std::make_shared<IpmbCommandFilter>(); |
| 798 | |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 799 | constexpr const char* configFilePath = |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 800 | "/usr/share/ipmbbridge/ipmb-channels.json"; |
| 801 | std::ifstream configFile(configFilePath); |
| 802 | if (!configFile.is_open()) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 803 | { |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 804 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 805 | "initializeChannels: Cannot open config path"); |
| 806 | return -1; |
| 807 | } |
| 808 | try |
| 809 | { |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 810 | uint8_t devIndex = 0; |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 811 | auto data = nlohmann::json::parse(configFile, nullptr); |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 812 | for (const auto& channelConfig : data["channels"]) |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 813 | { |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 814 | const std::string& typeConfig = channelConfig["type"]; |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 815 | const std::string& targetPath = channelConfig["slave-path"]; |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 816 | uint8_t bmcAddr = channelConfig["bmc-addr"]; |
| 817 | uint8_t reqAddr = channelConfig["remote-addr"]; |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 818 | |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 819 | ipmbChannelType type = ipmbChannelTypeMap.at(typeConfig); |
| 820 | |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 821 | if (channelConfig.contains("devIndex")) |
| 822 | { |
| 823 | devIndex = channelConfig["devIndex"]; |
| 824 | } |
| 825 | |
| 826 | auto channel = ipmbChannels.emplace( |
| 827 | ipmbChannels.end(), io, bmcAddr, reqAddr, |
| 828 | ((devIndex << 2) | static_cast<uint8_t>(type)), commandFilter); |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 829 | if (channel->ipmbChannelInit(targetPath.c_str()) < 0) |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 830 | { |
| 831 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 832 | "initializeChannels: channel initialization failed"); |
| 833 | return -1; |
| 834 | } |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 835 | } |
| 836 | } |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 837 | catch (const nlohmann::json::exception& e) |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 838 | { |
| 839 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 840 | "initializeChannels: Error parsing config file"); |
| 841 | return -1; |
| 842 | } |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 843 | catch (const std::out_of_range& e) |
Amithash Prasad | 314862d | 2019-03-26 11:14:03 -0700 | [diff] [blame] | 844 | { |
| 845 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 846 | "initializeChannels: Error invalid type"); |
| 847 | return -1; |
| 848 | } |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 849 | return 0; |
| 850 | } |
| 851 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 852 | auto ipmbHandleRequest = |
| 853 | [](boost::asio::yield_context yield, uint8_t reqChannel, uint8_t netfn, |
| 854 | uint8_t lun, uint8_t cmd, std::vector<uint8_t> dataReceived) { |
| 855 | IpmbChannel* channel = getChannel(reqChannel); |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 856 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 857 | if (channel == nullptr) |
| 858 | { |
| 859 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 860 | "ipmbHandleRequest: requested channel does not exist"); |
| 861 | return returnStatus(ipmbResponseStatus::invalid_param); |
| 862 | } |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 863 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 864 | // check outstanding request list for valid sequence number |
| 865 | uint8_t seqNum = 0; |
| 866 | bool seqValid = channel->seqNumGet(seqNum); |
| 867 | if (!seqValid) |
| 868 | { |
| 869 | phosphor::logging::log<phosphor::logging::level::WARNING>( |
| 870 | "ipmbHandleRequest: cannot add more requests to the list"); |
| 871 | return returnStatus(ipmbResponseStatus::busy); |
| 872 | } |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 873 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 874 | uint8_t bmcTargetAddress = channel->getBmcTargetAddress(); |
| 875 | uint8_t rqTargetAddress = channel->getRqTargetAddress(); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 876 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 877 | // construct the request to add it to outstanding request list |
| 878 | std::shared_ptr<IpmbRequest> request = std::make_shared<IpmbRequest>( |
| 879 | rqTargetAddress, netfn, ipmbRsLun, bmcTargetAddress, seqNum, lun, |
| 880 | cmd, dataReceived); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 881 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 882 | if (!request->timer) |
| 883 | { |
| 884 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 885 | "ipmbHandleRequest: timer object does not exist"); |
| 886 | return returnStatus(ipmbResponseStatus::error); |
| 887 | } |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 888 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 889 | return channel->requestAdd(yield, request); |
| 890 | }; |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 891 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 892 | void addUpdateTargetAddrHandler() |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 893 | { |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 894 | // callback to handle dbus signal of updating target addr |
| 895 | std::function<void(sdbusplus::message_t&)> updateTargetAddrHandler = |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 896 | [](sdbusplus::message_t& message) { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 897 | uint8_t reqChannel, busId, targetAddr; |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 898 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 899 | // valid source of signal, check whether from multi-node manager |
| 900 | std::string pathName = message.get_path(); |
| 901 | if (pathName != "/xyz/openbmc_project/MultiNode/Status") |
| 902 | { |
| 903 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 904 | "addUpdateTargetAddrHandler: invalid obj path"); |
| 905 | return; |
| 906 | } |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 907 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 908 | message.read(reqChannel, busId, targetAddr); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 909 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 910 | IpmbChannel* channel = getChannel(reqChannel); |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 911 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 912 | if (channel == nullptr || |
| 913 | channel->getChannelType() != ipmbChannelType::ipmb) |
| 914 | { |
| 915 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 916 | "addUpdateTargetAddrHandler: invalid channel"); |
| 917 | return; |
| 918 | } |
| 919 | if (busId != channel->getBusId()) |
| 920 | { |
| 921 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 922 | "addUpdateTargetAddrHandler: invalid busId"); |
| 923 | return; |
| 924 | } |
| 925 | if (channel->getBmcTargetAddress() == targetAddr) |
| 926 | { |
| 927 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 928 | "addUpdateTargetAddrHandler: channel bmc target addr is " |
| 929 | "unchanged, do nothing"); |
| 930 | return; |
| 931 | } |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 932 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 933 | channel->ipmbChannelUpdateTargetAddress(targetAddr); |
| 934 | }; |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 935 | |
Patrick Williams | 3852f8e | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 936 | static auto match = std::make_unique<sdbusplus::bus::match_t>( |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 937 | static_cast<sdbusplus::bus_t&>(*conn), |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 938 | "type='signal',member='updateBmcSlaveAddr',", updateTargetAddrHandler); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 939 | } |
| 940 | |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 941 | void addSendBroadcastHandler() |
| 942 | { |
| 943 | // callback to handle dbus signal of sending broadcast message |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 944 | std::function<void(sdbusplus::message_t&)> sendBroadcastHandler = |
| 945 | [](sdbusplus::message_t& message) { |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 946 | uint8_t reqChannel, netFn, lun, cmd; |
| 947 | std::vector<uint8_t> dataReceived; |
| 948 | message.read(reqChannel, netFn, lun, cmd, dataReceived); |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 949 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 950 | IpmbChannel* channel = getChannel(reqChannel); |
Kumar Thangavel | 950a2e8 | 2020-07-10 18:07:33 +0530 | [diff] [blame] | 951 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 952 | if (channel == nullptr) |
| 953 | { |
| 954 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 955 | "addSendBroadcastMsgHandler: requested channel does not " |
| 956 | "exist"); |
| 957 | return; |
| 958 | } |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 959 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 960 | uint8_t bmcTargetAddress = channel->getBmcTargetAddress(); |
| 961 | uint8_t seqNum = 0; // seqNum is not used in broadcast msg |
| 962 | uint8_t targetAddr = broadcastAddress; |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 963 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 964 | std::shared_ptr<IpmbRequest> request = |
| 965 | std::make_shared<IpmbRequest>(targetAddr, netFn, ipmbRsLun, |
| 966 | bmcTargetAddress, seqNum, lun, |
| 967 | cmd, dataReceived); |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 968 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 969 | std::shared_ptr<std::vector<uint8_t>> buffer = |
| 970 | std::make_shared<std::vector<uint8_t>>(); |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 971 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 972 | if (request->ipmbToi2cConstruct(*buffer) != 0) |
| 973 | { |
| 974 | return; |
| 975 | } |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 976 | |
Patrick Williams | 9898d61 | 2024-08-16 15:21:15 -0400 | [diff] [blame^] | 977 | channel->ipmbSendI2cFrame(buffer); |
| 978 | }; |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 979 | |
Patrick Williams | 3852f8e | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 980 | static auto match = std::make_unique<sdbusplus::bus::match_t>( |
Patrick Williams | fe0d38a | 2023-03-30 14:51:33 -0500 | [diff] [blame] | 981 | static_cast<sdbusplus::bus_t&>(*conn), |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 982 | "type='signal',member='sendBroadcast',", sendBroadcastHandler); |
| 983 | } |
| 984 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 985 | /** |
| 986 | * @brief Main |
| 987 | */ |
Patrick Williams | 825ad83 | 2023-03-30 15:55:04 -0500 | [diff] [blame] | 988 | int main() |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 989 | { |
| 990 | conn->request_name(ipmbBus); |
| 991 | |
| 992 | auto server = sdbusplus::asio::object_server(conn); |
| 993 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 994 | std::shared_ptr<sdbusplus::asio::dbus_interface> ipmbIface = |
| 995 | server.add_interface(ipmbObj, ipmbDbusIntf); |
| 996 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 997 | ipmbIface->register_method("sendRequest", std::move(ipmbHandleRequest)); |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 998 | ipmbIface->initialize(); |
| 999 | |
| 1000 | if (initializeChannels() < 0) |
| 1001 | { |
| 1002 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1003 | "Error initializeChannels"); |
| 1004 | return -1; |
| 1005 | } |
| 1006 | |
Matt Simmering | 0736e21 | 2023-11-01 16:28:55 -0700 | [diff] [blame] | 1007 | addUpdateTargetAddrHandler(); |
Qiang XU | 8edcf1a | 2019-06-14 22:18:15 +0800 | [diff] [blame] | 1008 | |
Qiang XU | bbfd00a | 2019-06-27 21:10:06 +0800 | [diff] [blame] | 1009 | addSendBroadcastHandler(); |
| 1010 | |
Dawid Frycki | a642a94 | 2018-06-12 10:44:23 -0700 | [diff] [blame] | 1011 | io.run(); |
| 1012 | return 0; |
| 1013 | } |