blob: 1907c7568c824eee14a28b5389fa959f01160c39 [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;
Matt Simmering0736e212023-11-01 16:28:55 -0700269 boost::asio::async_write(i2cTargetDescriptor, boost::asio::buffer(*buffer),
Patrick Williams825ad832023-03-30 15:55:04 -0500270 [this, buffer, retriesAttempted,
271 targetAddr](const boost::system::error_code& ec,
272 size_t /* bytesSent */) {
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500273 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 }
Patrick Williams825ad832023-03-30 15:55:04 -0500292 });
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 {
Patrick Williamse523af32023-03-30 15:32:06 -0500319 seqNum = (seqNum + 1) % ipmbMaxOutstandingRequestsCount;
Dawid Fryckia642a942018-06-12 10:44:23 -0700320
321 if (outstandingRequests[seqNum] == nullptr)
322 {
323 seq = seqNum;
324 return true;
325 }
326 }
327
328 return false;
329}
330
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500331void IpmbChannel::responseMatch(std::unique_ptr<IpmbResponse>& response)
Dawid Fryckia642a942018-06-12 10:44:23 -0700332{
333 std::shared_ptr<IpmbRequest> request = outstandingRequests[response->seq];
334
335 if (request != nullptr)
336 {
337 if (((ipmbRespNetFn(request->netFn)) == (response->netFn)) &&
338 ((request->rqLun) == (response->rqLun)) &&
339 ((request->rsLun) == (response->rsLun)) &&
340 ((request->cmd) == (response->cmd)))
341 {
342 // match, response is corresponding to previously sent request
343 request->state = ipmbRequestState::matched;
344 request->timer->cancel();
345 request->matchedResponse = std::move(response);
346 }
347 }
348}
349
350void IpmbChannel::processI2cEvent()
351{
352 std::array<uint8_t, ipmbMaxFrameLength> buffer{};
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500353 IPMB_PKT* ipmbPkt = reinterpret_cast<IPMB_PKT*>(buffer.data());
354 IPMB_HEADER* ipmbFrame = &(ipmbPkt->hdr);
Dawid Fryckia642a942018-06-12 10:44:23 -0700355
Matt Simmering0736e212023-11-01 16:28:55 -0700356 lseek(ipmbi2cTargetFd, 0, SEEK_SET);
357 ssize_t r = read(ipmbi2cTargetFd, buffer.data(), ipmbMaxFrameLength);
Patrick Williams1f5b24b2023-03-30 15:36:03 -0500358
359 // Handle error cases.
360 if (r < 0)
361 {
362 goto end;
363 }
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800364
365 /* Substract first byte len size from total frame length */
366 r--;
367
Dawid Fryckia642a942018-06-12 10:44:23 -0700368 if ((r < ipmbMinFrameLength) || (r > ipmbMaxFrameLength))
369 {
370 goto end;
371 }
372
373 // valiate the frame
374 if (!isFrameValid(ipmbFrame, r))
375 {
376 goto end;
377 }
378
Chen Yugang15185ff2020-09-01 09:20:33 +0800379 // if it is message received from ipmb channel, send out dbus signal
380 if (getChannelType() == ipmbChannelType::ipmb)
Qiang XUbbfd00a2019-06-27 21:10:06 +0800381 {
382 auto ipmbMessageReceived = IpmbRequest();
383 ipmbMessageReceived.i2cToIpmbConstruct(ipmbFrame, r);
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500384 sdbusplus::message_t msg = conn->new_signal(ipmbObj, ipmbDbusIntf,
385 "receiveBroadcast");
Qiang XUbbfd00a2019-06-27 21:10:06 +0800386 msg.append(ipmbMessageReceived.netFn, ipmbMessageReceived.cmd,
387 ipmbMessageReceived.data);
388 msg.signal_send();
389 }
390
Dawid Fryckia642a942018-06-12 10:44:23 -0700391 // copy frame to ipmib message buffer
Chen Yugang3e07b9e2020-10-13 16:14:04 +0800392 if (ipmbIsResponse(ipmbFrame))
Dawid Fryckia642a942018-06-12 10:44:23 -0700393 {
394 std::unique_ptr<IpmbResponse> ipmbMessageReceived =
395 std::make_unique<IpmbResponse>();
396
397 ipmbMessageReceived->i2cToIpmbConstruct(ipmbFrame, r);
398
399 // try to match response with outstanding request
400 responseMatch(ipmbMessageReceived);
401 }
402 else
403 {
404 // if command is blocked - respond with 'invalid command'
405 // completion code
406 if (commandFilter)
407 {
408 uint8_t netFn = ipmbNetFnGet(ipmbFrame->Header.Req.rsNetFnLUN);
409 uint8_t cmd = ipmbFrame->Header.Req.cmd;
Qiang XUbbfd00a2019-06-27 21:10:06 +0800410 uint8_t rqSA = ipmbFrame->Header.Req.rqSA;
Dawid Fryckia642a942018-06-12 10:44:23 -0700411
412 if (commandFilter->isBlocked(netFn, cmd))
413 {
414 uint8_t seq = ipmbSeqGet(ipmbFrame->Header.Req.rqSeqLUN);
415 uint8_t lun =
416 ipmbLunFromSeqLunGet(ipmbFrame->Header.Req.rqSeqLUN);
Dawid Fryckia642a942018-06-12 10:44:23 -0700417
418 // prepare generic response
Qiang XUbbfd00a2019-06-27 21:10:06 +0800419 auto ipmbResponse = IpmbResponse(
Matt Simmering0736e212023-11-01 16:28:55 -0700420 rqSA, ipmbRespNetFn(netFn), lun, ipmbBmcTargetAddress, seq,
Qiang XUbbfd00a2019-06-27 21:10:06 +0800421 ipmbRsLun, cmd, ipmbIpmiInvalidCmd, {});
Dawid Fryckia642a942018-06-12 10:44:23 -0700422
Dawid Frycki8188d762019-04-01 18:03:48 -0700423 auto buffer = ipmbResponse.ipmbToi2cConstruct();
424 if (buffer)
Dawid Fryckia642a942018-06-12 10:44:23 -0700425 {
Qiang XUbbfd00a2019-06-27 21:10:06 +0800426 ipmbSendI2cFrame(buffer);
Dawid Fryckia642a942018-06-12 10:44:23 -0700427 }
428
429 goto end;
430 }
431 }
432
433 auto ipmbMessageReceived = IpmbRequest();
Dawid Fryckia642a942018-06-12 10:44:23 -0700434 ipmbMessageReceived.i2cToIpmbConstruct(ipmbFrame, r);
435
Kumar Thangavel8fe0abe2020-08-19 21:42:23 +0530436 int devId = getDevIndex();
437
Dawid Frycki8188d762019-04-01 18:03:48 -0700438 std::map<std::string, std::variant<int>> options{
Kumar Thangavel8fe0abe2020-08-19 21:42:23 +0530439 {"rqSA", ipmbAddressTo7BitSet(ipmbMessageReceived.rqSA)},
440 {"hostId", devId}};
441
Dawid Frycki8188d762019-04-01 18:03:48 -0700442 using IpmiDbusRspType = std::tuple<uint8_t, uint8_t, uint8_t, uint8_t,
443 std::vector<uint8_t>>;
444 conn->async_method_call(
445 [this, rqLun{ipmbMessageReceived.rqLun},
Qiang XUbbfd00a2019-06-27 21:10:06 +0800446 seq{ipmbMessageReceived.seq}, address{ipmbMessageReceived.rqSA}](
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500447 const boost::system::error_code& ec,
448 const IpmiDbusRspType& response) {
449 const auto& [netfn, lun, cmd, cc, payload] = response;
450 if (ec)
451 {
452 phosphor::logging::log<phosphor::logging::level::ERR>(
453 "processI2cEvent: error getting response from IPMI");
454 return;
455 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700456
Matt Simmering0736e212023-11-01 16:28:55 -0700457 uint8_t bmcTargetAddress = getBmcTargetAddress();
Dawid Frycki8188d762019-04-01 18:03:48 -0700458
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500459 if (payload.size() > ipmbMaxDataSize)
460 {
461 phosphor::logging::log<phosphor::logging::level::ERR>(
462 "processI2cEvent: response exceeding maximum size");
Dawid Frycki8188d762019-04-01 18:03:48 -0700463
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500464 // prepare generic response
465 auto ipmbResponse = IpmbResponse(
Matt Simmering0736e212023-11-01 16:28:55 -0700466 address, netfn, rqLun, bmcTargetAddress, seq, ipmbRsLun,
467 cmd, ipmbIpmiCmdRespNotProvided, {});
Dawid Frycki8188d762019-04-01 18:03:48 -0700468
469 auto buffer = ipmbResponse.ipmbToi2cConstruct();
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500470 if (buffer)
Dawid Frycki8188d762019-04-01 18:03:48 -0700471 {
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500472 ipmbSendI2cFrame(buffer);
Dawid Frycki8188d762019-04-01 18:03:48 -0700473 }
474
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500475 return;
476 }
477
478 if (!(netfn & ipmbNetFnResponseMask))
479 {
480 // we are not expecting request here
481 phosphor::logging::log<phosphor::logging::level::ERR>(
482 "processI2cEvent: got a request instead of response");
483 return;
484 }
485
486 // if command is not supported, add it to filter
487 if (cc == ipmbIpmiInvalidCmd)
488 {
489 addFilter(ipmbReqNetFnFromRespNetFn(netfn), cmd);
490 }
491
492 // payload is empty after constructor invocation
493 auto ipmbResponse = IpmbResponse(address, netfn, rqLun,
Matt Simmering0736e212023-11-01 16:28:55 -0700494 bmcTargetAddress, seq, lun, cmd,
495 cc, payload);
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500496
497 auto buffer = ipmbResponse.ipmbToi2cConstruct();
498 if (!buffer)
499 {
500 phosphor::logging::log<phosphor::logging::level::ERR>(
501 "processI2cEvent: error constructing a request");
502 return;
503 }
504
505 ipmbSendI2cFrame(buffer);
Patrick Williams64067be2023-10-20 11:19:26 -0500506 },
Dawid Frycki8188d762019-04-01 18:03:48 -0700507 "xyz.openbmc_project.Ipmi.Host", "/xyz/openbmc_project/Ipmi",
508 "xyz.openbmc_project.Ipmi.Server", "execute",
509 ipmbMessageReceived.netFn, ipmbMessageReceived.rsLun,
510 ipmbMessageReceived.cmd, ipmbMessageReceived.data, options);
Dawid Fryckia642a942018-06-12 10:44:23 -0700511 }
512
513end:
Matt Simmering0736e212023-11-01 16:28:55 -0700514 i2cTargetDescriptor.async_wait(
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800515 boost::asio::posix::descriptor_base::wait_read,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500516 [this](const boost::system::error_code& ec) {
517 if (ec)
518 {
519 phosphor::logging::log<phosphor::logging::level::ERR>(
520 "Error: processI2cEvent()");
521 return;
522 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700523
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500524 processI2cEvent();
Patrick Williams64067be2023-10-20 11:19:26 -0500525 });
Dawid Fryckia642a942018-06-12 10:44:23 -0700526}
527
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500528IpmbChannel::IpmbChannel(boost::asio::io_context& io,
Matt Simmering0736e212023-11-01 16:28:55 -0700529 uint8_t ipmbBmcTargetAddress,
530 uint8_t ipmbRqTargetAddress, uint8_t channelIdx,
Dawid Fryckia642a942018-06-12 10:44:23 -0700531 std::shared_ptr<IpmbCommandFilter> commandFilter) :
Matt Simmering0736e212023-11-01 16:28:55 -0700532 i2cTargetDescriptor(io),
533 ipmbBmcTargetAddress(ipmbBmcTargetAddress),
534 ipmbRqTargetAddress(ipmbRqTargetAddress), channelIdx(channelIdx),
Dawid Fryckia642a942018-06-12 10:44:23 -0700535 commandFilter(commandFilter)
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500536{}
Dawid Fryckia642a942018-06-12 10:44:23 -0700537
Matt Simmering0736e212023-11-01 16:28:55 -0700538int IpmbChannel::ipmbChannelInit(const char* ipmbI2cTarget)
Dawid Fryckia642a942018-06-12 10:44:23 -0700539{
Matt Simmering0736e212023-11-01 16:28:55 -0700540 // extract bus id from target path and save
541 std::string ipmbI2cTargetStr(ipmbI2cTarget);
542 auto findHyphen = ipmbI2cTargetStr.find("-");
543 std::string busStr = ipmbI2cTargetStr.substr(findHyphen + 1);
Qiang XU8edcf1a2019-06-14 22:18:15 +0800544 try
545 {
546 ipmbBusId = std::stoi(busStr);
547 }
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500548 catch (const std::invalid_argument&)
Qiang XU8edcf1a2019-06-14 22:18:15 +0800549 {
550 phosphor::logging::log<phosphor::logging::level::ERR>(
Matt Simmering0736e212023-11-01 16:28:55 -0700551 "ipmbChannelInit: invalid bus id in target-path config");
Qiang XU8edcf1a2019-06-14 22:18:15 +0800552 return -1;
553 }
554
Matt Simmering0736e212023-11-01 16:28:55 -0700555 // Check if sysfs has device. If not, enable I2C target driver by command
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800556 // echo "ipmb-dev 0x1010" > /sys/bus/i2c/devices/i2c-0/new_device
Matt Simmering0736e212023-11-01 16:28:55 -0700557 bool hasSysfs = std::filesystem::exists(ipmbI2cTarget);
Qiang XU8edcf1a2019-06-14 22:18:15 +0800558 if (!hasSysfs)
559 {
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500560 std::string deviceFileName = "/sys/bus/i2c/devices/i2c-" + busStr +
561 "/new_device";
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800562 std::string para = "ipmb-dev 0x1010"; // init with BMC addr 0x20
Qiang XU8edcf1a2019-06-14 22:18:15 +0800563 std::fstream deviceFile;
564 deviceFile.open(deviceFileName, std::ios::out);
565 if (!deviceFile.good())
566 {
567 phosphor::logging::log<phosphor::logging::level::ERR>(
568 "ipmbChannelInit: error opening deviceFile");
569 return -1;
570 }
571 deviceFile << para;
572 deviceFile.close();
573 }
574
Matt Simmering0736e212023-11-01 16:28:55 -0700575 // open fd to i2c target device for read write
576 ipmbi2cTargetFd = open(ipmbI2cTarget, O_RDWR | O_NONBLOCK | O_CLOEXEC);
577 if (ipmbi2cTargetFd < 0)
Dawid Fryckia642a942018-06-12 10:44:23 -0700578 {
579 phosphor::logging::log<phosphor::logging::level::ERR>(
Matt Simmering0736e212023-11-01 16:28:55 -0700580 "ipmbChannelInit: error opening ipmbI2cTarget");
Dawid Fryckia642a942018-06-12 10:44:23 -0700581 return -1;
582 }
583
Matt Simmering0736e212023-11-01 16:28:55 -0700584 i2cTargetDescriptor.assign(ipmbi2cTargetFd);
Dawid Fryckia642a942018-06-12 10:44:23 -0700585
Matt Simmering0736e212023-11-01 16:28:55 -0700586 i2cTargetDescriptor.async_wait(
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800587 boost::asio::posix::descriptor_base::wait_read,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500588 [this](const boost::system::error_code& ec) {
589 if (ec)
590 {
591 phosphor::logging::log<phosphor::logging::level::ERR>(
592 "Error: processI2cEvent()");
593 return;
594 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700595
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500596 processI2cEvent();
Patrick Williams64067be2023-10-20 11:19:26 -0500597 });
Dawid Fryckia642a942018-06-12 10:44:23 -0700598
599 return 0;
600}
601
Matt Simmering0736e212023-11-01 16:28:55 -0700602int IpmbChannel::ipmbChannelUpdateTargetAddress(const uint8_t newBmcTargetAddr)
Qiang XU8edcf1a2019-06-14 22:18:15 +0800603{
Matt Simmering0736e212023-11-01 16:28:55 -0700604 if (ipmbi2cTargetFd > 0)
Qiang XU8edcf1a2019-06-14 22:18:15 +0800605 {
Matt Simmering0736e212023-11-01 16:28:55 -0700606 i2cTargetDescriptor.close();
607 close(ipmbi2cTargetFd);
608 ipmbi2cTargetFd = 0;
Qiang XU8edcf1a2019-06-14 22:18:15 +0800609 }
610
Matt Simmering0736e212023-11-01 16:28:55 -0700611 // disable old I2C target driver by command:
Qiang XU8edcf1a2019-06-14 22:18:15 +0800612 // echo "0x1010" > /sys/bus/i2c/devices/i2c-0/delete_device
613 std::string deviceFileName;
614 std::string para;
615 std::fstream deviceFile;
616 deviceFileName = "/sys/bus/i2c/devices/i2c-" + std::to_string(ipmbBusId) +
617 "/delete_device";
618 para = "0x1010"; // align with removed ipmb0 definition in dts file
619 deviceFile.open(deviceFileName, std::ios::out);
620 if (!deviceFile.good())
621 {
622 phosphor::logging::log<phosphor::logging::level::ERR>(
Matt Simmering0736e212023-11-01 16:28:55 -0700623 "ipmbChannelUpdateTargetAddress: error opening deviceFile to delete "
Qiang XU8edcf1a2019-06-14 22:18:15 +0800624 "sysfs");
625 return -1;
626 }
627 deviceFile << para;
628 deviceFile.close();
629
Matt Simmering0736e212023-11-01 16:28:55 -0700630 // enable new I2C target driver by command:
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800631 // echo "ipmb-dev 0x1012" > /sys/bus/i2c/devices/i2c-0/new_device
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500632 deviceFileName = "/sys/bus/i2c/devices/i2c-" + std::to_string(ipmbBusId) +
633 "/new_device";
Qiang XU8edcf1a2019-06-14 22:18:15 +0800634 std::ostringstream hex;
Matt Simmering0736e212023-11-01 16:28:55 -0700635 uint16_t addr = 0x1000 + (newBmcTargetAddr >> 1);
Qiang XU8edcf1a2019-06-14 22:18:15 +0800636 hex << std::hex << static_cast<uint16_t>(addr);
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500637 const std::string& addressHexStr = hex.str();
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800638 para = "ipmb-dev 0x" + addressHexStr;
Qiang XU8edcf1a2019-06-14 22:18:15 +0800639 deviceFile.open(deviceFileName, std::ios::out);
640 if (!deviceFile.good())
641 {
642 phosphor::logging::log<phosphor::logging::level::ERR>(
Matt Simmering0736e212023-11-01 16:28:55 -0700643 "ipmbChannelUpdateTargetAddress: error opening deviceFile to create "
Qiang XU8edcf1a2019-06-14 22:18:15 +0800644 "sysfs");
645 return -1;
646 }
647 deviceFile << para;
648 deviceFile.close();
649
Matt Simmering0736e212023-11-01 16:28:55 -0700650 // open fd to i2c target device
651 std::string ipmbI2cTargetStr = "/dev/ipmb-" + std::to_string(ipmbBusId);
652 ipmbi2cTargetFd = open(ipmbI2cTargetStr.c_str(), O_RDWR | O_NONBLOCK);
653 if (ipmbi2cTargetFd < 0)
Qiang XU8edcf1a2019-06-14 22:18:15 +0800654 {
655 phosphor::logging::log<phosphor::logging::level::ERR>(
Matt Simmering0736e212023-11-01 16:28:55 -0700656 "ipmbChannelInit: error opening ipmbI2cTarget");
Qiang XU8edcf1a2019-06-14 22:18:15 +0800657 return -1;
658 }
659
Matt Simmering0736e212023-11-01 16:28:55 -0700660 // start to receive i2c data as target
661 i2cTargetDescriptor.assign(ipmbi2cTargetFd);
662 i2cTargetDescriptor.async_wait(
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800663 boost::asio::posix::descriptor_base::wait_read,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500664 [this](const boost::system::error_code& ec) {
665 if (ec)
666 {
667 phosphor::logging::log<phosphor::logging::level::ERR>(
668 "Error: processI2cEvent()");
669 return;
670 }
Qiang XU8edcf1a2019-06-14 22:18:15 +0800671
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500672 processI2cEvent();
Patrick Williams64067be2023-10-20 11:19:26 -0500673 });
Qiang XU8edcf1a2019-06-14 22:18:15 +0800674
Matt Simmering0736e212023-11-01 16:28:55 -0700675 ipmbBmcTargetAddress = newBmcTargetAddr;
Qiang XUbbfd00a2019-06-27 21:10:06 +0800676
Qiang XU8edcf1a2019-06-14 22:18:15 +0800677 return 0;
678}
679
680uint8_t IpmbChannel::getBusId()
681{
682 return ipmbBusId;
683}
684
Matt Simmering0736e212023-11-01 16:28:55 -0700685uint8_t IpmbChannel::getBmcTargetAddress()
Dawid Fryckia642a942018-06-12 10:44:23 -0700686{
Matt Simmering0736e212023-11-01 16:28:55 -0700687 return ipmbBmcTargetAddress;
Dawid Fryckia642a942018-06-12 10:44:23 -0700688}
689
Matt Simmering0736e212023-11-01 16:28:55 -0700690uint8_t IpmbChannel::getRqTargetAddress()
Dawid Fryckia642a942018-06-12 10:44:23 -0700691{
Matt Simmering0736e212023-11-01 16:28:55 -0700692 return ipmbRqTargetAddress;
Dawid Fryckia642a942018-06-12 10:44:23 -0700693}
694
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530695uint8_t IpmbChannel::getDevIndex()
696{
697 return channelIdx >> 2;
698}
699
700uint8_t IpmbChannel::getChannelIdx()
701{
702 return channelIdx;
703}
704
Dawid Fryckia642a942018-06-12 10:44:23 -0700705ipmbChannelType IpmbChannel::getChannelType()
706{
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530707 return static_cast<ipmbChannelType>((channelIdx & 3));
Dawid Fryckia642a942018-06-12 10:44:23 -0700708}
709
710void IpmbChannel::addFilter(const uint8_t respNetFn, const uint8_t cmd)
711{
712 if (commandFilter)
713 {
714 commandFilter->addFilter(respNetFn, cmd);
715 }
716}
717
718std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500719 IpmbChannel::requestAdd(boost::asio::yield_context& yield,
Dawid Fryckia642a942018-06-12 10:44:23 -0700720 std::shared_ptr<IpmbRequest> request)
721{
722 makeRequestValid(request);
723
724 std::vector<uint8_t> buffer(0);
725 if (request->ipmbToi2cConstruct(buffer) != 0)
726 {
727 return returnStatus(ipmbResponseStatus::error);
728 }
729
730 for (int i = 0; i < ipmbNumberOfTries; i++)
731 {
732 boost::system::error_code ec;
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800733 int i2cRetryCnt = 0;
Dawid Fryckia642a942018-06-12 10:44:23 -0700734
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800735 for (; i2cRetryCnt < ipmbI2cNumberOfRetries; i2cRetryCnt++)
Dawid Fryckia642a942018-06-12 10:44:23 -0700736 {
Matt Simmering0736e212023-11-01 16:28:55 -0700737 boost::asio::async_write(i2cTargetDescriptor,
Vijay Khemka37a7eac2019-12-06 13:52:28 -0800738 boost::asio::buffer(buffer), yield[ec]);
Dawid Fryckia642a942018-06-12 10:44:23 -0700739
740 if (ec)
741 {
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800742 continue; // retry
Dawid Fryckia642a942018-06-12 10:44:23 -0700743 }
744 break;
745 }
746
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800747 if (i2cRetryCnt == ipmbI2cNumberOfRetries)
748 {
Qiang XUbbfd00a2019-06-27 21:10:06 +0800749 std::string msgToLog =
750 "requestAdd: Sent to I2C failed after retries."
751 " busId=" +
752 std::to_string(ipmbBusId) + ", error=" + ec.message();
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800753 phosphor::logging::log<phosphor::logging::level::INFO>(
Qiang XUbbfd00a2019-06-27 21:10:06 +0800754 msgToLog.c_str());
Jae Hyun Yoo25e85c72019-03-05 14:28:13 -0800755 }
756
Dawid Fryckia642a942018-06-12 10:44:23 -0700757 request->timer->expires_after(
758 std::chrono::milliseconds(ipmbRequestRetryTimeout));
759 request->timer->async_wait(yield[ec]);
760
761 if (ec && ec != boost::asio::error::operation_aborted)
762 {
763 // unexpected error - invalidate request and return generic error
764 phosphor::logging::log<phosphor::logging::level::ERR>(
765 "requestAdd: async_wait error");
766 makeRequestInvalid(*request);
767 return returnStatus(ipmbResponseStatus::error);
768 }
769
770 if (request->state == ipmbRequestState::matched)
771 {
772 // matched response, send it to client application
773 makeRequestInvalid(*request);
774 return request->returnMatchedResponse();
775 }
776 }
777
778 makeRequestInvalid(*request);
779 return returnStatus(ipmbResponseStatus::timeout);
780}
781
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500782static IpmbChannel* getChannel(uint8_t reqChannel)
Dawid Fryckia642a942018-06-12 10:44:23 -0700783{
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500784 auto channel = std::find_if(ipmbChannels.begin(), ipmbChannels.end(),
785 [reqChannel](IpmbChannel& channel) {
786 return channel.getChannelIdx() == reqChannel;
787 });
Dawid Fryckia642a942018-06-12 10:44:23 -0700788 if (channel != ipmbChannels.end())
789 {
790 return &(*channel);
791 }
792
793 return nullptr;
794}
795
796static int initializeChannels()
797{
798 std::shared_ptr<IpmbCommandFilter> commandFilter =
799 std::make_shared<IpmbCommandFilter>();
800
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500801 constexpr const char* configFilePath =
Amithash Prasad314862d2019-03-26 11:14:03 -0700802 "/usr/share/ipmbbridge/ipmb-channels.json";
803 std::ifstream configFile(configFilePath);
804 if (!configFile.is_open())
Dawid Fryckia642a942018-06-12 10:44:23 -0700805 {
Amithash Prasad314862d2019-03-26 11:14:03 -0700806 phosphor::logging::log<phosphor::logging::level::ERR>(
807 "initializeChannels: Cannot open config path");
808 return -1;
809 }
810 try
811 {
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530812 uint8_t devIndex = 0;
Amithash Prasad314862d2019-03-26 11:14:03 -0700813 auto data = nlohmann::json::parse(configFile, nullptr);
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500814 for (const auto& channelConfig : data["channels"])
Dawid Fryckia642a942018-06-12 10:44:23 -0700815 {
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500816 const std::string& typeConfig = channelConfig["type"];
Matt Simmering0736e212023-11-01 16:28:55 -0700817 const std::string& targetPath = channelConfig["slave-path"];
Amithash Prasad314862d2019-03-26 11:14:03 -0700818 uint8_t bmcAddr = channelConfig["bmc-addr"];
819 uint8_t reqAddr = channelConfig["remote-addr"];
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530820
Amithash Prasad314862d2019-03-26 11:14:03 -0700821 ipmbChannelType type = ipmbChannelTypeMap.at(typeConfig);
822
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530823 if (channelConfig.contains("devIndex"))
824 {
825 devIndex = channelConfig["devIndex"];
826 }
827
828 auto channel = ipmbChannels.emplace(
829 ipmbChannels.end(), io, bmcAddr, reqAddr,
830 ((devIndex << 2) | static_cast<uint8_t>(type)), commandFilter);
Matt Simmering0736e212023-11-01 16:28:55 -0700831 if (channel->ipmbChannelInit(targetPath.c_str()) < 0)
Amithash Prasad314862d2019-03-26 11:14:03 -0700832 {
833 phosphor::logging::log<phosphor::logging::level::ERR>(
834 "initializeChannels: channel initialization failed");
835 return -1;
836 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700837 }
838 }
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500839 catch (const nlohmann::json::exception& e)
Amithash Prasad314862d2019-03-26 11:14:03 -0700840 {
841 phosphor::logging::log<phosphor::logging::level::ERR>(
842 "initializeChannels: Error parsing config file");
843 return -1;
844 }
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500845 catch (const std::out_of_range& e)
Amithash Prasad314862d2019-03-26 11:14:03 -0700846 {
847 phosphor::logging::log<phosphor::logging::level::ERR>(
848 "initializeChannels: Error invalid type");
849 return -1;
850 }
Dawid Fryckia642a942018-06-12 10:44:23 -0700851 return 0;
852}
853
Dawid Fryckia642a942018-06-12 10:44:23 -0700854auto ipmbHandleRequest = [](boost::asio::yield_context yield,
855 uint8_t reqChannel, uint8_t netfn, uint8_t lun,
856 uint8_t cmd, std::vector<uint8_t> dataReceived) {
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500857 IpmbChannel* channel = getChannel(reqChannel);
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530858
Dawid Fryckia642a942018-06-12 10:44:23 -0700859 if (channel == nullptr)
860 {
861 phosphor::logging::log<phosphor::logging::level::ERR>(
862 "ipmbHandleRequest: requested channel does not exist");
863 return returnStatus(ipmbResponseStatus::invalid_param);
864 }
865
866 // check outstanding request list for valid sequence number
867 uint8_t seqNum = 0;
868 bool seqValid = channel->seqNumGet(seqNum);
869 if (!seqValid)
870 {
871 phosphor::logging::log<phosphor::logging::level::WARNING>(
872 "ipmbHandleRequest: cannot add more requests to the list");
873 return returnStatus(ipmbResponseStatus::busy);
874 }
875
Matt Simmering0736e212023-11-01 16:28:55 -0700876 uint8_t bmcTargetAddress = channel->getBmcTargetAddress();
877 uint8_t rqTargetAddress = channel->getRqTargetAddress();
Dawid Fryckia642a942018-06-12 10:44:23 -0700878
879 // construct the request to add it to outstanding request list
880 std::shared_ptr<IpmbRequest> request = std::make_shared<IpmbRequest>(
Matt Simmering0736e212023-11-01 16:28:55 -0700881 rqTargetAddress, netfn, ipmbRsLun, bmcTargetAddress, seqNum, lun, cmd,
Dawid Fryckia642a942018-06-12 10:44:23 -0700882 dataReceived);
883
884 if (!request->timer)
885 {
886 phosphor::logging::log<phosphor::logging::level::ERR>(
887 "ipmbHandleRequest: timer object does not exist");
888 return returnStatus(ipmbResponseStatus::error);
889 }
890
891 return channel->requestAdd(yield, request);
892};
893
Matt Simmering0736e212023-11-01 16:28:55 -0700894void addUpdateTargetAddrHandler()
Qiang XU8edcf1a2019-06-14 22:18:15 +0800895{
Matt Simmering0736e212023-11-01 16:28:55 -0700896 // callback to handle dbus signal of updating target addr
897 std::function<void(sdbusplus::message_t&)> updateTargetAddrHandler =
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500898 [](sdbusplus::message_t& message) {
Matt Simmering0736e212023-11-01 16:28:55 -0700899 uint8_t reqChannel, busId, targetAddr;
Qiang XU8edcf1a2019-06-14 22:18:15 +0800900
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500901 // valid source of signal, check whether from multi-node manager
902 std::string pathName = message.get_path();
903 if (pathName != "/xyz/openbmc_project/MultiNode/Status")
904 {
905 phosphor::logging::log<phosphor::logging::level::ERR>(
Matt Simmering0736e212023-11-01 16:28:55 -0700906 "addUpdateTargetAddrHandler: invalid obj path");
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500907 return;
908 }
Qiang XU8edcf1a2019-06-14 22:18:15 +0800909
Matt Simmering0736e212023-11-01 16:28:55 -0700910 message.read(reqChannel, busId, targetAddr);
Qiang XU8edcf1a2019-06-14 22:18:15 +0800911
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500912 IpmbChannel* channel = getChannel(reqChannel);
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530913
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500914 if (channel == nullptr ||
915 channel->getChannelType() != ipmbChannelType::ipmb)
916 {
917 phosphor::logging::log<phosphor::logging::level::ERR>(
Matt Simmering0736e212023-11-01 16:28:55 -0700918 "addUpdateTargetAddrHandler: invalid channel");
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500919 return;
920 }
921 if (busId != channel->getBusId())
922 {
923 phosphor::logging::log<phosphor::logging::level::ERR>(
Matt Simmering0736e212023-11-01 16:28:55 -0700924 "addUpdateTargetAddrHandler: invalid busId");
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500925 return;
926 }
Matt Simmering0736e212023-11-01 16:28:55 -0700927 if (channel->getBmcTargetAddress() == targetAddr)
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500928 {
929 phosphor::logging::log<phosphor::logging::level::INFO>(
Matt Simmering0736e212023-11-01 16:28:55 -0700930 "addUpdateTargetAddrHandler: channel bmc target addr is "
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500931 "unchanged, do nothing");
932 return;
933 }
Qiang XU8edcf1a2019-06-14 22:18:15 +0800934
Matt Simmering0736e212023-11-01 16:28:55 -0700935 channel->ipmbChannelUpdateTargetAddress(targetAddr);
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500936 };
Qiang XU8edcf1a2019-06-14 22:18:15 +0800937
Patrick Williams3852f8e2022-07-22 19:26:56 -0500938 static auto match = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500939 static_cast<sdbusplus::bus_t&>(*conn),
Matt Simmering0736e212023-11-01 16:28:55 -0700940 "type='signal',member='updateBmcSlaveAddr',", updateTargetAddrHandler);
Qiang XU8edcf1a2019-06-14 22:18:15 +0800941}
942
Qiang XUbbfd00a2019-06-27 21:10:06 +0800943void addSendBroadcastHandler()
944{
945 // callback to handle dbus signal of sending broadcast message
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500946 std::function<void(sdbusplus::message_t&)> sendBroadcastHandler =
947 [](sdbusplus::message_t& message) {
948 uint8_t reqChannel, netFn, lun, cmd;
949 std::vector<uint8_t> dataReceived;
950 message.read(reqChannel, netFn, lun, cmd, dataReceived);
Qiang XUbbfd00a2019-06-27 21:10:06 +0800951
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500952 IpmbChannel* channel = getChannel(reqChannel);
Kumar Thangavel950a2e82020-07-10 18:07:33 +0530953
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500954 if (channel == nullptr)
955 {
956 phosphor::logging::log<phosphor::logging::level::ERR>(
957 "addSendBroadcastMsgHandler: requested channel does not "
958 "exist");
959 return;
960 }
Qiang XUbbfd00a2019-06-27 21:10:06 +0800961
Matt Simmering0736e212023-11-01 16:28:55 -0700962 uint8_t bmcTargetAddress = channel->getBmcTargetAddress();
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500963 uint8_t seqNum = 0; // seqNum is not used in broadcast msg
964 uint8_t targetAddr = broadcastAddress;
Qiang XUbbfd00a2019-06-27 21:10:06 +0800965
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500966 std::shared_ptr<IpmbRequest> request = std::make_shared<IpmbRequest>(
Matt Simmering0736e212023-11-01 16:28:55 -0700967 targetAddr, netFn, ipmbRsLun, bmcTargetAddress, seqNum, lun, cmd,
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500968 dataReceived);
Qiang XUbbfd00a2019-06-27 21:10:06 +0800969
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500970 std::shared_ptr<std::vector<uint8_t>> buffer =
971 std::make_shared<std::vector<uint8_t>>();
Qiang XUbbfd00a2019-06-27 21:10:06 +0800972
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500973 if (request->ipmbToi2cConstruct(*buffer) != 0)
974 {
975 return;
976 }
Qiang XUbbfd00a2019-06-27 21:10:06 +0800977
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500978 channel->ipmbSendI2cFrame(buffer);
979 };
Qiang XUbbfd00a2019-06-27 21:10:06 +0800980
Patrick Williams3852f8e2022-07-22 19:26:56 -0500981 static auto match = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williamsfe0d38a2023-03-30 14:51:33 -0500982 static_cast<sdbusplus::bus_t&>(*conn),
Qiang XUbbfd00a2019-06-27 21:10:06 +0800983 "type='signal',member='sendBroadcast',", sendBroadcastHandler);
984}
985
Dawid Fryckia642a942018-06-12 10:44:23 -0700986/**
987 * @brief Main
988 */
Patrick Williams825ad832023-03-30 15:55:04 -0500989int main()
Dawid Fryckia642a942018-06-12 10:44:23 -0700990{
991 conn->request_name(ipmbBus);
992
993 auto server = sdbusplus::asio::object_server(conn);
994
Dawid Fryckia642a942018-06-12 10:44:23 -0700995 std::shared_ptr<sdbusplus::asio::dbus_interface> ipmbIface =
996 server.add_interface(ipmbObj, ipmbDbusIntf);
997
Dawid Fryckia642a942018-06-12 10:44:23 -0700998 ipmbIface->register_method("sendRequest", std::move(ipmbHandleRequest));
Dawid Fryckia642a942018-06-12 10:44:23 -0700999 ipmbIface->initialize();
1000
1001 if (initializeChannels() < 0)
1002 {
1003 phosphor::logging::log<phosphor::logging::level::ERR>(
1004 "Error initializeChannels");
1005 return -1;
1006 }
1007
Matt Simmering0736e212023-11-01 16:28:55 -07001008 addUpdateTargetAddrHandler();
Qiang XU8edcf1a2019-06-14 22:18:15 +08001009
Qiang XUbbfd00a2019-06-27 21:10:06 +08001010 addSendBroadcastHandler();
1011
Dawid Fryckia642a942018-06-12 10:44:23 -07001012 io.run();
1013 return 0;
1014}