blob: ffc81c8cb46d589c093a0e8bff46abd108d689a9 [file] [log] [blame]
Chicago Duan184f6022020-04-17 11:30:49 +08001#include "config.h"
2
3#include "softoff.hpp"
4
5#include "libpldm/entity.h"
6#include "libpldm/platform.h"
7#include "libpldm/requester/pldm.h"
8#include "libpldm/state_set.h"
9
10#include "common/utils.hpp"
11
12#include <sdbusplus/bus.hpp>
13#include <sdeventplus/clock.hpp>
George Liua2767e62021-02-24 18:53:34 +080014#include <sdeventplus/exception.hpp>
Chicago Duan184f6022020-04-17 11:30:49 +080015#include <sdeventplus/source/io.hpp>
16#include <sdeventplus/source/time.hpp>
17
18#include <array>
19#include <iostream>
20
21namespace pldm
22{
23
24using namespace sdeventplus;
25using namespace sdeventplus::source;
26constexpr auto clockId = sdeventplus::ClockId::RealTime;
27using Clock = Clock<clockId>;
28using Timer = Time<clockId>;
29
Chicago Duan184f6022020-04-17 11:30:49 +080030constexpr pldm::pdr::TerminusID TID = 0; // TID will be implemented later.
31namespace sdbusRule = sdbusplus::bus::match::rules;
32
33SoftPowerOff::SoftPowerOff(sdbusplus::bus::bus& bus, sd_event* event) :
34 bus(bus), timer(event)
35{
Manojkiran Eda31a78442021-09-12 15:18:25 +053036 getHostState();
Chicago Duan184f6022020-04-17 11:30:49 +080037 if (hasError || completed)
38 {
39 return;
40 }
41
Manojkiran Eda31a78442021-09-12 15:18:25 +053042 auto rc = getEffecterID();
Tom Joseph29d22112020-11-18 15:05:15 +053043 if (completed)
44 {
45 std::cerr
46 << "pldm-softpoweroff: effecter to initiate softoff not found \n";
47 return;
48 }
49 else if (rc != PLDM_SUCCESS)
Chicago Duan184f6022020-04-17 11:30:49 +080050 {
51 hasError = true;
52 return;
53 }
54
55 rc = getSensorInfo();
56 if (rc != PLDM_SUCCESS)
57 {
58 std::cerr << "Message get Sensor PDRs error. PLDM "
59 "error code = "
60 << std::hex << std::showbase << rc << "\n";
61 hasError = true;
62 return;
63 }
64
65 // Matches on the pldm StateSensorEvent signal
66 pldmEventSignal = std::make_unique<sdbusplus::bus::match_t>(
67 bus,
68 sdbusRule::type::signal() + sdbusRule::member("StateSensorEvent") +
69 sdbusRule::path("/xyz/openbmc_project/pldm") +
70 sdbusRule::interface("xyz.openbmc_project.PLDM.Event"),
71 std::bind(std::mem_fn(&SoftPowerOff::hostSoftOffComplete), this,
72 std::placeholders::_1));
73}
74
75int SoftPowerOff::getHostState()
76{
77 try
78 {
79 pldm::utils::PropertyValue propertyValue =
80 pldm::utils::DBusHandler().getDbusPropertyVariant(
81 "/xyz/openbmc_project/state/host0", "CurrentHostState",
82 "xyz.openbmc_project.State.Host");
83
Andrew Geissler5b5fa432021-01-22 16:27:24 -060084 if ((std::get<std::string>(propertyValue) !=
85 "xyz.openbmc_project.State.Host.HostState.Running") &&
86 (std::get<std::string>(propertyValue) !=
87 "xyz.openbmc_project.State.Host.HostState.TransitioningToOff"))
Chicago Duan184f6022020-04-17 11:30:49 +080088 {
89 // Host state is not "Running", this app should return success
90 completed = true;
91 return PLDM_SUCCESS;
92 }
93 }
94 catch (const std::exception& e)
95 {
96 std::cerr << "PLDM host soft off: Can't get current host state.\n";
97 hasError = true;
98 return PLDM_ERROR;
99 }
100
101 return PLDM_SUCCESS;
102}
103
104void SoftPowerOff::hostSoftOffComplete(sdbusplus::message::message& msg)
105{
106 pldm::pdr::TerminusID msgTID;
107 pldm::pdr::SensorID msgSensorID;
108 pldm::pdr::SensorOffset msgSensorOffset;
109 pldm::pdr::EventState msgEventState;
110 pldm::pdr::EventState msgPreviousEventState;
111
112 // Read the msg and populate each variable
113 msg.read(msgTID, msgSensorID, msgSensorOffset, msgEventState,
114 msgPreviousEventState);
115
116 if (msgSensorID == sensorID && msgSensorOffset == sensorOffset &&
117 msgEventState == PLDM_SW_TERM_GRACEFUL_SHUTDOWN)
118 {
119 // Receive Graceful shutdown completion event message. Disable the timer
120 auto rc = timer.stop();
121 if (rc < 0)
122 {
123 std::cerr << "PLDM soft off: Failure to STOP the timer. ERRNO="
124 << rc << "\n";
125 }
126
127 // This marks the completion of pldm soft power off.
128 completed = true;
129 }
130}
131
132int SoftPowerOff::getEffecterID()
133{
134 auto& bus = pldm::utils::DBusHandler::getBus();
135
136 // VMM is a logical entity, so the bit 15 in entity type is set.
137 pdr::EntityType entityType = PLDM_ENTITY_VIRTUAL_MACHINE_MANAGER | 0x8000;
138
139 try
140 {
141 std::vector<std::vector<uint8_t>> VMMResponse{};
142 auto VMMMethod = bus.new_method_call(
143 "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm",
144 "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR");
145 VMMMethod.append(TID, entityType,
146 (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS);
147
148 auto VMMResponseMsg = bus.call(VMMMethod);
149
150 VMMResponseMsg.read(VMMResponse);
151 if (VMMResponse.size() != 0)
152 {
153 for (auto& rep : VMMResponse)
154 {
155 auto VMMPdr =
156 reinterpret_cast<pldm_state_effecter_pdr*>(rep.data());
157 effecterID = VMMPdr->effecter_id;
158 }
159 }
160 else
161 {
162 VMMPdrExist = false;
163 }
164 }
Patrick Williams4fea7a22021-09-02 09:54:12 -0500165 catch (const sdbusplus::exception::exception& e)
Chicago Duan184f6022020-04-17 11:30:49 +0800166 {
167 std::cerr << "PLDM soft off: Error get VMM PDR,ERROR=" << e.what()
168 << "\n";
169 VMMPdrExist = false;
170 }
171
172 if (VMMPdrExist)
173 {
174 return PLDM_SUCCESS;
175 }
176
177 // If the Virtual Machine Manager PDRs doesn't exist, go find the System
178 // Firmware PDRs.
179 // System Firmware is a logical entity, so the bit 15 in entity type is set
180 entityType = PLDM_ENTITY_SYS_FIRMWARE | 0x8000;
181 try
182 {
183 std::vector<std::vector<uint8_t>> sysFwResponse{};
184 auto sysFwMethod = bus.new_method_call(
185 "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm",
186 "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR");
187 sysFwMethod.append(TID, entityType,
188 (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS);
189
190 auto sysFwResponseMsg = bus.call(sysFwMethod);
191
192 sysFwResponseMsg.read(sysFwResponse);
193
194 if (sysFwResponse.size() == 0)
195 {
196 std::cerr
197 << "No effecter ID has been found that matches the criteria"
198 << "\n";
199 return PLDM_ERROR;
200 }
201
202 for (auto& rep : sysFwResponse)
203 {
204 auto sysFwPdr =
205 reinterpret_cast<pldm_state_effecter_pdr*>(rep.data());
206 effecterID = sysFwPdr->effecter_id;
207 }
208 }
Patrick Williams4fea7a22021-09-02 09:54:12 -0500209 catch (const sdbusplus::exception::exception& e)
Chicago Duan184f6022020-04-17 11:30:49 +0800210 {
211 std::cerr << "PLDM soft off: Error get system firmware PDR,ERROR="
212 << e.what() << "\n";
Tom Joseph29d22112020-11-18 15:05:15 +0530213 completed = true;
Chicago Duan184f6022020-04-17 11:30:49 +0800214 return PLDM_ERROR;
215 }
216
217 return PLDM_SUCCESS;
218}
219
220int SoftPowerOff::getSensorInfo()
221{
222 pldm::pdr::EntityType entityType;
223
224 entityType = VMMPdrExist ? PLDM_ENTITY_VIRTUAL_MACHINE_MANAGER
225 : PLDM_ENTITY_SYS_FIRMWARE;
226
227 // The Virtual machine manager/System firmware is logical entity, so bit 15
228 // need to be set.
229 entityType = entityType | 0x8000;
230
231 try
232 {
233 auto& bus = pldm::utils::DBusHandler::getBus();
234 std::vector<std::vector<uint8_t>> Response{};
235 auto method = bus.new_method_call(
236 "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm",
237 "xyz.openbmc_project.PLDM.PDR", "FindStateSensorPDR");
238 method.append(TID, entityType,
239 (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS);
240
241 auto ResponseMsg = bus.call(method);
242
243 ResponseMsg.read(Response);
244
245 if (Response.size() == 0)
246 {
247 std::cerr
248 << "No sensor PDR has been found that matches the criteria"
249 << "\n";
250 return PLDM_ERROR;
251 }
252
253 pldm_state_sensor_pdr* pdr;
254 for (auto& rep : Response)
255 {
256 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(rep.data());
Manojkiran Eda31a78442021-09-12 15:18:25 +0530257 if (!pdr)
258 {
259 std::cerr << "Failed to get state sensor PDR.\n";
260 return PLDM_ERROR;
261 }
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530262 }
263
Chicago Duan184f6022020-04-17 11:30:49 +0800264 sensorID = pdr->sensor_id;
265
266 auto compositeSensorCount = pdr->composite_sensor_count;
267 auto possibleStatesStart = pdr->possible_states;
268
269 for (auto offset = 0; offset < compositeSensorCount; offset++)
270 {
271 auto possibleStates =
272 reinterpret_cast<state_sensor_possible_states*>(
273 possibleStatesStart);
274 auto setId = possibleStates->state_set_id;
275 auto possibleStateSize = possibleStates->possible_states_size;
276
277 if (setId == PLDM_STATE_SET_SW_TERMINATION_STATUS)
278 {
279 sensorOffset = offset;
280 break;
281 }
282 possibleStatesStart +=
283 possibleStateSize + sizeof(setId) + sizeof(possibleStateSize);
284 }
285 }
Patrick Williams4fea7a22021-09-02 09:54:12 -0500286 catch (const sdbusplus::exception::exception& e)
Chicago Duan184f6022020-04-17 11:30:49 +0800287 {
288 std::cerr << "PLDM soft off: Error get State Sensor PDR,ERROR="
289 << e.what() << "\n";
290 return PLDM_ERROR;
291 }
292
293 return PLDM_SUCCESS;
294}
295
296int SoftPowerOff::hostSoftOff(sdeventplus::Event& event)
297{
298 constexpr uint8_t effecterCount = 1;
299 uint8_t mctpEID;
300 uint8_t instanceID;
301
302 mctpEID = pldm::utils::readHostEID();
303
304 // Get instanceID
305 try
306 {
307 auto& bus = pldm::utils::DBusHandler::getBus();
308 auto method = bus.new_method_call(
309 "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm",
310 "xyz.openbmc_project.PLDM.Requester", "GetInstanceId");
311 method.append(mctpEID);
312
313 auto ResponseMsg = bus.call(method);
314
315 ResponseMsg.read(instanceID);
316 }
Patrick Williams4fea7a22021-09-02 09:54:12 -0500317 catch (const sdbusplus::exception::exception& e)
Chicago Duan184f6022020-04-17 11:30:49 +0800318 {
319 std::cerr << "PLDM soft off: Error get instanceID,ERROR=" << e.what()
320 << "\n";
321 return PLDM_ERROR;
322 }
323
324 std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterID) +
325 sizeof(effecterCount) +
326 sizeof(set_effecter_state_field)>
327 requestMsg{};
328 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
329 set_effecter_state_field stateField{
330 PLDM_REQUEST_SET, PLDM_SW_TERM_GRACEFUL_SHUTDOWN_REQUESTED};
331 auto rc = encode_set_state_effecter_states_req(
332 instanceID, effecterID, effecterCount, &stateField, request);
333 if (rc != PLDM_SUCCESS)
334 {
335 std::cerr << "Message encode failure. PLDM error code = " << std::hex
336 << std::showbase << rc << "\n";
337 return PLDM_ERROR;
338 }
339
340 // Open connection to MCTP socket
341 int fd = pldm_open();
342 if (-1 == fd)
343 {
344 std::cerr << "Failed to connect to mctp demux daemon"
345 << "\n";
346 return PLDM_ERROR;
347 }
348
349 // Add a timer to the event loop, default 30s.
Deepak Kodihallib0d15f12021-04-16 12:18:10 +0530350 auto timerCallback = [=, this](Timer& /*source*/,
351 Timer::TimePoint /*time*/) {
Chicago Duan184f6022020-04-17 11:30:49 +0800352 if (!responseReceived)
353 {
354 std::cerr << "PLDM soft off: ERROR! Can't get the response for the "
355 "PLDM request msg. Time out!\n"
356 << "Exit the pldm-softpoweroff\n";
357 exit(-1);
358 }
359 return;
360 };
361 Timer time(event, (Clock(event).now() + std::chrono::seconds{30}),
362 std::chrono::seconds{1}, std::move(timerCallback));
363
364 // Add a callback to handle EPOLLIN on fd
Deepak Kodihallib0d15f12021-04-16 12:18:10 +0530365 auto callback = [=, this](IO& io, int fd, uint32_t revents) {
Chicago Duan184f6022020-04-17 11:30:49 +0800366 if (!(revents & EPOLLIN))
367 {
368 return;
369 }
370
371 uint8_t* responseMsg = nullptr;
372 size_t responseMsgSize{};
373
374 auto rc = pldm_recv(mctpEID, fd, request->hdr.instance_id, &responseMsg,
375 &responseMsgSize);
376 if (rc)
377 {
378 return;
379 }
380
381 std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{
382 responseMsg, std::free};
383
384 // We've got the response meant for the PLDM request msg that was
385 // sent out
386 io.set_enabled(Enabled::Off);
387 auto response = reinterpret_cast<pldm_msg*>(responseMsgPtr.get());
388 std::cerr << "Getting the response. PLDM RC = " << std::hex
389 << std::showbase
390 << static_cast<uint16_t>(response->payload[0]) << "\n";
391
392 responseReceived = true;
393
394 // Start Timer
395 using namespace std::chrono;
396 auto timeMicroseconds =
397 duration_cast<microseconds>(seconds(SOFTOFF_TIMEOUT_SECONDS));
398
399 auto ret = startTimer(timeMicroseconds);
400 if (ret < 0)
401 {
402 std::cerr << "Failure to start Host soft off wait timer, ERRNO = "
403 << ret << "Exit the pldm-softpoweroff\n";
404 exit(-1);
405 }
406 else
407 {
408 std::cerr << "Timer started waiting for host soft off, "
409 "TIMEOUT_IN_SEC = "
410 << SOFTOFF_TIMEOUT_SECONDS << "\n";
411 }
412 return;
413 };
414 IO io(event, fd, EPOLLIN, std::move(callback));
415
416 // Send PLDM Request message - pldm_send doesn't wait for response
417 rc = pldm_send(mctpEID, fd, requestMsg.data(), requestMsg.size());
Sampa Misra9f8d2b02021-03-24 08:33:14 +0000418 if (0 > rc)
Chicago Duan184f6022020-04-17 11:30:49 +0800419 {
420 std::cerr << "Failed to send message/receive response. RC = " << rc
421 << ", errno = " << errno << "\n";
422 return PLDM_ERROR;
423 }
424
425 // Time out or soft off complete
426 while (!isCompleted() && !isTimerExpired())
427 {
428 try
429 {
430 event.run(std::nullopt);
431 }
432 catch (const sdeventplus::SdEventError& e)
433 {
434 std::cerr
435 << "PLDM host soft off: Failure in processing request.ERROR= "
436 << e.what() << "\n";
437 return PLDM_ERROR;
438 }
439 }
440
441 return PLDM_SUCCESS;
442}
443
444int SoftPowerOff::startTimer(const std::chrono::microseconds& usec)
445{
446 return timer.start(usec);
447}
448} // namespace pldm