blob: 78b1c7fc99363327d7cd3667ec33d973ff0cf085 [file] [log] [blame]
Dawid Fryckia642a942018-06-12 10:44:23 -07001/* 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 XU8edcf1a2019-06-14 22:18:15 +080021#include <boost/algorithm/string/replace.hpp>
Ed Tanous1486b8a2023-02-28 13:28:59 -080022#include <boost/asio/io_context.hpp>
Ed Tanous09027c02020-08-31 17:28:36 -070023#include <boost/asio/write.hpp>
Patrick Williamsfe0d38a2023-03-30 14:51:33 -050024#include <nlohmann/json.hpp>
25#include <phosphor-logging/log.hpp>
26
Qiang XU8edcf1a2019-06-14 22:18:15 +080027#include <filesystem>
Amithash Prasad314862d2019-03-26 11:14:03 -070028#include <fstream>
Jason M. Bills7867cd72023-03-29 07:58:02 -070029#include <list>
Dawid Fryckia642a942018-06-12 10:44:23 -070030#include <tuple>
Amithash Prasad314862d2019-03-26 11:14:03 -070031#include <unordered_map>
Dawid Fryckia642a942018-06-12 10:44:23 -070032
Dawid Fryckia642a942018-06-12 10:44:23 -070033/**
34 * @brief Dbus
35 */
Patrick Williamsfe0d38a2023-03-30 14:51:33 -050036static constexpr const char* ipmbBus = "xyz.openbmc_project.Ipmi.Channel.Ipmb";
37static constexpr const char* ipmbObj = "/xyz/openbmc_project/Ipmi/Channel/Ipmb";
38static constexpr const char* ipmbDbusIntf = "org.openbmc.Ipmb";
Dawid Fryckia642a942018-06-12 10:44:23 -070039
Ed Tanous1486b8a2023-02-28 13:28:59 -080040boost::asio::io_context io;
Dawid Fryckia642a942018-06-12 10:44:23 -070041auto conn = std::make_shared<sdbusplus::asio::connection>(io);
42
Dawid Fryckia642a942018-06-12 10:44:23 -070043static std::list<IpmbChannel> ipmbChannels;
Amithash Prasad314862d2019-03-26 11:14:03 -070044static const std::unordered_map<std::string, ipmbChannelType>
45 ipmbChannelTypeMap = {{"me", ipmbChannelType::me},
46 {"ipmb", ipmbChannelType::ipmb}};
Dawid Fryckia642a942018-06-12 10:44:23 -070047
48/**
49 * @brief Ipmb request class methods
50 */
51IpmbRequest::IpmbRequest()
52{
53 data.reserve(ipmbMaxDataSize);
54}
55
56IpmbRequest::IpmbRequest(uint8_t address, uint8_t netFn, uint8_t rsLun,
57 uint8_t rqSA, uint8_t seq, uint8_t rqLun, uint8_t cmd,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -050058 const std::vector<uint8_t>& inputData) :
Dawid Fryckia642a942018-06-12 10:44:23 -070059 address(address),
60 netFn(netFn), rsLun(rsLun), rqSA(rqSA), seq(seq), rqLun(rqLun), cmd(cmd),
61 timer(io)
62{
63 data.reserve(ipmbMaxDataSize);
64 state = ipmbRequestState::invalid;
65
66 if (inputData.size() > 0)
67 {
68 data = std::move(inputData);
69 }
70}
71
Patrick Williamsfe0d38a2023-03-30 14:51:33 -050072void IpmbRequest::i2cToIpmbConstruct(IPMB_HEADER* ipmbBuffer,
Dawid Fryckia642a942018-06-12 10:44:23 -070073 size_t bufferLength)
74{
75 // constructing ipmb request from i2c buffer
76 netFn = ipmbNetFnGet(ipmbBuffer->Header.Req.rsNetFnLUN);
77 rsLun = ipmbLunFromNetFnLunGet(ipmbBuffer->Header.Req.rsNetFnLUN);
78 rqSA = ipmbBuffer->Header.Req.rqSA;
79 seq = ipmbSeqGet(ipmbBuffer->Header.Req.rqSeqLUN);
80 rqLun = ipmbLunFromSeqLunGet(ipmbBuffer->Header.Req.rqSeqLUN);
81 cmd = ipmbBuffer->Header.Req.cmd;
82
Patrick Williamsfe0d38a2023-03-30 14:51:33 -050083 size_t dataLength = bufferLength -
84 (ipmbConnectionHeaderLength +
85 ipmbRequestDataHeaderLength + ipmbChecksumSize);
Dawid Fryckia642a942018-06-12 10:44:23 -070086
87 if (dataLength > 0)
88 {
89 data.insert(data.end(), ipmbBuffer->Header.Req.data,
90 &ipmbBuffer->Header.Req.data[dataLength]);
91 }
92}
93
Patrick Williamsfe0d38a2023-03-30 14:51:33 -050094int IpmbRequest::ipmbToi2cConstruct(std::vector<uint8_t>& buffer)
Dawid Fryckia642a942018-06-12 10:44:23 -070095{
Vijay Khemka37a7eac2019-12-06 13:52:28 -080096 /* Add one byte for length byte as per required by driver */
97 size_t bufferLength = 1 + data.size() + ipmbRequestDataHeaderLength +
Dawid Fryckia642a942018-06-12 10:44:23 -070098 ipmbConnectionHeaderLength + ipmbChecksumSize;
99
100 if (bufferLength > ipmbMaxFrameLength)
101 {
102 return -1;
103 }
104
105 buffer.resize(bufferLength);
106 static_assert(ipmbMaxFrameLength >= sizeof(IPMB_HEADER));
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500107 IPMB_PKT* ipmbPkt = reinterpret_cast<IPMB_PKT*>(buffer.data());
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800108 ipmbPkt->len = bufferLength - 1;
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500109 IPMB_HEADER* ipmbBuffer = &(ipmbPkt->hdr);
Dawid Fryckia642a942018-06-12 10:44:23 -0700110
111 // constructing buffer from ipmb request
112 ipmbBuffer->Header.Req.address = address;
113 ipmbBuffer->Header.Req.rsNetFnLUN = ipmbNetFnLunSet(netFn, rsLun);
114 ipmbBuffer->Header.Req.rqSA = rqSA;
115 ipmbBuffer->Header.Req.rqSeqLUN = ipmbSeqLunSet(seq, rqLun);
116 ipmbBuffer->Header.Req.cmd = cmd;
117
Qiang XUbbfd00a2019-06-27 21:10:06 +0800118 ipmbBuffer->Header.Req.checksum1 = ipmbChecksumCompute(
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500119 (uint8_t*)ipmbBuffer, ipmbConnectionHeaderLength - ipmbChecksumSize);
Dawid Fryckia642a942018-06-12 10:44:23 -0700120
121 if (data.size() > 0)
122 {
123 std::copy(data.begin(), data.end(), ipmbBuffer->Header.Req.data);
124 }
125
126 buffer[bufferLength - ipmbChecksumSize] =
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500127 ipmbChecksumCompute((uint8_t*)ipmbBuffer + ipmbChecksum2StartOffset,
Dawid Fryckia642a942018-06-12 10:44:23 -0700128 (ipmbRequestDataHeaderLength + data.size()));
129
130 return 0;
131}
132
133std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
134 IpmbRequest::returnMatchedResponse()
135{
136 return std::make_tuple(
137 static_cast<int>(ipmbResponseStatus::success), matchedResponse->netFn,
138 matchedResponse->rsLun, matchedResponse->cmd,
139 matchedResponse->completionCode, matchedResponse->data);
140}
141
142static std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
143 returnStatus(ipmbResponseStatus status)
144{
145 // we only want to send status here, other fields are not relevant
146 return std::make_tuple(static_cast<int>(status), 0, 0, 0, 0,
147 std::vector<uint8_t>(0));
148}
149
Dawid Fryckia642a942018-06-12 10:44:23 -0700150/**
151 * @brief Ipmb response class methods
152 */
153IpmbResponse::IpmbResponse()
154{
155 data.reserve(ipmbMaxDataSize);
156}
157
158IpmbResponse::IpmbResponse(uint8_t address, uint8_t netFn, uint8_t rqLun,
159 uint8_t rsSA, uint8_t seq, uint8_t rsLun,
160 uint8_t cmd, uint8_t completionCode,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500161 const std::vector<uint8_t>& inputData) :
Dawid Fryckia642a942018-06-12 10:44:23 -0700162 address(address),
163 netFn(netFn), rqLun(rqLun), rsSA(rsSA), seq(seq), rsLun(rsLun), cmd(cmd),
164 completionCode(completionCode)
165{
166 data.reserve(ipmbMaxDataSize);
167
168 if (inputData.size() > 0)
169 {
170 data = std::move(inputData);
171 }
172}
173
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500174void IpmbResponse::i2cToIpmbConstruct(IPMB_HEADER* ipmbBuffer,
Dawid Fryckia642a942018-06-12 10:44:23 -0700175 size_t bufferLength)
176{
177 netFn = ipmbNetFnGet(ipmbBuffer->Header.Resp.rqNetFnLUN);
178 rqLun = ipmbLunFromNetFnLunGet(ipmbBuffer->Header.Resp.rqNetFnLUN);
179 rsSA = ipmbBuffer->Header.Resp.rsSA;
180 seq = ipmbSeqGet(ipmbBuffer->Header.Resp.rsSeqLUN);
181 rsLun = ipmbLunFromSeqLunGet(ipmbBuffer->Header.Resp.rsSeqLUN);
182 cmd = ipmbBuffer->Header.Resp.cmd;
183 completionCode = ipmbBuffer->Header.Resp.completionCode;
184
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500185 size_t dataLength = bufferLength -
186 (ipmbConnectionHeaderLength +
187 ipmbResponseDataHeaderLength + ipmbChecksumSize);
Dawid Fryckia642a942018-06-12 10:44:23 -0700188
189 if (dataLength > 0)
190 {
191 data.insert(data.end(), ipmbBuffer->Header.Resp.data,
192 &ipmbBuffer->Header.Resp.data[dataLength]);
193 }
194}
195
Dawid Frycki8188d762019-04-01 18:03:48 -0700196std::shared_ptr<std::vector<uint8_t>> IpmbResponse::ipmbToi2cConstruct()
Dawid Fryckia642a942018-06-12 10:44:23 -0700197{
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800198 /* Add one byte for length byte as per required by driver */
199 size_t bufferLength = 1 + data.size() + ipmbResponseDataHeaderLength +
Dawid Fryckia642a942018-06-12 10:44:23 -0700200 ipmbConnectionHeaderLength + ipmbChecksumSize;
201
202 if (bufferLength > ipmbMaxFrameLength)
203 {
Dawid Frycki8188d762019-04-01 18:03:48 -0700204 return nullptr;
Dawid Fryckia642a942018-06-12 10:44:23 -0700205 }
206
Dawid Frycki8188d762019-04-01 18:03:48 -0700207 std::shared_ptr<std::vector<uint8_t>> buffer =
208 std::make_shared<std::vector<uint8_t>>(bufferLength);
209
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500210 IPMB_PKT* ipmbPkt = reinterpret_cast<IPMB_PKT*>(buffer->data());
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800211 ipmbPkt->len = bufferLength - 1;
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500212 IPMB_HEADER* ipmbBuffer = &(ipmbPkt->hdr);
Dawid Fryckia642a942018-06-12 10:44:23 -0700213
214 ipmbBuffer->Header.Resp.address = address;
215 ipmbBuffer->Header.Resp.rqNetFnLUN = ipmbNetFnLunSet(netFn, rqLun);
216 ipmbBuffer->Header.Resp.rsSA = rsSA;
217 ipmbBuffer->Header.Resp.rsSeqLUN = ipmbSeqLunSet(seq, rsLun);
218 ipmbBuffer->Header.Resp.cmd = cmd;
219 ipmbBuffer->Header.Resp.completionCode = completionCode;
220
221 ipmbBuffer->Header.Resp.checksum1 = ipmbChecksumCompute(
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500222 (uint8_t*)ipmbBuffer, ipmbConnectionHeaderLength - ipmbChecksumSize);
Dawid Fryckia642a942018-06-12 10:44:23 -0700223
224 if (data.size() > 0)
225 {
226 std::copy(data.begin(), data.end(), ipmbBuffer->Header.Resp.data);
227 }
228
Dawid Frycki8188d762019-04-01 18:03:48 -0700229 (*buffer)[bufferLength - ipmbChecksumSize] =
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500230 ipmbChecksumCompute((uint8_t*)ipmbBuffer + ipmbChecksum2StartOffset,
Dawid Fryckia642a942018-06-12 10:44:23 -0700231 (ipmbResponseDataHeaderLength + data.size()));
232
Dawid Frycki8188d762019-04-01 18:03:48 -0700233 return buffer;
Dawid Fryckia642a942018-06-12 10:44:23 -0700234}
235
236bool IpmbCommandFilter::isBlocked(const uint8_t reqNetFn, const uint8_t cmd)
237{
238 auto blockedCmd = unhandledCommands.find({reqNetFn, cmd});
239
240 if (blockedCmd != unhandledCommands.end())
241 {
242 return true;
243 }
244
245 return false;
246}
247
248void IpmbCommandFilter::addFilter(const uint8_t reqNetFn, const uint8_t cmd)
249{
250 if (unhandledCommands.insert({reqNetFn, cmd}).second)
251 {
252 phosphor::logging::log<phosphor::logging::level::INFO>(
253 "addFilter: added command to filter",
254 phosphor::logging::entry("netFn = %d", reqNetFn),
255 phosphor::logging::entry("cmd = %d", cmd));
256 }
257}
258
259/**
260 * @brief Ipmb channel
261 */
Qiang XUbbfd00a2019-06-27 21:10:06 +0800262void IpmbChannel::ipmbSendI2cFrame(std::shared_ptr<std::vector<uint8_t>> buffer,
Dawid Fryckia642a942018-06-12 10:44:23 -0700263 size_t retriesAttempted = 0)
264{
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500265 IPMB_PKT* ipmbPkt = reinterpret_cast<IPMB_PKT*>(buffer->data());
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800266 uint8_t targetAddr = ipmbIsResponse(&(ipmbPkt->hdr))
267 ? ipmbPkt->hdr.Header.Resp.address
268 : ipmbPkt->hdr.Header.Req.address;
269 boost::asio::async_write(
270 i2cSlaveDescriptor, boost::asio::buffer(*buffer),
271 [this, buffer, retriesAttempted,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500272 targetAddr](const boost::system::error_code& ec, size_t bytesSent) {
273 if (ec)
274 {
275 size_t currentRetryCnt = retriesAttempted;
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800276
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500277 if (currentRetryCnt > ipmbI2cNumberOfRetries)
278 {
279 std::string msgToLog =
280 "ipmbSendI2cFrame: send to I2C failed after retries."
281 " busId=" +
282 std::to_string(ipmbBusId) +
283 ", targetAddr=" + std::to_string(targetAddr) +
284 ", error=" + ec.message();
285 phosphor::logging::log<phosphor::logging::level::ERR>(
286 msgToLog.c_str());
287 return;
Dawid Fryckia642a942018-06-12 10:44:23 -0700288 }
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500289 currentRetryCnt++;
290 ipmbSendI2cFrame(buffer, currentRetryCnt);
291 }
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800292 });
Dawid Fryckia642a942018-06-12 10:44:23 -0700293}
294
295/**
296 * @brief Ipmb Outstanding Requests
297 */
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500298void IpmbChannel::makeRequestInvalid(IpmbRequest& request)
Dawid Fryckia642a942018-06-12 10:44:23 -0700299{
300 // change request state to invalid and remove it from outstanding requests
301 // list
302 request.state = ipmbRequestState::invalid;
303 outstandingRequests[request.seq] = nullptr;
304}
305
306void IpmbChannel::makeRequestValid(std::shared_ptr<IpmbRequest> request)
307{
308 // change request state to valid and add it to outstanding requests list
309 request->state = ipmbRequestState::valid;
310 outstandingRequests[request->seq] = request;
311}
312
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500313bool IpmbChannel::seqNumGet(uint8_t& seq)
Dawid Fryckia642a942018-06-12 10:44:23 -0700314{
315 static uint8_t seqNum = 0;
316
317 for (int i = 0; i < ipmbMaxOutstandingRequestsCount; i++)
318 {
319 seqNum = ++seqNum & ipmbSeqMask;
320 if (seqNum == ipmbMaxOutstandingRequestsCount)
321 {
322 seqNum = 0;
323 }
324
325 if (outstandingRequests[seqNum] == nullptr)
326 {
327 seq = seqNum;
328 return true;
329 }
330 }
331
332 return false;
333}
334
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500335void IpmbChannel::responseMatch(std::unique_ptr<IpmbResponse>& response)
Dawid Fryckia642a942018-06-12 10:44:23 -0700336{
337 std::shared_ptr<IpmbRequest> request = outstandingRequests[response->seq];
338
339 if (request != nullptr)
340 {
341 if (((ipmbRespNetFn(request->netFn)) == (response->netFn)) &&
342 ((request->rqLun) == (response->rqLun)) &&
343 ((request->rsLun) == (response->rsLun)) &&
344 ((request->cmd) == (response->cmd)))
345 {
346 // match, response is corresponding to previously sent request
347 request->state = ipmbRequestState::matched;
348 request->timer->cancel();
349 request->matchedResponse = std::move(response);
350 }
351 }
352}
353
354void IpmbChannel::processI2cEvent()
355{
356 std::array<uint8_t, ipmbMaxFrameLength> buffer{};
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500357 IPMB_PKT* ipmbPkt = reinterpret_cast<IPMB_PKT*>(buffer.data());
358 IPMB_HEADER* ipmbFrame = &(ipmbPkt->hdr);
Dawid Fryckia642a942018-06-12 10:44:23 -0700359
360 lseek(ipmbi2cSlaveFd, 0, SEEK_SET);
361 int r = read(ipmbi2cSlaveFd, buffer.data(), ipmbMaxFrameLength);
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800362
363 /* Substract first byte len size from total frame length */
364 r--;
365
Dawid Fryckia642a942018-06-12 10:44:23 -0700366 if ((r < ipmbMinFrameLength) || (r > ipmbMaxFrameLength))
367 {
368 goto end;
369 }
370
371 // valiate the frame
372 if (!isFrameValid(ipmbFrame, r))
373 {
374 goto end;
375 }
376
Chen Yugang15185ff2020-09-01 09:20:33 +0800377 // if it is message received from ipmb channel, send out dbus signal
378 if (getChannelType() == ipmbChannelType::ipmb)
Qiang XUbbfd00a2019-06-27 21:10:06 +0800379 {
380 auto ipmbMessageReceived = IpmbRequest();
381 ipmbMessageReceived.i2cToIpmbConstruct(ipmbFrame, r);
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500382 sdbusplus::message_t msg = conn->new_signal(ipmbObj, ipmbDbusIntf,
383 "receiveBroadcast");
Qiang XUbbfd00a2019-06-27 21:10:06 +0800384 msg.append(ipmbMessageReceived.netFn, ipmbMessageReceived.cmd,
385 ipmbMessageReceived.data);
386 msg.signal_send();
387 }
388
Dawid Fryckia642a942018-06-12 10:44:23 -0700389 // copy frame to ipmib message buffer
Chen Yugang3e07b9e2020-10-13 16:14:04 +0800390 if (ipmbIsResponse(ipmbFrame))
Dawid Fryckia642a942018-06-12 10:44:23 -0700391 {
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 XUbbfd00a2019-06-27 21:10:06 +0800408 uint8_t rqSA = ipmbFrame->Header.Req.rqSA;
Dawid Fryckia642a942018-06-12 10:44:23 -0700409
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 Fryckia642a942018-06-12 10:44:23 -0700415
416 // prepare generic response
Qiang XUbbfd00a2019-06-27 21:10:06 +0800417 auto ipmbResponse = IpmbResponse(
418 rqSA, ipmbRespNetFn(netFn), lun, ipmbBmcSlaveAddress, seq,
419 ipmbRsLun, cmd, ipmbIpmiInvalidCmd, {});
Dawid Fryckia642a942018-06-12 10:44:23 -0700420
Dawid Frycki8188d762019-04-01 18:03:48 -0700421 auto buffer = ipmbResponse.ipmbToi2cConstruct();
422 if (buffer)
Dawid Fryckia642a942018-06-12 10:44:23 -0700423 {
Qiang XUbbfd00a2019-06-27 21:10:06 +0800424 ipmbSendI2cFrame(buffer);
Dawid Fryckia642a942018-06-12 10:44:23 -0700425 }
426
427 goto end;
428 }
429 }
430
431 auto ipmbMessageReceived = IpmbRequest();
Dawid Fryckia642a942018-06-12 10:44:23 -0700432 ipmbMessageReceived.i2cToIpmbConstruct(ipmbFrame, r);
433
Kumar Thangavel8fe0abe2020-08-19 21:42:23 +0530434 int devId = getDevIndex();
435
Dawid Frycki8188d762019-04-01 18:03:48 -0700436 std::map<std::string, std::variant<int>> options{
Kumar Thangavel8fe0abe2020-08-19 21:42:23 +0530437 {"rqSA", ipmbAddressTo7BitSet(ipmbMessageReceived.rqSA)},
438 {"hostId", devId}};
439
Dawid Frycki8188d762019-04-01 18:03:48 -0700440 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 XUbbfd00a2019-06-27 21:10:06 +0800444 seq{ipmbMessageReceived.seq}, address{ipmbMessageReceived.rqSA}](
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500445 const boost::system::error_code& ec,
446 const IpmiDbusRspType& response) {
447 const auto& [netfn, lun, cmd, cc, payload] = response;
448 if (ec)
449 {
450 phosphor::logging::log<phosphor::logging::level::ERR>(
451 "processI2cEvent: error getting response from IPMI");
452 return;
453 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700454
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500455 uint8_t bmcSlaveAddress = getBmcSlaveAddress();
Dawid Frycki8188d762019-04-01 18:03:48 -0700456
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500457 if (payload.size() > ipmbMaxDataSize)
458 {
459 phosphor::logging::log<phosphor::logging::level::ERR>(
460 "processI2cEvent: response exceeding maximum size");
Dawid Frycki8188d762019-04-01 18:03:48 -0700461
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500462 // prepare generic response
463 auto ipmbResponse = IpmbResponse(
464 address, netfn, rqLun, bmcSlaveAddress, seq, ipmbRsLun, cmd,
465 ipmbIpmiCmdRespNotProvided, {});
Dawid Frycki8188d762019-04-01 18:03:48 -0700466
467 auto buffer = ipmbResponse.ipmbToi2cConstruct();
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500468 if (buffer)
Dawid Frycki8188d762019-04-01 18:03:48 -0700469 {
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500470 ipmbSendI2cFrame(buffer);
Dawid Frycki8188d762019-04-01 18:03:48 -0700471 }
472
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500473 return;
474 }
475
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 = IpmbResponse(address, netfn, rqLun,
492 bmcSlaveAddress, seq, lun, cmd, cc,
493 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);
Dawid Frycki8188d762019-04-01 18:03:48 -0700504 },
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 Fryckia642a942018-06-12 10:44:23 -0700509 }
510
511end:
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800512 i2cSlaveDescriptor.async_wait(
513 boost::asio::posix::descriptor_base::wait_read,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500514 [this](const boost::system::error_code& ec) {
515 if (ec)
516 {
517 phosphor::logging::log<phosphor::logging::level::ERR>(
518 "Error: processI2cEvent()");
519 return;
520 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700521
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500522 processI2cEvent();
Dawid Fryckia642a942018-06-12 10:44:23 -0700523 });
524}
525
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500526IpmbChannel::IpmbChannel(boost::asio::io_context& io,
Dawid Fryckia642a942018-06-12 10:44:23 -0700527 uint8_t ipmbBmcSlaveAddress,
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530528 uint8_t ipmbRqSlaveAddress, uint8_t channelIdx,
Dawid Fryckia642a942018-06-12 10:44:23 -0700529 std::shared_ptr<IpmbCommandFilter> commandFilter) :
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800530 i2cSlaveDescriptor(io),
531 ipmbBmcSlaveAddress(ipmbBmcSlaveAddress),
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530532 ipmbRqSlaveAddress(ipmbRqSlaveAddress), channelIdx(channelIdx),
Dawid Fryckia642a942018-06-12 10:44:23 -0700533 commandFilter(commandFilter)
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500534{}
Dawid Fryckia642a942018-06-12 10:44:23 -0700535
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500536int IpmbChannel::ipmbChannelInit(const char* ipmbI2cSlave)
Dawid Fryckia642a942018-06-12 10:44:23 -0700537{
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800538 // extract bus id from slave path and save
539 std::string ipmbI2cSlaveStr(ipmbI2cSlave);
540 auto findHyphen = ipmbI2cSlaveStr.find("-");
541 std::string busStr = ipmbI2cSlaveStr.substr(findHyphen + 1);
Qiang XU8edcf1a2019-06-14 22:18:15 +0800542 try
543 {
544 ipmbBusId = std::stoi(busStr);
545 }
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500546 catch (const std::invalid_argument&)
Qiang XU8edcf1a2019-06-14 22:18:15 +0800547 {
548 phosphor::logging::log<phosphor::logging::level::ERR>(
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800549 "ipmbChannelInit: invalid bus id in slave-path config");
Qiang XU8edcf1a2019-06-14 22:18:15 +0800550 return -1;
551 }
552
553 // Check if sysfs has device. If not, enable I2C slave driver by command
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800554 // echo "ipmb-dev 0x1010" > /sys/bus/i2c/devices/i2c-0/new_device
Qiang XU8edcf1a2019-06-14 22:18:15 +0800555 bool hasSysfs = std::filesystem::exists(ipmbI2cSlave);
556 if (!hasSysfs)
557 {
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500558 std::string deviceFileName = "/sys/bus/i2c/devices/i2c-" + busStr +
559 "/new_device";
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800560 std::string para = "ipmb-dev 0x1010"; // init with BMC addr 0x20
Qiang XU8edcf1a2019-06-14 22:18:15 +0800561 std::fstream deviceFile;
562 deviceFile.open(deviceFileName, std::ios::out);
563 if (!deviceFile.good())
564 {
565 phosphor::logging::log<phosphor::logging::level::ERR>(
566 "ipmbChannelInit: error opening deviceFile");
567 return -1;
568 }
569 deviceFile << para;
570 deviceFile.close();
571 }
572
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800573 // open fd to i2c slave device for read write
574 ipmbi2cSlaveFd = open(ipmbI2cSlave, O_RDWR | O_NONBLOCK | O_CLOEXEC);
Dawid Fryckia642a942018-06-12 10:44:23 -0700575 if (ipmbi2cSlaveFd < 0)
576 {
577 phosphor::logging::log<phosphor::logging::level::ERR>(
578 "ipmbChannelInit: error opening ipmbI2cSlave");
579 return -1;
580 }
581
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800582 i2cSlaveDescriptor.assign(ipmbi2cSlaveFd);
Dawid Fryckia642a942018-06-12 10:44:23 -0700583
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800584 i2cSlaveDescriptor.async_wait(
585 boost::asio::posix::descriptor_base::wait_read,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500586 [this](const boost::system::error_code& ec) {
587 if (ec)
588 {
589 phosphor::logging::log<phosphor::logging::level::ERR>(
590 "Error: processI2cEvent()");
591 return;
592 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700593
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500594 processI2cEvent();
Dawid Fryckia642a942018-06-12 10:44:23 -0700595 });
596
597 return 0;
598}
599
Qiang XU8edcf1a2019-06-14 22:18:15 +0800600int IpmbChannel::ipmbChannelUpdateSlaveAddress(const uint8_t newBmcSlaveAddr)
601{
602 if (ipmbi2cSlaveFd > 0)
603 {
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800604 i2cSlaveDescriptor.close();
Qiang XU8edcf1a2019-06-14 22:18:15 +0800605 close(ipmbi2cSlaveFd);
606 ipmbi2cSlaveFd = 0;
607 }
608
609 // disable old I2C slave driver by command:
610 // echo "0x1010" > /sys/bus/i2c/devices/i2c-0/delete_device
611 std::string deviceFileName;
612 std::string para;
613 std::fstream deviceFile;
614 deviceFileName = "/sys/bus/i2c/devices/i2c-" + std::to_string(ipmbBusId) +
615 "/delete_device";
616 para = "0x1010"; // align with removed ipmb0 definition in dts file
617 deviceFile.open(deviceFileName, std::ios::out);
618 if (!deviceFile.good())
619 {
620 phosphor::logging::log<phosphor::logging::level::ERR>(
621 "ipmbChannelUpdateSlaveAddress: error opening deviceFile to delete "
622 "sysfs");
623 return -1;
624 }
625 deviceFile << para;
626 deviceFile.close();
627
628 // enable new I2C slave driver by command:
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800629 // echo "ipmb-dev 0x1012" > /sys/bus/i2c/devices/i2c-0/new_device
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500630 deviceFileName = "/sys/bus/i2c/devices/i2c-" + std::to_string(ipmbBusId) +
631 "/new_device";
Qiang XU8edcf1a2019-06-14 22:18:15 +0800632 std::ostringstream hex;
633 uint16_t addr = 0x1000 + (newBmcSlaveAddr >> 1);
634 hex << std::hex << static_cast<uint16_t>(addr);
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500635 const std::string& addressHexStr = hex.str();
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800636 para = "ipmb-dev 0x" + addressHexStr;
Qiang XU8edcf1a2019-06-14 22:18:15 +0800637 deviceFile.open(deviceFileName, std::ios::out);
638 if (!deviceFile.good())
639 {
640 phosphor::logging::log<phosphor::logging::level::ERR>(
641 "ipmbChannelUpdateSlaveAddress: error opening deviceFile to create "
642 "sysfs");
643 return -1;
644 }
645 deviceFile << para;
646 deviceFile.close();
647
648 // open fd to i2c slave device
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800649 std::string ipmbI2cSlaveStr = "/dev/ipmb-" + std::to_string(ipmbBusId);
650 ipmbi2cSlaveFd = open(ipmbI2cSlaveStr.c_str(), O_RDWR | O_NONBLOCK);
Qiang XU8edcf1a2019-06-14 22:18:15 +0800651 if (ipmbi2cSlaveFd < 0)
652 {
653 phosphor::logging::log<phosphor::logging::level::ERR>(
654 "ipmbChannelInit: error opening ipmbI2cSlave");
655 return -1;
656 }
657
658 // start to receive i2c data as slave
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800659 i2cSlaveDescriptor.assign(ipmbi2cSlaveFd);
660 i2cSlaveDescriptor.async_wait(
661 boost::asio::posix::descriptor_base::wait_read,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500662 [this](const boost::system::error_code& ec) {
663 if (ec)
664 {
665 phosphor::logging::log<phosphor::logging::level::ERR>(
666 "Error: processI2cEvent()");
667 return;
668 }
Qiang XU8edcf1a2019-06-14 22:18:15 +0800669
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500670 processI2cEvent();
Qiang XU8edcf1a2019-06-14 22:18:15 +0800671 });
672
Qiang XUbbfd00a2019-06-27 21:10:06 +0800673 ipmbBmcSlaveAddress = newBmcSlaveAddr;
674
Qiang XU8edcf1a2019-06-14 22:18:15 +0800675 return 0;
676}
677
678uint8_t IpmbChannel::getBusId()
679{
680 return ipmbBusId;
681}
682
Dawid Fryckia642a942018-06-12 10:44:23 -0700683uint8_t IpmbChannel::getBmcSlaveAddress()
684{
685 return ipmbBmcSlaveAddress;
686}
687
688uint8_t IpmbChannel::getRqSlaveAddress()
689{
690 return ipmbRqSlaveAddress;
691}
692
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530693uint8_t IpmbChannel::getDevIndex()
694{
695 return channelIdx >> 2;
696}
697
698uint8_t IpmbChannel::getChannelIdx()
699{
700 return channelIdx;
701}
702
Dawid Fryckia642a942018-06-12 10:44:23 -0700703ipmbChannelType IpmbChannel::getChannelType()
704{
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530705 return static_cast<ipmbChannelType>((channelIdx & 3));
Dawid Fryckia642a942018-06-12 10:44:23 -0700706}
707
708void IpmbChannel::addFilter(const uint8_t respNetFn, const uint8_t cmd)
709{
710 if (commandFilter)
711 {
712 commandFilter->addFilter(respNetFn, cmd);
713 }
714}
715
716std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500717 IpmbChannel::requestAdd(boost::asio::yield_context& yield,
Dawid Fryckia642a942018-06-12 10:44:23 -0700718 std::shared_ptr<IpmbRequest> request)
719{
720 makeRequestValid(request);
721
722 std::vector<uint8_t> buffer(0);
723 if (request->ipmbToi2cConstruct(buffer) != 0)
724 {
725 return returnStatus(ipmbResponseStatus::error);
726 }
727
728 for (int i = 0; i < ipmbNumberOfTries; i++)
729 {
730 boost::system::error_code ec;
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800731 int i2cRetryCnt = 0;
Dawid Fryckia642a942018-06-12 10:44:23 -0700732
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800733 for (; i2cRetryCnt < ipmbI2cNumberOfRetries; i2cRetryCnt++)
Dawid Fryckia642a942018-06-12 10:44:23 -0700734 {
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800735 boost::asio::async_write(i2cSlaveDescriptor,
736 boost::asio::buffer(buffer), yield[ec]);
Dawid Fryckia642a942018-06-12 10:44:23 -0700737
738 if (ec)
739 {
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800740 continue; // retry
Dawid Fryckia642a942018-06-12 10:44:23 -0700741 }
742 break;
743 }
744
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800745 if (i2cRetryCnt == ipmbI2cNumberOfRetries)
746 {
Qiang XUbbfd00a2019-06-27 21:10:06 +0800747 std::string msgToLog =
748 "requestAdd: Sent to I2C failed after retries."
749 " busId=" +
750 std::to_string(ipmbBusId) + ", error=" + ec.message();
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800751 phosphor::logging::log<phosphor::logging::level::INFO>(
Qiang XUbbfd00a2019-06-27 21:10:06 +0800752 msgToLog.c_str());
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800753 }
754
Dawid Fryckia642a942018-06-12 10:44:23 -0700755 request->timer->expires_after(
756 std::chrono::milliseconds(ipmbRequestRetryTimeout));
757 request->timer->async_wait(yield[ec]);
758
759 if (ec && ec != boost::asio::error::operation_aborted)
760 {
761 // unexpected error - invalidate request and return generic error
762 phosphor::logging::log<phosphor::logging::level::ERR>(
763 "requestAdd: async_wait error");
764 makeRequestInvalid(*request);
765 return returnStatus(ipmbResponseStatus::error);
766 }
767
768 if (request->state == ipmbRequestState::matched)
769 {
770 // matched response, send it to client application
771 makeRequestInvalid(*request);
772 return request->returnMatchedResponse();
773 }
774 }
775
776 makeRequestInvalid(*request);
777 return returnStatus(ipmbResponseStatus::timeout);
778}
779
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500780static IpmbChannel* getChannel(uint8_t reqChannel)
Dawid Fryckia642a942018-06-12 10:44:23 -0700781{
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500782 auto channel = std::find_if(ipmbChannels.begin(), ipmbChannels.end(),
783 [reqChannel](IpmbChannel& channel) {
784 return channel.getChannelIdx() == reqChannel;
785 });
Dawid Fryckia642a942018-06-12 10:44:23 -0700786 if (channel != ipmbChannels.end())
787 {
788 return &(*channel);
789 }
790
791 return nullptr;
792}
793
794static int initializeChannels()
795{
796 std::shared_ptr<IpmbCommandFilter> commandFilter =
797 std::make_shared<IpmbCommandFilter>();
798
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500799 constexpr const char* configFilePath =
Amithash Prasad314862d2019-03-26 11:14:03 -0700800 "/usr/share/ipmbbridge/ipmb-channels.json";
801 std::ifstream configFile(configFilePath);
802 if (!configFile.is_open())
Dawid Fryckia642a942018-06-12 10:44:23 -0700803 {
Amithash Prasad314862d2019-03-26 11:14:03 -0700804 phosphor::logging::log<phosphor::logging::level::ERR>(
805 "initializeChannels: Cannot open config path");
806 return -1;
807 }
808 try
809 {
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530810 uint8_t devIndex = 0;
Amithash Prasad314862d2019-03-26 11:14:03 -0700811 auto data = nlohmann::json::parse(configFile, nullptr);
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500812 for (const auto& channelConfig : data["channels"])
Dawid Fryckia642a942018-06-12 10:44:23 -0700813 {
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500814 const std::string& typeConfig = channelConfig["type"];
815 const std::string& slavePath = channelConfig["slave-path"];
Amithash Prasad314862d2019-03-26 11:14:03 -0700816 uint8_t bmcAddr = channelConfig["bmc-addr"];
817 uint8_t reqAddr = channelConfig["remote-addr"];
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530818
Amithash Prasad314862d2019-03-26 11:14:03 -0700819 ipmbChannelType type = ipmbChannelTypeMap.at(typeConfig);
820
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530821 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);
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800829 if (channel->ipmbChannelInit(slavePath.c_str()) < 0)
Amithash Prasad314862d2019-03-26 11:14:03 -0700830 {
831 phosphor::logging::log<phosphor::logging::level::ERR>(
832 "initializeChannels: channel initialization failed");
833 return -1;
834 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700835 }
836 }
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500837 catch (const nlohmann::json::exception& e)
Amithash Prasad314862d2019-03-26 11:14:03 -0700838 {
839 phosphor::logging::log<phosphor::logging::level::ERR>(
840 "initializeChannels: Error parsing config file");
841 return -1;
842 }
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500843 catch (const std::out_of_range& e)
Amithash Prasad314862d2019-03-26 11:14:03 -0700844 {
845 phosphor::logging::log<phosphor::logging::level::ERR>(
846 "initializeChannels: Error invalid type");
847 return -1;
848 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700849 return 0;
850}
851
Dawid Fryckia642a942018-06-12 10:44:23 -0700852auto ipmbHandleRequest = [](boost::asio::yield_context yield,
853 uint8_t reqChannel, uint8_t netfn, uint8_t lun,
854 uint8_t cmd, std::vector<uint8_t> dataReceived) {
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500855 IpmbChannel* channel = getChannel(reqChannel);
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530856
Dawid Fryckia642a942018-06-12 10:44:23 -0700857 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 }
863
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 }
873
874 uint8_t bmcSlaveAddress = channel->getBmcSlaveAddress();
875 uint8_t rqSlaveAddress = channel->getRqSlaveAddress();
876
877 // construct the request to add it to outstanding request list
878 std::shared_ptr<IpmbRequest> request = std::make_shared<IpmbRequest>(
879 rqSlaveAddress, netfn, ipmbRsLun, bmcSlaveAddress, seqNum, lun, cmd,
880 dataReceived);
881
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 }
888
889 return channel->requestAdd(yield, request);
890};
891
Qiang XU8edcf1a2019-06-14 22:18:15 +0800892void addUpdateSlaveAddrHandler()
893{
894 // callback to handle dbus signal of updating slave addr
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500895 std::function<void(sdbusplus::message_t&)> updateSlaveAddrHandler =
896 [](sdbusplus::message_t& message) {
897 uint8_t reqChannel, busId, slaveAddr;
Qiang XU8edcf1a2019-06-14 22:18:15 +0800898
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500899 // 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 "addUpdateSlaveAddrHandler: invalid obj path");
905 return;
906 }
Qiang XU8edcf1a2019-06-14 22:18:15 +0800907
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500908 message.read(reqChannel, busId, slaveAddr);
Qiang XU8edcf1a2019-06-14 22:18:15 +0800909
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500910 IpmbChannel* channel = getChannel(reqChannel);
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530911
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500912 if (channel == nullptr ||
913 channel->getChannelType() != ipmbChannelType::ipmb)
914 {
915 phosphor::logging::log<phosphor::logging::level::ERR>(
916 "addUpdateSlaveAddrHandler: invalid channel");
917 return;
918 }
919 if (busId != channel->getBusId())
920 {
921 phosphor::logging::log<phosphor::logging::level::ERR>(
922 "addUpdateSlaveAddrHandler: invalid busId");
923 return;
924 }
925 if (channel->getBmcSlaveAddress() == slaveAddr)
926 {
927 phosphor::logging::log<phosphor::logging::level::INFO>(
928 "addUpdateSlaveAddrHandler: channel bmc slave addr is "
929 "unchanged, do nothing");
930 return;
931 }
Qiang XU8edcf1a2019-06-14 22:18:15 +0800932
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500933 channel->ipmbChannelUpdateSlaveAddress(slaveAddr);
934 };
Qiang XU8edcf1a2019-06-14 22:18:15 +0800935
Patrick Williams3852f8e2022-07-22 19:26:56 -0500936 static auto match = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500937 static_cast<sdbusplus::bus_t&>(*conn),
Qiang XU8edcf1a2019-06-14 22:18:15 +0800938 "type='signal',member='updateBmcSlaveAddr',", updateSlaveAddrHandler);
939}
940
Qiang XUbbfd00a2019-06-27 21:10:06 +0800941void addSendBroadcastHandler()
942{
943 // callback to handle dbus signal of sending broadcast message
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500944 std::function<void(sdbusplus::message_t&)> sendBroadcastHandler =
945 [](sdbusplus::message_t& message) {
946 uint8_t reqChannel, netFn, lun, cmd;
947 std::vector<uint8_t> dataReceived;
948 message.read(reqChannel, netFn, lun, cmd, dataReceived);
Qiang XUbbfd00a2019-06-27 21:10:06 +0800949
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500950 IpmbChannel* channel = getChannel(reqChannel);
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530951
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500952 if (channel == nullptr)
953 {
954 phosphor::logging::log<phosphor::logging::level::ERR>(
955 "addSendBroadcastMsgHandler: requested channel does not "
956 "exist");
957 return;
958 }
Qiang XUbbfd00a2019-06-27 21:10:06 +0800959
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500960 uint8_t bmcSlaveAddress = channel->getBmcSlaveAddress();
961 uint8_t seqNum = 0; // seqNum is not used in broadcast msg
962 uint8_t targetAddr = broadcastAddress;
Qiang XUbbfd00a2019-06-27 21:10:06 +0800963
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500964 std::shared_ptr<IpmbRequest> request = std::make_shared<IpmbRequest>(
965 targetAddr, netFn, ipmbRsLun, bmcSlaveAddress, seqNum, lun, cmd,
966 dataReceived);
Qiang XUbbfd00a2019-06-27 21:10:06 +0800967
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500968 std::shared_ptr<std::vector<uint8_t>> buffer =
969 std::make_shared<std::vector<uint8_t>>();
Qiang XUbbfd00a2019-06-27 21:10:06 +0800970
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500971 if (request->ipmbToi2cConstruct(*buffer) != 0)
972 {
973 return;
974 }
Qiang XUbbfd00a2019-06-27 21:10:06 +0800975
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500976 channel->ipmbSendI2cFrame(buffer);
977 };
Qiang XUbbfd00a2019-06-27 21:10:06 +0800978
Patrick Williams3852f8e2022-07-22 19:26:56 -0500979 static auto match = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500980 static_cast<sdbusplus::bus_t&>(*conn),
Qiang XUbbfd00a2019-06-27 21:10:06 +0800981 "type='signal',member='sendBroadcast',", sendBroadcastHandler);
982}
983
Dawid Fryckia642a942018-06-12 10:44:23 -0700984/**
985 * @brief Main
986 */
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500987int main(int argc, char* argv[])
Dawid Fryckia642a942018-06-12 10:44:23 -0700988{
989 conn->request_name(ipmbBus);
990
991 auto server = sdbusplus::asio::object_server(conn);
992
Dawid Fryckia642a942018-06-12 10:44:23 -0700993 std::shared_ptr<sdbusplus::asio::dbus_interface> ipmbIface =
994 server.add_interface(ipmbObj, ipmbDbusIntf);
995
Dawid Fryckia642a942018-06-12 10:44:23 -0700996 ipmbIface->register_method("sendRequest", std::move(ipmbHandleRequest));
Dawid Fryckia642a942018-06-12 10:44:23 -0700997 ipmbIface->initialize();
998
999 if (initializeChannels() < 0)
1000 {
1001 phosphor::logging::log<phosphor::logging::level::ERR>(
1002 "Error initializeChannels");
1003 return -1;
1004 }
1005
Qiang XU8edcf1a2019-06-14 22:18:15 +08001006 addUpdateSlaveAddrHandler();
1007
Qiang XUbbfd00a2019-06-27 21:10:06 +08001008 addSendBroadcastHandler();
1009
Dawid Fryckia642a942018-06-12 10:44:23 -07001010 io.run();
1011 return 0;
1012}