blob: d35f74414fe6948fd0d4825b7e7ca70382cd4720 [file] [log] [blame]
Dung Caofaf6a6a2020-12-28 04:44:45 +00001/*
2 * Copyright (c) 2021 Ampere Computing LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * This is a daemon that forwards requests and receive responses from SSIF over
17 * the D-Bus IPMI Interface.
18 */
19
20#include <getopt.h>
21#include <linux/ipmi_bmc.h>
22
23#include <CLI/CLI.hpp>
24#include <boost/algorithm/string/replace.hpp>
Ed Tanousd289aea2024-02-06 20:33:26 -080025#include <boost/asio/completion_condition.hpp>
26#include <boost/asio/io_context.hpp>
27#include <boost/asio/read.hpp>
28#include <boost/asio/steady_timer.hpp>
29#include <boost/asio/write.hpp>
Dung Caofaf6a6a2020-12-28 04:44:45 +000030#include <phosphor-logging/log.hpp>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
Patrick Williams8b050c92023-10-20 11:19:32 -050033#include <sdbusplus/timer.hpp>
Dung Caofaf6a6a2020-12-28 04:44:45 +000034
35#include <iostream>
36
Dung Cao51835392022-10-27 04:29:54 +000037/* Max length of ipmi ssif message included netfn and cmd field */
Thang Q. Nguyen4300d212023-05-15 12:22:53 +070038#define IPMI_SSIF_PAYLOAD_MAX 254
Dung Cao51835392022-10-27 04:29:54 +000039
Ed Tanous498b87f2024-02-14 09:07:34 -080040using phosphor::logging::level;
41using phosphor::logging::log;
Dung Caofaf6a6a2020-12-28 04:44:45 +000042
Patrick Williams8b050c92023-10-20 11:19:32 -050043struct ipmi_cmd
44{
45 uint8_t netfn;
46 uint8_t lun;
47 uint8_t cmd;
quang.ampere14480ee2022-10-18 09:04:51 +070048} prev_req_cmd;
49
Dung Caofaf6a6a2020-12-28 04:44:45 +000050static constexpr const char devBase[] = "/dev/ipmi-ssif-host";
51/* SSIF use IPMI SSIF channel */
Patrick Williams8b050c92023-10-20 11:19:32 -050052static constexpr const char* ssifBus =
53 "xyz.openbmc_project.Ipmi.Channel.ipmi_ssif";
54static constexpr const char* ssifObj =
55 "/xyz/openbmc_project/Ipmi/Channel/ipmi_ssif";
Quang Nguyenf2994dc2022-11-25 16:31:49 +070056/* The timer of driver is set to 15 seconds, need to send
57 * response before timeout occurs
58 */
59static constexpr const unsigned int hostReqTimeout = 14000000;
Dung Caofaf6a6a2020-12-28 04:44:45 +000060
Patrick Williams8b050c92023-10-20 11:19:32 -050061class SsifChannel
62{
63 public:
64 static constexpr size_t ssifMessageSize = IPMI_SSIF_PAYLOAD_MAX +
65 sizeof(unsigned int);
66 size_t sizeofLenField = sizeof(unsigned int);
67 static constexpr uint8_t netFnShift = 2;
68 static constexpr uint8_t lunMask = (1 << netFnShift) - 1;
Dung Caofaf6a6a2020-12-28 04:44:45 +000069
Patrick Williams8b050c92023-10-20 11:19:32 -050070 SsifChannel(std::shared_ptr<boost::asio::io_context>& io,
71 std::shared_ptr<sdbusplus::asio::connection>& bus,
72 const std::string& channel, bool verbose,
73 int numberOfReqNotRsp);
74 bool initOK() const
75 {
Ed Tanousfcd0ea42024-02-14 09:15:41 -080076 return dev.is_open();
Patrick Williams8b050c92023-10-20 11:19:32 -050077 }
78 void channelAbort(const char* msg, const boost::system::error_code& ec);
79 void async_read();
80 void processMessage(const boost::system::error_code& ecRd, size_t rlen);
81 int showNumOfReqNotRsp();
Ed Tanousfcd0ea42024-02-14 09:15:41 -080082 boost::asio::posix::stream_descriptor dev;
Dung Caofaf6a6a2020-12-28 04:44:45 +000083
Patrick Williams8b050c92023-10-20 11:19:32 -050084 protected:
85 std::array<uint8_t, ssifMessageSize> xferBuffer;
86 std::shared_ptr<boost::asio::io_context> io;
87 std::shared_ptr<sdbusplus::asio::connection> bus;
88 std::shared_ptr<sdbusplus::asio::object_server> server;
89 bool verbose;
90 /* This variable is always 0 when a request is responsed properly,
91 * any value larger than 0 meaning there is/are request(s) which
92 * not processed properly
93 * */
94 int numberOfReqNotRsp;
Ed Tanous4ae3b9e2024-02-14 08:54:03 -080095
96 boost::asio::steady_timer rspTimer;
Dung Caofaf6a6a2020-12-28 04:44:45 +000097};
98
Quang Nguyenf2994dc2022-11-25 16:31:49 +070099std::unique_ptr<SsifChannel> ssifchannel = nullptr;
100
Patrick Williams8b050c92023-10-20 11:19:32 -0500101SsifChannel::SsifChannel(std::shared_ptr<boost::asio::io_context>& io,
102 std::shared_ptr<sdbusplus::asio::connection>& bus,
103 const std::string& device, bool verbose,
104 int numberOfReqNotRsp) :
Ed Tanousfcd0ea42024-02-14 09:15:41 -0800105 dev(*io),
106 io(io), bus(bus), verbose(verbose), numberOfReqNotRsp(numberOfReqNotRsp),
Ed Tanous4ae3b9e2024-02-14 08:54:03 -0800107 rspTimer(*io)
Dung Caofaf6a6a2020-12-28 04:44:45 +0000108{
Patrick Williams8b050c92023-10-20 11:19:32 -0500109 std::string devName = devBase;
110 if (!device.empty())
111 {
112 devName = device;
113 }
Dung Caofaf6a6a2020-12-28 04:44:45 +0000114
Patrick Williams8b050c92023-10-20 11:19:32 -0500115 // open device
116 int fd = open(devName.c_str(), O_RDWR | O_NONBLOCK);
117 if (fd < 0)
118 {
119 std::string msgToLog = "Couldn't open SSIF driver with flags O_RDWR."
120 " FILENAME=" +
121 devName + " ERROR=" + strerror(errno);
122 log<level::ERR>(msgToLog.c_str());
123 return;
124 }
125 else
126 {
Ed Tanousfcd0ea42024-02-14 09:15:41 -0800127 dev.assign(fd);
Patrick Williams8b050c92023-10-20 11:19:32 -0500128 }
Dung Caofaf6a6a2020-12-28 04:44:45 +0000129
Patrick Williams8b050c92023-10-20 11:19:32 -0500130 async_read();
131 // register interfaces...
132 server = std::make_shared<sdbusplus::asio::object_server>(bus);
133 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
134 server->add_interface(ssifObj, ssifBus);
135 iface->initialize();
Dung Caofaf6a6a2020-12-28 04:44:45 +0000136}
137
Patrick Williams8b050c92023-10-20 11:19:32 -0500138void SsifChannel::channelAbort(const char* msg,
139 const boost::system::error_code& ec)
Dung Caofaf6a6a2020-12-28 04:44:45 +0000140{
Patrick Williams8b050c92023-10-20 11:19:32 -0500141 std::string msgToLog = std::string(msg) + " ERROR=" + ec.message();
142 log<level::ERR>(msgToLog.c_str());
143 // bail; maybe a restart from systemd can clear the error
144 io->stop();
Dung Caofaf6a6a2020-12-28 04:44:45 +0000145}
146
147void SsifChannel::async_read()
148{
Ed Tanousfcd0ea42024-02-14 09:15:41 -0800149 boost::asio::async_read(dev,
Patrick Williams8b050c92023-10-20 11:19:32 -0500150 boost::asio::buffer(xferBuffer, xferBuffer.size()),
151 boost::asio::transfer_at_least(2),
152 [this](const boost::system::error_code& ec,
153 size_t rlen) { processMessage(ec, rlen); });
Dung Caofaf6a6a2020-12-28 04:44:45 +0000154}
155
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700156int SsifChannel::showNumOfReqNotRsp()
157{
Patrick Williams8b050c92023-10-20 11:19:32 -0500158 return numberOfReqNotRsp;
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700159}
160
Ed Tanousd289aea2024-02-06 20:33:26 -0800161void rspTimerHandler(const boost::system::error_code& ec)
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700162{
Ed Tanousd289aea2024-02-06 20:33:26 -0800163 if (ec == boost::asio::error::operation_aborted)
164 {
165 return;
166 }
Patrick Williams8b050c92023-10-20 11:19:32 -0500167 std::vector<uint8_t> rsp;
168 constexpr uint8_t ccResponseNotAvailable = 0xce;
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700169
Patrick Williams8b050c92023-10-20 11:19:32 -0500170 rsp.resize(ssifchannel->sizeofLenField + sizeof(prev_req_cmd.cmd) +
171 sizeof(prev_req_cmd.netfn) + sizeof(ccResponseNotAvailable));
172 std::string msgToLog = "timeout, send response to keep host alive"
173 " netfn=" +
174 std::to_string(prev_req_cmd.netfn) +
175 " lun=" + std::to_string(prev_req_cmd.lun) +
176 " cmd=" + std::to_string(prev_req_cmd.cmd) +
177 " cc=" + std::to_string(ccResponseNotAvailable) +
178 " numberOfReqNotRsp=" +
179 std::to_string(ssifchannel->showNumOfReqNotRsp());
180 log<level::INFO>(msgToLog.c_str());
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700181
Patrick Williams8b050c92023-10-20 11:19:32 -0500182 unsigned int* t = (unsigned int*)&rsp[0];
183 *t = 3;
184 rsp[ssifchannel->sizeofLenField] =
185 ((prev_req_cmd.netfn + 1) << ssifchannel->netFnShift) |
186 (prev_req_cmd.lun & ssifchannel->lunMask);
187 rsp[ssifchannel->sizeofLenField + 1] = prev_req_cmd.cmd;
188 rsp[ssifchannel->sizeofLenField + 2] = ccResponseNotAvailable;
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700189
Patrick Williams8b050c92023-10-20 11:19:32 -0500190 boost::system::error_code ecWr;
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700191
Ed Tanousfcd0ea42024-02-14 09:15:41 -0800192 size_t wlen = boost::asio::write(ssifchannel->dev, boost::asio::buffer(rsp),
193 ecWr);
Patrick Williams8b050c92023-10-20 11:19:32 -0500194 if (ecWr || wlen != rsp.size())
195 {
196 msgToLog =
197 "Failed to send ssif respond message:"
198 " size=" +
199 std::to_string(wlen) + " expect=" + std::to_string(rsp.size()) +
200 " error=" + ecWr.message() +
201 " netfn=" + std::to_string(prev_req_cmd.netfn + 1) +
202 " lun=" + std::to_string(prev_req_cmd.lun) +
203 " cmd=" + std::to_string(rsp[ssifchannel->sizeofLenField + 1]) +
204 " cc=" + std::to_string(ccResponseNotAvailable);
205 log<level::ERR>(msgToLog.c_str());
206 }
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700207}
208
Patrick Williams8b050c92023-10-20 11:19:32 -0500209void SsifChannel::processMessage(const boost::system::error_code& ecRd,
210 size_t rlen)
Dung Caofaf6a6a2020-12-28 04:44:45 +0000211{
Patrick Williams8b050c92023-10-20 11:19:32 -0500212 if (ecRd || rlen < 2)
213 {
214 channelAbort("Failed to read req msg", ecRd);
215 return;
216 }
217 async_read();
Dung Caofaf6a6a2020-12-28 04:44:45 +0000218
Patrick Williams8b050c92023-10-20 11:19:32 -0500219 auto rawIter = xferBuffer.cbegin();
220 auto rawEnd = rawIter + rlen;
221 uint8_t netfn = rawIter[sizeofLenField] >> netFnShift;
222 uint8_t lun = rawIter[sizeofLenField] & lunMask;
223 uint8_t cmd = rawIter[sizeofLenField + 1];
quang.ampere14480ee2022-10-18 09:04:51 +0700224
Patrick Williams8b050c92023-10-20 11:19:32 -0500225 /* keep track of previous request */
226 prev_req_cmd.netfn = netfn;
227 prev_req_cmd.lun = lun;
228 prev_req_cmd.cmd = cmd;
quang.ampere14480ee2022-10-18 09:04:51 +0700229
Patrick Williams8b050c92023-10-20 11:19:32 -0500230 /* there is a request coming */
231 numberOfReqNotRsp++;
232 /* start response timer */
Ed Tanous4ae3b9e2024-02-14 08:54:03 -0800233 rspTimer.expires_after(std::chrono::microseconds(hostReqTimeout));
234 rspTimer.async_wait(rspTimerHandler);
quang.name507782a2022-11-20 17:05:20 +0700235
Patrick Williams8b050c92023-10-20 11:19:32 -0500236 if (verbose)
237 {
238 unsigned int lenRecv;
239 unsigned int* p = (unsigned int*)rawIter;
240 lenRecv = p[0];
241 std::string msgToLog =
242 "Read ssif request message with"
243 " len=" +
244 std::to_string(lenRecv) + " netfn=" + std::to_string(netfn) +
245 " lun=" + std::to_string(lun) + " cmd=" + std::to_string(cmd) +
246 " numberOfReqNotRsp=" + std::to_string(numberOfReqNotRsp);
247 log<level::INFO>(msgToLog.c_str());
248 }
249 // copy out payload
250 std::vector<uint8_t> data(rawIter + sizeofLenField + 2, rawEnd);
251 // non-session bridges still need to pass an empty options map
252 std::map<std::string, std::variant<int>> options;
253 // the response is a tuple because dbus can only return a single value
254 using IpmiDbusRspType =
255 std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>;
256 static constexpr const char ipmiQueueService[] =
257 "xyz.openbmc_project.Ipmi.Host";
258 static constexpr const char ipmiQueuePath[] = "/xyz/openbmc_project/Ipmi";
259 static constexpr const char ipmiQueueIntf[] =
260 "xyz.openbmc_project.Ipmi.Server";
261 static constexpr const char ipmiQueueMethod[] = "execute";
262 /* now, we do not care dbus timeout, since we already have actions
263 * before dbus timeout occurs
264 */
265 static constexpr unsigned int dbusTimeout = 60000000;
266 bus->async_method_call_timed(
267 [this, netfnCap{netfn}, lunCap{lun},
268 cmdCap{cmd}](const boost::system::error_code& ec,
269 const IpmiDbusRspType& response) {
270 std::vector<uint8_t> rsp;
271 const auto& [netfn, lun, cmd, cc, payload] = response;
272 numberOfReqNotRsp--;
273 if (ec)
274 {
275 std::string msgToLog =
276 "ssif<->ipmid bus error:"
277 " netfn=" +
278 std::to_string(netfn) + " lun=" + std::to_string(lun) +
279 " cmd=" + std::to_string(cmd) + " error=" + ec.message();
280 log<level::ERR>(msgToLog.c_str());
281 rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) +
282 sizeof(cc));
283 /* if dbusTimeout, just return and do not send any response
284 * to let host continue with other commands, response here
285 * is potentially make the response duplicated
286 * */
287 return;
288 }
289 else
290 {
291 if ((prev_req_cmd.netfn != (netfn - 1) || prev_req_cmd.lun != lun ||
292 prev_req_cmd.cmd != cmd) ||
293 ((prev_req_cmd.netfn == (netfn - 1) &&
294 prev_req_cmd.lun == lun && prev_req_cmd.cmd == cmd) &&
295 numberOfReqNotRsp != 0))
296 {
297 /* Only send response to the last request command to void
298 * duplicated response which makes host driver confused and
299 * failed to create interface
300 *
301 * Drop responses which are (1) different from the request
302 * (2) parameters are the same as request but handshake flow
303 * are in dupplicate request state
304 * */
305 if (verbose)
306 {
307 std::string msgToLog = "Drop ssif respond message with"
308 " len=" +
309 std::to_string(payload.size() + 3) +
310 " netfn=" + std::to_string(netfn) +
311 " lun=" + std::to_string(lun) +
312 " cmd=" + std::to_string(cmd) +
313 " cc=" + std::to_string(cc) +
314 " numberOfReqNotRsp=" +
315 std::to_string(numberOfReqNotRsp);
316 log<level::INFO>(msgToLog.c_str());
317 }
318 return;
319 }
320 rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) +
321 sizeof(cc) + payload.size());
Dung Caofaf6a6a2020-12-28 04:44:45 +0000322
Patrick Williams8b050c92023-10-20 11:19:32 -0500323 // write the response
324 auto rspIter = rsp.begin();
325 unsigned int* p = (unsigned int*)&rspIter[0];
326 *p = payload.size() + 3;
327 rspIter[sizeofLenField] = (netfn << netFnShift) | (lun & lunMask);
328 rspIter[sizeofLenField + 1] = cmd;
329 rspIter[sizeofLenField + 2] = cc;
330 if (payload.size())
331 {
332 std::copy(payload.cbegin(), payload.cend(),
333 rspIter + sizeofLenField + 3);
334 }
335 }
336 if (verbose)
337 {
338 std::string msgToLog =
339 "Send ssif respond message with"
340 " len=" +
341 std::to_string(payload.size() + 3) +
342 " netfn=" + std::to_string(netfn) +
343 " lun=" + std::to_string(lun) + " cmd=" + std::to_string(cmd) +
344 " cc=" + std::to_string(cc) +
345 " numberOfReqNotRsp=" + std::to_string(numberOfReqNotRsp);
346 log<level::INFO>(msgToLog.c_str());
347 }
348 boost::system::error_code ecWr;
Ed Tanousfcd0ea42024-02-14 09:15:41 -0800349 size_t wlen = boost::asio::write(dev, boost::asio::buffer(rsp), ecWr);
Patrick Williams8b050c92023-10-20 11:19:32 -0500350 if (ecWr || wlen != rsp.size())
351 {
352 std::string msgToLog =
353 "Failed to send ssif respond message:"
354 " size=" +
355 std::to_string(wlen) + " expect=" + std::to_string(rsp.size()) +
356 " error=" + ecWr.message() + " netfn=" + std::to_string(netfn) +
357 " lun=" + std::to_string(lun) + " cmd=" + std::to_string(cmd) +
358 " cc=" + std::to_string(cc);
359 log<level::ERR>(msgToLog.c_str());
360 }
Ed Tanous4ae3b9e2024-02-14 08:54:03 -0800361 rspTimer.cancel();
Patrick Williams8b050c92023-10-20 11:19:32 -0500362 },
363 ipmiQueueService, ipmiQueuePath, ipmiQueueIntf, ipmiQueueMethod,
364 dbusTimeout, netfn, lun, cmd, data, options);
Dung Caofaf6a6a2020-12-28 04:44:45 +0000365}
366
Patrick Williams8b050c92023-10-20 11:19:32 -0500367int main(int argc, char* argv[])
Dung Caofaf6a6a2020-12-28 04:44:45 +0000368{
Patrick Williams8b050c92023-10-20 11:19:32 -0500369 CLI::App app("SSIF IPMI bridge");
370 std::string device;
371 app.add_option("-d,--device", device,
372 "use <DEVICE> file. Default is /dev/ipmi-ssif-host");
373 bool verbose = false;
374 int numberOfReqNotRsp = 0;
375 app.add_option("-v,--verbose", verbose, "print more verbose output");
376 CLI11_PARSE(app, argc, argv);
Dung Caofaf6a6a2020-12-28 04:44:45 +0000377
Patrick Williams8b050c92023-10-20 11:19:32 -0500378 auto io = std::make_shared<boost::asio::io_context>();
Dung Caofaf6a6a2020-12-28 04:44:45 +0000379
Ed Tanousd289aea2024-02-06 20:33:26 -0800380 auto bus = std::make_shared<sdbusplus::asio::connection>(*io);
Patrick Williams8b050c92023-10-20 11:19:32 -0500381 bus->request_name(ssifBus);
382 // Create the SSIF channel, listening on D-Bus and on the SSIF device
383 ssifchannel = make_unique<SsifChannel>(io, bus, device, verbose,
384 numberOfReqNotRsp);
385 if (!ssifchannel->initOK())
386 {
387 return EXIT_FAILURE;
388 }
Patrick Williams8b050c92023-10-20 11:19:32 -0500389 io->run();
Thang Q. Nguyen4300d212023-05-15 12:22:53 +0700390
Patrick Williams8b050c92023-10-20 11:19:32 -0500391 return 0;
Dung Caofaf6a6a2020-12-28 04:44:45 +0000392}