blob: 47eacf0249e412ccb670b5e07af51316304769e0 [file] [log] [blame]
Patrick Venture5e6ac712017-10-25 12:16:19 -07001#include "watchdog.hpp"
2
Patrick Venture0b02be92018-08-31 11:55:55 -07003#include "watchdog_service.hpp"
4
William A. Kennington III52575252018-02-09 15:54:56 -08005#include <endian.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07006
7#include <cstdint>
Vernon Mauerye08fbff2019-04-03 09:19:34 -07008#include <ipmid/api.hpp>
William A. Kennington III021b4c12018-05-10 11:12:51 -07009#include <phosphor-logging/elog-errors.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070010#include <phosphor-logging/elog.hpp>
William A. Kennington III52575252018-02-09 15:54:56 -080011#include <phosphor-logging/log.hpp>
12#include <string>
William A. Kennington III021b4c12018-05-10 11:12:51 -070013#include <xyz/openbmc_project/Common/error.hpp>
Patrick Venture894571d2017-11-09 14:46:54 -080014
William A. Kennington IIIbae471c2018-06-15 10:38:01 -070015using phosphor::logging::commit;
William A. Kennington III52575252018-02-09 15:54:56 -080016using phosphor::logging::level;
17using phosphor::logging::log;
William A. Kennington III021b4c12018-05-10 11:12:51 -070018using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Venture5e6ac712017-10-25 12:16:19 -070019
William A. Kennington IIIbae471c2018-06-15 10:38:01 -070020static bool lastCallSuccessful = false;
21
22void reportError()
23{
24 // We don't want to fill the SEL with errors if the daemon dies and doesn't
25 // come back but the watchdog keeps on ticking. Instead, we only report the
26 // error if we haven't reported one since the last successful call
27 if (!lastCallSuccessful)
28 {
29 return;
30 }
31 lastCallSuccessful = false;
32
33 // TODO: This slow down the end of the IPMI transaction waiting
34 // for the commit to finish. commit<>() can take at least 5 seconds
35 // to complete. 5s is very slow for an IPMI command and ends up
36 // congesting the IPMI channel needlessly, especially if the watchdog
37 // is ticking fairly quickly and we have some transient issues.
38 commit<InternalFailure>();
39}
40
Vernon Mauery11df4f62019-03-25 14:17:54 -070041ipmi::RspType<> ipmiAppResetWatchdogTimer()
Patrick Venture5e6ac712017-10-25 12:16:19 -070042{
William A. Kennington III52575252018-02-09 15:54:56 -080043 try
44 {
45 WatchdogService wd_service;
William A. Kennington III52575252018-02-09 15:54:56 -080046
William A. Kennington IIIde14a022018-02-09 16:11:18 -080047 // Notify the caller if we haven't initialized our timer yet
48 // so it can configure actions and timeouts
William A. Kennington III2ecf5122018-04-27 14:31:51 -070049 if (!wd_service.getInitialized())
William A. Kennington IIIde14a022018-02-09 16:11:18 -080050 {
William A. Kennington IIIbae471c2018-06-15 10:38:01 -070051 lastCallSuccessful = true;
Vernon Mauery11df4f62019-03-25 14:17:54 -070052
53 constexpr uint8_t ccWatchdogNotInit = 0x80;
54 return ipmi::response(ccWatchdogNotInit);
William A. Kennington IIIde14a022018-02-09 16:11:18 -080055 }
56
William A. Kennington III4b017a92018-04-27 14:31:08 -070057 // The ipmi standard dictates we enable the watchdog during reset
58 wd_service.resetTimeRemaining(true);
William A. Kennington IIIbae471c2018-06-15 10:38:01 -070059 lastCallSuccessful = true;
Vernon Mauery11df4f62019-03-25 14:17:54 -070060 return ipmi::responseSuccess();
Patrick Venture5e6ac712017-10-25 12:16:19 -070061 }
William A. Kennington III021b4c12018-05-10 11:12:51 -070062 catch (const InternalFailure& e)
63 {
William A. Kennington IIIbae471c2018-06-15 10:38:01 -070064 reportError();
Vernon Mauery11df4f62019-03-25 14:17:54 -070065 return ipmi::responseUnspecifiedError();
William A. Kennington III021b4c12018-05-10 11:12:51 -070066 }
William A. Kennington III52575252018-02-09 15:54:56 -080067 catch (const std::exception& e)
68 {
69 const std::string e_str = std::string("wd_reset: ") + e.what();
70 log<level::ERR>(e_str.c_str());
William A. Kennington IIIbae471c2018-06-15 10:38:01 -070071 reportError();
Vernon Mauery11df4f62019-03-25 14:17:54 -070072 return ipmi::responseUnspecifiedError();
William A. Kennington III5325f2c2018-01-08 15:17:09 -080073 }
William A. Kennington III52575252018-02-09 15:54:56 -080074 catch (...)
75 {
76 log<level::ERR>("wd_reset: Unknown Error");
William A. Kennington IIIbae471c2018-06-15 10:38:01 -070077 reportError();
Vernon Mauery11df4f62019-03-25 14:17:54 -070078 return ipmi::responseUnspecifiedError();
William A. Kennington III5325f2c2018-01-08 15:17:09 -080079 }
Patrick Venture5e6ac712017-10-25 12:16:19 -070080}
William A. Kennington III61d5f7b2018-02-09 15:23:53 -080081
William A. Kennington III52575252018-02-09 15:54:56 -080082static constexpr uint8_t wd_dont_stop = 0x1 << 6;
83static constexpr uint8_t wd_timeout_action_mask = 0x3;
84
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +000085static constexpr uint8_t wdTimerUseResTimer1 = 0x0;
86static constexpr uint8_t wdTimerUseResTimer2 = 0x6;
87static constexpr uint8_t wdTimerUseResTimer3 = 0x7;
88
Yong Li4dd71af2019-09-29 14:18:07 +080089static constexpr uint8_t wdTimeoutActionMax = 3;
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +000090static constexpr uint8_t wdTimeoutInterruptTimer = 0x04;
Yong Li118907e2019-01-11 17:36:17 +080091
Patrick Venture0b02be92018-08-31 11:55:55 -070092enum class IpmiAction : uint8_t
93{
William A. Kennington III52575252018-02-09 15:54:56 -080094 None = 0x0,
95 HardReset = 0x1,
96 PowerOff = 0x2,
97 PowerCycle = 0x3,
98};
99
William A. Kennington IIIb638de22018-02-09 16:12:53 -0800100/** @brief Converts an IPMI Watchdog Action to DBUS defined action
101 * @param[in] ipmi_action The IPMI Watchdog Action
102 * @return The Watchdog Action that the ipmi_action maps to
103 */
104WatchdogService::Action ipmiActionToWdAction(IpmiAction ipmi_action)
105{
Patrick Venture0b02be92018-08-31 11:55:55 -0700106 switch (ipmi_action)
William A. Kennington IIIb638de22018-02-09 16:12:53 -0800107 {
108 case IpmiAction::None:
109 {
110 return WatchdogService::Action::None;
111 }
112 case IpmiAction::HardReset:
113 {
114 return WatchdogService::Action::HardReset;
115 }
116 case IpmiAction::PowerOff:
117 {
118 return WatchdogService::Action::PowerOff;
119 }
120 case IpmiAction::PowerCycle:
121 {
122 return WatchdogService::Action::PowerCycle;
123 }
124 default:
125 {
126 throw std::domain_error("IPMI Action is invalid");
127 }
128 }
129}
130
Yong Li118907e2019-01-11 17:36:17 +0800131enum class IpmiTimerUse : uint8_t
132{
133 Reserved = 0x0,
134 BIOSFRB2 = 0x1,
135 BIOSPOST = 0x2,
136 OSLoad = 0x3,
137 SMSOS = 0x4,
138 OEM = 0x5,
139};
140
141WatchdogService::TimerUse ipmiTimerUseToWdTimerUse(IpmiTimerUse ipmiTimerUse)
142{
143 switch (ipmiTimerUse)
144 {
145 case IpmiTimerUse::Reserved:
146 {
147 return WatchdogService::TimerUse::Reserved;
148 }
149 case IpmiTimerUse::BIOSFRB2:
150 {
151 return WatchdogService::TimerUse::BIOSFRB2;
152 }
153 case IpmiTimerUse::BIOSPOST:
154 {
155 return WatchdogService::TimerUse::BIOSPOST;
156 }
157 case IpmiTimerUse::OSLoad:
158 {
159 return WatchdogService::TimerUse::OSLoad;
160 }
161 case IpmiTimerUse::SMSOS:
162 {
163 return WatchdogService::TimerUse::SMSOS;
164 }
165 case IpmiTimerUse::OEM:
166 {
167 return WatchdogService::TimerUse::OEM;
168 }
169 default:
170 {
171 return WatchdogService::TimerUse::Reserved;
172 }
173 }
174}
175
Yong Li4dd71af2019-09-29 14:18:07 +0800176static bool timerNotLogFlags = false;
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000177static uint8_t timerUseExpirationFlags = 0;
Yong Li4dd71af2019-09-29 14:18:07 +0800178static uint3_t timerPreTimeoutInterrupt = 0;
William A. Kennington III52575252018-02-09 15:54:56 -0800179
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000180/**@brief The Set Watchdog Timer ipmi command.
181 *
182 * @param
183 * - timerUse
184 * - dontStopTimer
185 * - dontLog
186 * - timerAction
187 * - pretimeout
188 * - expireFlags
189 * - initialCountdown
190 *
191 * @return completion code on success.
192 **/
193ipmi::RspType<> ipmiSetWatchdogTimer(
194 uint3_t timerUse, uint3_t reserved, bool dontStopTimer, bool dontLog,
195 uint3_t timeoutAction, uint1_t reserved1, uint3_t preTimeoutInterrupt,
196 uint1_t reserved2, uint8_t preTimeoutInterval, uint1_t reserved3,
197 uint5_t expFlagValue, uint2_t reserved4, uint16_t initialCountdown)
William A. Kennington III61d5f7b2018-02-09 15:23:53 -0800198{
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000199 if ((timerUse == wdTimerUseResTimer1) ||
200 (timerUse == wdTimerUseResTimer2) ||
201 (timerUse == wdTimerUseResTimer3) ||
Yong Li4dd71af2019-09-29 14:18:07 +0800202 (timeoutAction > wdTimeoutActionMax) ||
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000203 (preTimeoutInterrupt == wdTimeoutInterruptTimer) ||
Yong Li4dd71af2019-09-29 14:18:07 +0800204 (reserved | reserved1 | reserved2 | reserved3 | reserved4))
William A. Kennington III52575252018-02-09 15:54:56 -0800205 {
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000206 return ipmi::responseInvalidFieldRequest();
William A. Kennington III52575252018-02-09 15:54:56 -0800207 }
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000208
209 if (preTimeoutInterval > (initialCountdown / 10))
210 {
211 return ipmi::responseInvalidFieldRequest();
212 }
213
Yong Li4dd71af2019-09-29 14:18:07 +0800214 timerNotLogFlags = dontLog;
215 timerPreTimeoutInterrupt = preTimeoutInterrupt;
William A. Kennington III61d5f7b2018-02-09 15:23:53 -0800216
William A. Kennington III52575252018-02-09 15:54:56 -0800217 try
William A. Kennington III61d5f7b2018-02-09 15:23:53 -0800218 {
William A. Kennington III52575252018-02-09 15:54:56 -0800219 WatchdogService wd_service;
220 // Stop the timer if the don't stop bit is not set
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000221 if (!(dontStopTimer))
William A. Kennington III52575252018-02-09 15:54:56 -0800222 {
223 wd_service.setEnabled(false);
William A. Kennington III61d5f7b2018-02-09 15:23:53 -0800224 }
225
William A. Kennington III52575252018-02-09 15:54:56 -0800226 // Set the action based on the request
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000227 const auto ipmi_action = static_cast<IpmiAction>(
228 static_cast<uint8_t>(timeoutAction) & wd_timeout_action_mask);
William A. Kennington IIIb638de22018-02-09 16:12:53 -0800229 wd_service.setExpireAction(ipmiActionToWdAction(ipmi_action));
William A. Kennington III52575252018-02-09 15:54:56 -0800230
Yong Li118907e2019-01-11 17:36:17 +0800231 const auto ipmiTimerUse =
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000232 static_cast<IpmiTimerUse>(static_cast<uint8_t>(timerUse));
Yong Li118907e2019-01-11 17:36:17 +0800233 wd_service.setTimerUse(ipmiTimerUseToWdTimerUse(ipmiTimerUse));
234
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000235 wd_service.setExpiredTimerUse(WatchdogService::TimerUse::Reserved);
236
237 timerUseExpirationFlags &= static_cast<uint8_t>(~expFlagValue) << 2;
238
William A. Kennington III52575252018-02-09 15:54:56 -0800239 // Set the new interval and the time remaining deci -> mill seconds
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000240 const uint64_t interval = initialCountdown * 100;
William A. Kennington III52575252018-02-09 15:54:56 -0800241 wd_service.setInterval(interval);
242 wd_service.setTimeRemaining(interval);
243
William A. Kennington IIIde14a022018-02-09 16:11:18 -0800244 // Mark as initialized so that future resets behave correctly
245 wd_service.setInitialized(true);
246
William A. Kennington IIIbae471c2018-06-15 10:38:01 -0700247 lastCallSuccessful = true;
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000248 return ipmi::responseSuccess();
William A. Kennington III61d5f7b2018-02-09 15:23:53 -0800249 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700250 catch (const std::domain_error&)
William A. Kennington III52575252018-02-09 15:54:56 -0800251 {
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000252 return ipmi::responseInvalidFieldRequest();
William A. Kennington III52575252018-02-09 15:54:56 -0800253 }
William A. Kennington III021b4c12018-05-10 11:12:51 -0700254 catch (const InternalFailure& e)
255 {
William A. Kennington IIIbae471c2018-06-15 10:38:01 -0700256 reportError();
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000257 return ipmi::responseUnspecifiedError();
William A. Kennington III021b4c12018-05-10 11:12:51 -0700258 }
William A. Kennington III52575252018-02-09 15:54:56 -0800259 catch (const std::exception& e)
260 {
261 const std::string e_str = std::string("wd_set: ") + e.what();
262 log<level::ERR>(e_str.c_str());
William A. Kennington IIIbae471c2018-06-15 10:38:01 -0700263 reportError();
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000264 return ipmi::responseUnspecifiedError();
William A. Kennington III52575252018-02-09 15:54:56 -0800265 }
266 catch (...)
267 {
268 log<level::ERR>("wd_set: Unknown Error");
William A. Kennington IIIbae471c2018-06-15 10:38:01 -0700269 reportError();
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000270 return ipmi::responseUnspecifiedError();
William A. Kennington III52575252018-02-09 15:54:56 -0800271 }
William A. Kennington III61d5f7b2018-02-09 15:23:53 -0800272}
William A. Kennington III73f44512018-02-09 15:28:46 -0800273
274/** @brief Converts a DBUS Watchdog Action to IPMI defined action
275 * @param[in] wd_action The DBUS Watchdog Action
276 * @return The IpmiAction that the wd_action maps to
277 */
278IpmiAction wdActionToIpmiAction(WatchdogService::Action wd_action)
279{
Patrick Venture0b02be92018-08-31 11:55:55 -0700280 switch (wd_action)
William A. Kennington III73f44512018-02-09 15:28:46 -0800281 {
282 case WatchdogService::Action::None:
283 {
284 return IpmiAction::None;
285 }
286 case WatchdogService::Action::HardReset:
287 {
288 return IpmiAction::HardReset;
289 }
290 case WatchdogService::Action::PowerOff:
291 {
292 return IpmiAction::PowerOff;
293 }
294 case WatchdogService::Action::PowerCycle:
295 {
296 return IpmiAction::PowerCycle;
297 }
298 default:
299 {
300 // We have no method via IPMI to signal that the action is unknown
301 // or unmappable in some way.
302 // Just ignore the error and return NONE so the host can reconcile.
303 return IpmiAction::None;
304 }
305 }
306}
307
Yong Li118907e2019-01-11 17:36:17 +0800308IpmiTimerUse wdTimerUseToIpmiTimerUse(WatchdogService::TimerUse wdTimerUse)
309{
310 switch (wdTimerUse)
311 {
312 case WatchdogService::TimerUse::Reserved:
313 {
314 return IpmiTimerUse::Reserved;
315 }
316 case WatchdogService::TimerUse::BIOSFRB2:
317 {
318 return IpmiTimerUse::BIOSFRB2;
319 }
320 case WatchdogService::TimerUse::BIOSPOST:
321 {
322 return IpmiTimerUse::BIOSPOST;
323 }
324 case WatchdogService::TimerUse::OSLoad:
325 {
326 return IpmiTimerUse::OSLoad;
327 }
328
329 case WatchdogService::TimerUse::SMSOS:
330 {
331 return IpmiTimerUse::SMSOS;
332 }
333 case WatchdogService::TimerUse::OEM:
334 {
335 return IpmiTimerUse::OEM;
336 }
337 default:
338 {
339 return IpmiTimerUse::Reserved;
340 }
341 }
342}
343
William A. Kennington III73f44512018-02-09 15:28:46 -0800344static constexpr uint8_t wd_running = 0x1 << 6;
345
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000346/**@brief The getWatchdogTimer ipmi command.
347 *
348 * @return Completion code plus timer details.
349 * - timerUse
350 * - timerAction
351 * - pretimeout
352 * - expireFlags
353 * - initialCountdown
354 * - presentCountdown
355 **/
Yong Li4dd71af2019-09-29 14:18:07 +0800356ipmi::RspType<uint3_t, // timerUse - timer use
357 uint3_t, // timerUse - reserved
358 bool, // timerUse - timer is started
359 bool, // timerUse - don't log
360
361 uint3_t, // timerAction - timeout action
362 uint1_t, // timerAction - reserved
363 uint3_t, // timerAction - pre-timeout interrupt
364 uint1_t, // timerAction - reserved
365
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000366 uint8_t, // pretimeout
367 uint8_t, // expireFlags
368 uint16_t, // initial Countdown - Little Endian (deciseconds)
369 uint16_t // present Countdown - Little Endian (deciseconds)
370 >
371 ipmiGetWatchdogTimer()
William A. Kennington III73f44512018-02-09 15:28:46 -0800372{
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000373 uint8_t expireFlags = 0;
374 uint16_t presentCountdown = 0;
375 uint8_t pretimeout = 0;
William A. Kennington III73f44512018-02-09 15:28:46 -0800376
377 try
378 {
379 WatchdogService wd_service;
380 WatchdogService::Properties wd_prop = wd_service.getProperties();
381
382 // Build and return the response
Yong Lif7c9db02019-01-15 13:45:33 +0800383 // Interval and timeRemaining need converted from milli -> deci seconds
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000384 uint16_t initialCountdown = htole16(wd_prop.interval / 100);
385
386 if (wd_prop.expiredTimerUse != WatchdogService::TimerUse::Reserved)
387 {
388 timerUseExpirationFlags |=
389 1 << static_cast<uint8_t>(
390 wdTimerUseToIpmiTimerUse(wd_prop.expiredTimerUse));
391 }
392
William A. Kennington III73f44512018-02-09 15:28:46 -0800393 if (wd_prop.enabled)
394 {
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000395 presentCountdown = htole16(wd_prop.timeRemaining / 100);
396 expireFlags = 0;
Yong Lif7c9db02019-01-15 13:45:33 +0800397 }
398 else
399 {
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000400 if (wd_prop.expiredTimerUse == WatchdogService::TimerUse::Reserved)
401 {
402 presentCountdown = initialCountdown;
403 expireFlags = 0;
404 }
405 else
406 {
407 presentCountdown = 0;
408 expireFlags = timerUseExpirationFlags;
Yong Li4dd71af2019-09-29 14:18:07 +0800409 // Automatically clear it whenever a timer expiration occurs.
410 timerNotLogFlags = false;
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000411 }
William A. Kennington III73f44512018-02-09 15:28:46 -0800412 }
Yong Li118907e2019-01-11 17:36:17 +0800413
William A. Kennington III73f44512018-02-09 15:28:46 -0800414 // TODO: Do something about having pretimeout support
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000415 pretimeout = 0;
416
William A. Kennington IIIbae471c2018-06-15 10:38:01 -0700417 lastCallSuccessful = true;
Yong Li4dd71af2019-09-29 14:18:07 +0800418 return ipmi::responseSuccess(
419 static_cast<uint3_t>(wdTimerUseToIpmiTimerUse(wd_prop.timerUse)), 0,
420 wd_prop.enabled, timerNotLogFlags,
421 static_cast<uint3_t>(wdActionToIpmiAction(wd_prop.expireAction)), 0,
422 timerPreTimeoutInterrupt, 0, pretimeout, expireFlags,
423 initialCountdown, presentCountdown);
William A. Kennington III73f44512018-02-09 15:28:46 -0800424 }
William A. Kennington III021b4c12018-05-10 11:12:51 -0700425 catch (const InternalFailure& e)
426 {
William A. Kennington IIIbae471c2018-06-15 10:38:01 -0700427 reportError();
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000428 return ipmi::responseUnspecifiedError();
William A. Kennington III021b4c12018-05-10 11:12:51 -0700429 }
William A. Kennington III73f44512018-02-09 15:28:46 -0800430 catch (const std::exception& e)
431 {
432 const std::string e_str = std::string("wd_get: ") + e.what();
433 log<level::ERR>(e_str.c_str());
William A. Kennington IIIbae471c2018-06-15 10:38:01 -0700434 reportError();
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000435 return ipmi::responseUnspecifiedError();
William A. Kennington III73f44512018-02-09 15:28:46 -0800436 }
437 catch (...)
438 {
439 log<level::ERR>("wd_get: Unknown Error");
William A. Kennington IIIbae471c2018-06-15 10:38:01 -0700440 reportError();
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000441 return ipmi::responseUnspecifiedError();
William A. Kennington III73f44512018-02-09 15:28:46 -0800442 }
443}