blob: 5ac0eedd79ce151af4522a8b186c573b4201972e [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
Dung Caofaf6a6a2020-12-28 04:44:45 +000040using namespace phosphor::logging;
41
Patrick Williams8b050c92023-10-20 11:19:32 -050042struct ipmi_cmd
43{
44 uint8_t netfn;
45 uint8_t lun;
46 uint8_t cmd;
quang.ampere14480ee2022-10-18 09:04:51 +070047} prev_req_cmd;
48
Dung Caofaf6a6a2020-12-28 04:44:45 +000049static constexpr const char devBase[] = "/dev/ipmi-ssif-host";
50/* SSIF use IPMI SSIF channel */
Patrick Williams8b050c92023-10-20 11:19:32 -050051static constexpr const char* ssifBus =
52 "xyz.openbmc_project.Ipmi.Channel.ipmi_ssif";
53static constexpr const char* ssifObj =
54 "/xyz/openbmc_project/Ipmi/Channel/ipmi_ssif";
Quang Nguyenf2994dc2022-11-25 16:31:49 +070055/* The timer of driver is set to 15 seconds, need to send
56 * response before timeout occurs
57 */
58static constexpr const unsigned int hostReqTimeout = 14000000;
Dung Caofaf6a6a2020-12-28 04:44:45 +000059
Patrick Williams8b050c92023-10-20 11:19:32 -050060class SsifChannel
61{
62 public:
63 static constexpr size_t ssifMessageSize = IPMI_SSIF_PAYLOAD_MAX +
64 sizeof(unsigned int);
65 size_t sizeofLenField = sizeof(unsigned int);
66 static constexpr uint8_t netFnShift = 2;
67 static constexpr uint8_t lunMask = (1 << netFnShift) - 1;
Dung Caofaf6a6a2020-12-28 04:44:45 +000068
Patrick Williams8b050c92023-10-20 11:19:32 -050069 SsifChannel(std::shared_ptr<boost::asio::io_context>& io,
70 std::shared_ptr<sdbusplus::asio::connection>& bus,
71 const std::string& channel, bool verbose,
72 int numberOfReqNotRsp);
73 bool initOK() const
74 {
75 return !!dev;
76 }
77 void channelAbort(const char* msg, const boost::system::error_code& ec);
78 void async_read();
79 void processMessage(const boost::system::error_code& ecRd, size_t rlen);
80 int showNumOfReqNotRsp();
81 std::unique_ptr<boost::asio::posix::stream_descriptor> dev = nullptr;
Dung Caofaf6a6a2020-12-28 04:44:45 +000082
Patrick Williams8b050c92023-10-20 11:19:32 -050083 protected:
84 std::array<uint8_t, ssifMessageSize> xferBuffer;
85 std::shared_ptr<boost::asio::io_context> io;
86 std::shared_ptr<sdbusplus::asio::connection> bus;
87 std::shared_ptr<sdbusplus::asio::object_server> server;
88 bool verbose;
89 /* This variable is always 0 when a request is responsed properly,
90 * any value larger than 0 meaning there is/are request(s) which
91 * not processed properly
92 * */
93 int numberOfReqNotRsp;
Ed Tanous4ae3b9e2024-02-14 08:54:03 -080094
95 boost::asio::steady_timer rspTimer;
Dung Caofaf6a6a2020-12-28 04:44:45 +000096};
97
Quang Nguyenf2994dc2022-11-25 16:31:49 +070098std::unique_ptr<SsifChannel> ssifchannel = nullptr;
99
Patrick Williams8b050c92023-10-20 11:19:32 -0500100SsifChannel::SsifChannel(std::shared_ptr<boost::asio::io_context>& io,
101 std::shared_ptr<sdbusplus::asio::connection>& bus,
102 const std::string& device, bool verbose,
103 int numberOfReqNotRsp) :
104 io(io),
Ed Tanous4ae3b9e2024-02-14 08:54:03 -0800105 bus(bus), verbose(verbose), numberOfReqNotRsp(numberOfReqNotRsp),
106 rspTimer(*io)
Dung Caofaf6a6a2020-12-28 04:44:45 +0000107{
Patrick Williams8b050c92023-10-20 11:19:32 -0500108 std::string devName = devBase;
109 if (!device.empty())
110 {
111 devName = device;
112 }
Dung Caofaf6a6a2020-12-28 04:44:45 +0000113
Patrick Williams8b050c92023-10-20 11:19:32 -0500114 // open device
115 int fd = open(devName.c_str(), O_RDWR | O_NONBLOCK);
116 if (fd < 0)
117 {
118 std::string msgToLog = "Couldn't open SSIF driver with flags O_RDWR."
119 " FILENAME=" +
120 devName + " ERROR=" + strerror(errno);
121 log<level::ERR>(msgToLog.c_str());
122 return;
123 }
124 else
125 {
126 dev = std::make_unique<boost::asio::posix::stream_descriptor>(*io, fd);
127 }
Dung Caofaf6a6a2020-12-28 04:44:45 +0000128
Patrick Williams8b050c92023-10-20 11:19:32 -0500129 async_read();
130 // register interfaces...
131 server = std::make_shared<sdbusplus::asio::object_server>(bus);
132 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
133 server->add_interface(ssifObj, ssifBus);
134 iface->initialize();
Dung Caofaf6a6a2020-12-28 04:44:45 +0000135}
136
Patrick Williams8b050c92023-10-20 11:19:32 -0500137void SsifChannel::channelAbort(const char* msg,
138 const boost::system::error_code& ec)
Dung Caofaf6a6a2020-12-28 04:44:45 +0000139{
Patrick Williams8b050c92023-10-20 11:19:32 -0500140 std::string msgToLog = std::string(msg) + " ERROR=" + ec.message();
141 log<level::ERR>(msgToLog.c_str());
142 // bail; maybe a restart from systemd can clear the error
143 io->stop();
Dung Caofaf6a6a2020-12-28 04:44:45 +0000144}
145
146void SsifChannel::async_read()
147{
Patrick Williams8b050c92023-10-20 11:19:32 -0500148 boost::asio::async_read(*dev,
149 boost::asio::buffer(xferBuffer, xferBuffer.size()),
150 boost::asio::transfer_at_least(2),
151 [this](const boost::system::error_code& ec,
152 size_t rlen) { processMessage(ec, rlen); });
Dung Caofaf6a6a2020-12-28 04:44:45 +0000153}
154
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700155int SsifChannel::showNumOfReqNotRsp()
156{
Patrick Williams8b050c92023-10-20 11:19:32 -0500157 return numberOfReqNotRsp;
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700158}
159
Ed Tanousd289aea2024-02-06 20:33:26 -0800160void rspTimerHandler(const boost::system::error_code& ec)
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700161{
Ed Tanousd289aea2024-02-06 20:33:26 -0800162 if (ec == boost::asio::error::operation_aborted)
163 {
164 return;
165 }
Patrick Williams8b050c92023-10-20 11:19:32 -0500166 std::vector<uint8_t> rsp;
167 constexpr uint8_t ccResponseNotAvailable = 0xce;
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700168
Patrick Williams8b050c92023-10-20 11:19:32 -0500169 rsp.resize(ssifchannel->sizeofLenField + sizeof(prev_req_cmd.cmd) +
170 sizeof(prev_req_cmd.netfn) + sizeof(ccResponseNotAvailable));
171 std::string msgToLog = "timeout, send response to keep host alive"
172 " netfn=" +
173 std::to_string(prev_req_cmd.netfn) +
174 " lun=" + std::to_string(prev_req_cmd.lun) +
175 " cmd=" + std::to_string(prev_req_cmd.cmd) +
176 " cc=" + std::to_string(ccResponseNotAvailable) +
177 " numberOfReqNotRsp=" +
178 std::to_string(ssifchannel->showNumOfReqNotRsp());
179 log<level::INFO>(msgToLog.c_str());
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700180
Patrick Williams8b050c92023-10-20 11:19:32 -0500181 unsigned int* t = (unsigned int*)&rsp[0];
182 *t = 3;
183 rsp[ssifchannel->sizeofLenField] =
184 ((prev_req_cmd.netfn + 1) << ssifchannel->netFnShift) |
185 (prev_req_cmd.lun & ssifchannel->lunMask);
186 rsp[ssifchannel->sizeofLenField + 1] = prev_req_cmd.cmd;
187 rsp[ssifchannel->sizeofLenField + 2] = ccResponseNotAvailable;
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700188
Patrick Williams8b050c92023-10-20 11:19:32 -0500189 boost::system::error_code ecWr;
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700190
Patrick Williams8b050c92023-10-20 11:19:32 -0500191 size_t wlen = boost::asio::write(*(ssifchannel->dev),
192 boost::asio::buffer(rsp), ecWr);
193 if (ecWr || wlen != rsp.size())
194 {
195 msgToLog =
196 "Failed to send ssif respond message:"
197 " size=" +
198 std::to_string(wlen) + " expect=" + std::to_string(rsp.size()) +
199 " error=" + ecWr.message() +
200 " netfn=" + std::to_string(prev_req_cmd.netfn + 1) +
201 " lun=" + std::to_string(prev_req_cmd.lun) +
202 " cmd=" + std::to_string(rsp[ssifchannel->sizeofLenField + 1]) +
203 " cc=" + std::to_string(ccResponseNotAvailable);
204 log<level::ERR>(msgToLog.c_str());
205 }
Quang Nguyenf2994dc2022-11-25 16:31:49 +0700206}
207
Patrick Williams8b050c92023-10-20 11:19:32 -0500208void SsifChannel::processMessage(const boost::system::error_code& ecRd,
209 size_t rlen)
Dung Caofaf6a6a2020-12-28 04:44:45 +0000210{
Patrick Williams8b050c92023-10-20 11:19:32 -0500211 if (ecRd || rlen < 2)
212 {
213 channelAbort("Failed to read req msg", ecRd);
214 return;
215 }
216 async_read();
Dung Caofaf6a6a2020-12-28 04:44:45 +0000217
Patrick Williams8b050c92023-10-20 11:19:32 -0500218 auto rawIter = xferBuffer.cbegin();
219 auto rawEnd = rawIter + rlen;
220 uint8_t netfn = rawIter[sizeofLenField] >> netFnShift;
221 uint8_t lun = rawIter[sizeofLenField] & lunMask;
222 uint8_t cmd = rawIter[sizeofLenField + 1];
quang.ampere14480ee2022-10-18 09:04:51 +0700223
Patrick Williams8b050c92023-10-20 11:19:32 -0500224 /* keep track of previous request */
225 prev_req_cmd.netfn = netfn;
226 prev_req_cmd.lun = lun;
227 prev_req_cmd.cmd = cmd;
quang.ampere14480ee2022-10-18 09:04:51 +0700228
Patrick Williams8b050c92023-10-20 11:19:32 -0500229 /* there is a request coming */
230 numberOfReqNotRsp++;
231 /* start response timer */
Ed Tanous4ae3b9e2024-02-14 08:54:03 -0800232 rspTimer.expires_after(std::chrono::microseconds(hostReqTimeout));
233 rspTimer.async_wait(rspTimerHandler);
quang.name507782a2022-11-20 17:05:20 +0700234
Patrick Williams8b050c92023-10-20 11:19:32 -0500235 if (verbose)
236 {
237 unsigned int lenRecv;
238 unsigned int* p = (unsigned int*)rawIter;
239 lenRecv = p[0];
240 std::string msgToLog =
241 "Read ssif request message with"
242 " len=" +
243 std::to_string(lenRecv) + " netfn=" + std::to_string(netfn) +
244 " lun=" + std::to_string(lun) + " cmd=" + std::to_string(cmd) +
245 " numberOfReqNotRsp=" + std::to_string(numberOfReqNotRsp);
246 log<level::INFO>(msgToLog.c_str());
247 }
248 // copy out payload
249 std::vector<uint8_t> data(rawIter + sizeofLenField + 2, rawEnd);
250 // non-session bridges still need to pass an empty options map
251 std::map<std::string, std::variant<int>> options;
252 // the response is a tuple because dbus can only return a single value
253 using IpmiDbusRspType =
254 std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>;
255 static constexpr const char ipmiQueueService[] =
256 "xyz.openbmc_project.Ipmi.Host";
257 static constexpr const char ipmiQueuePath[] = "/xyz/openbmc_project/Ipmi";
258 static constexpr const char ipmiQueueIntf[] =
259 "xyz.openbmc_project.Ipmi.Server";
260 static constexpr const char ipmiQueueMethod[] = "execute";
261 /* now, we do not care dbus timeout, since we already have actions
262 * before dbus timeout occurs
263 */
264 static constexpr unsigned int dbusTimeout = 60000000;
265 bus->async_method_call_timed(
266 [this, netfnCap{netfn}, lunCap{lun},
267 cmdCap{cmd}](const boost::system::error_code& ec,
268 const IpmiDbusRspType& response) {
269 std::vector<uint8_t> rsp;
270 const auto& [netfn, lun, cmd, cc, payload] = response;
271 numberOfReqNotRsp--;
272 if (ec)
273 {
274 std::string msgToLog =
275 "ssif<->ipmid bus error:"
276 " netfn=" +
277 std::to_string(netfn) + " lun=" + std::to_string(lun) +
278 " cmd=" + std::to_string(cmd) + " error=" + ec.message();
279 log<level::ERR>(msgToLog.c_str());
280 rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) +
281 sizeof(cc));
282 /* if dbusTimeout, just return and do not send any response
283 * to let host continue with other commands, response here
284 * is potentially make the response duplicated
285 * */
286 return;
287 }
288 else
289 {
290 if ((prev_req_cmd.netfn != (netfn - 1) || prev_req_cmd.lun != lun ||
291 prev_req_cmd.cmd != cmd) ||
292 ((prev_req_cmd.netfn == (netfn - 1) &&
293 prev_req_cmd.lun == lun && prev_req_cmd.cmd == cmd) &&
294 numberOfReqNotRsp != 0))
295 {
296 /* Only send response to the last request command to void
297 * duplicated response which makes host driver confused and
298 * failed to create interface
299 *
300 * Drop responses which are (1) different from the request
301 * (2) parameters are the same as request but handshake flow
302 * are in dupplicate request state
303 * */
304 if (verbose)
305 {
306 std::string msgToLog = "Drop ssif respond message with"
307 " len=" +
308 std::to_string(payload.size() + 3) +
309 " netfn=" + std::to_string(netfn) +
310 " lun=" + std::to_string(lun) +
311 " cmd=" + std::to_string(cmd) +
312 " cc=" + std::to_string(cc) +
313 " numberOfReqNotRsp=" +
314 std::to_string(numberOfReqNotRsp);
315 log<level::INFO>(msgToLog.c_str());
316 }
317 return;
318 }
319 rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) +
320 sizeof(cc) + payload.size());
Dung Caofaf6a6a2020-12-28 04:44:45 +0000321
Patrick Williams8b050c92023-10-20 11:19:32 -0500322 // write the response
323 auto rspIter = rsp.begin();
324 unsigned int* p = (unsigned int*)&rspIter[0];
325 *p = payload.size() + 3;
326 rspIter[sizeofLenField] = (netfn << netFnShift) | (lun & lunMask);
327 rspIter[sizeofLenField + 1] = cmd;
328 rspIter[sizeofLenField + 2] = cc;
329 if (payload.size())
330 {
331 std::copy(payload.cbegin(), payload.cend(),
332 rspIter + sizeofLenField + 3);
333 }
334 }
335 if (verbose)
336 {
337 std::string msgToLog =
338 "Send ssif respond message with"
339 " len=" +
340 std::to_string(payload.size() + 3) +
341 " netfn=" + std::to_string(netfn) +
342 " lun=" + std::to_string(lun) + " cmd=" + std::to_string(cmd) +
343 " cc=" + std::to_string(cc) +
344 " numberOfReqNotRsp=" + std::to_string(numberOfReqNotRsp);
345 log<level::INFO>(msgToLog.c_str());
346 }
347 boost::system::error_code ecWr;
348 size_t wlen = boost::asio::write(*dev, boost::asio::buffer(rsp), ecWr);
349 if (ecWr || wlen != rsp.size())
350 {
351 std::string msgToLog =
352 "Failed to send ssif respond message:"
353 " size=" +
354 std::to_string(wlen) + " expect=" + std::to_string(rsp.size()) +
355 " error=" + ecWr.message() + " netfn=" + std::to_string(netfn) +
356 " lun=" + std::to_string(lun) + " cmd=" + std::to_string(cmd) +
357 " cc=" + std::to_string(cc);
358 log<level::ERR>(msgToLog.c_str());
359 }
Ed Tanous4ae3b9e2024-02-14 08:54:03 -0800360 rspTimer.cancel();
Patrick Williams8b050c92023-10-20 11:19:32 -0500361 },
362 ipmiQueueService, ipmiQueuePath, ipmiQueueIntf, ipmiQueueMethod,
363 dbusTimeout, netfn, lun, cmd, data, options);
Dung Caofaf6a6a2020-12-28 04:44:45 +0000364}
365
Patrick Williams8b050c92023-10-20 11:19:32 -0500366int main(int argc, char* argv[])
Dung Caofaf6a6a2020-12-28 04:44:45 +0000367{
Patrick Williams8b050c92023-10-20 11:19:32 -0500368 CLI::App app("SSIF IPMI bridge");
369 std::string device;
370 app.add_option("-d,--device", device,
371 "use <DEVICE> file. Default is /dev/ipmi-ssif-host");
372 bool verbose = false;
373 int numberOfReqNotRsp = 0;
374 app.add_option("-v,--verbose", verbose, "print more verbose output");
375 CLI11_PARSE(app, argc, argv);
Dung Caofaf6a6a2020-12-28 04:44:45 +0000376
Patrick Williams8b050c92023-10-20 11:19:32 -0500377 auto io = std::make_shared<boost::asio::io_context>();
Dung Caofaf6a6a2020-12-28 04:44:45 +0000378
Ed Tanousd289aea2024-02-06 20:33:26 -0800379 auto bus = std::make_shared<sdbusplus::asio::connection>(*io);
Patrick Williams8b050c92023-10-20 11:19:32 -0500380 bus->request_name(ssifBus);
381 // Create the SSIF channel, listening on D-Bus and on the SSIF device
382 ssifchannel = make_unique<SsifChannel>(io, bus, device, verbose,
383 numberOfReqNotRsp);
384 if (!ssifchannel->initOK())
385 {
386 return EXIT_FAILURE;
387 }
Patrick Williams8b050c92023-10-20 11:19:32 -0500388 io->run();
Thang Q. Nguyen4300d212023-05-15 12:22:53 +0700389
Patrick Williams8b050c92023-10-20 11:19:32 -0500390 return 0;
Dung Caofaf6a6a2020-12-28 04:44:45 +0000391}