blob: ca41567829002663a79996994ee08c70fdd6280b [file] [log] [blame]
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -06001/**
2 * Copyright (C) 2020 IBM Corporation
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
Brad Bishop63508a72020-10-27 18:55:01 -040017#include "registration.hpp"
18
Jayanth Othayoth785cf6a2021-06-28 00:16:35 -050019extern "C"
20{
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060021#include <libpdbg.h>
Jayanth Othayoth785cf6a2021-06-28 00:16:35 -050022#include <libpdbg_sbe.h>
23}
24
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050025#include <sys/wait.h>
26#include <unistd.h>
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060027
28#include <phosphor-logging/log.hpp>
Brad Bishop5e5d4452020-10-27 19:46:13 -040029
Brad Bishop63508a72020-10-27 18:55:01 -040030#include <system_error>
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060031#include <vector>
32
33namespace openpower
34{
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050035namespace misc
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060036{
37
38/**
39 * @brief Calls sbe_enter_mpipl on the SBE in the provided target.
40 * @return void
41 */
42void sbeEnterMpReboot(struct pdbg_target* tgt)
43{
44 using namespace phosphor::logging;
45 int error = 0;
46 if ((error = sbe_mpipl_enter(tgt)) < 0)
47 {
48 log<level::ERR>("Failed to initiate memory preserving reboot");
49 // TODO Create a PEL in the future for this failure case.
50 throw std::system_error(error, std::generic_category(),
51 "Failed to initiate memory preserving reboot");
52 }
53}
54
55/**
56 * @brief initiate memory preserving reboot on each SBE.
57 * @return void
58 */
59void enterMpReboot()
60{
61 using namespace phosphor::logging;
62 struct pdbg_target* target;
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050063 std::vector<pid_t> pidList;
64 bool failed = false;
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060065 pdbg_targets_init(NULL);
66
67 log<level::INFO>("Starting memory preserving reboot");
68 pdbg_for_each_class_target("pib", target)
69 {
70 if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED)
71 {
72 continue;
73 }
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050074
75 pid_t pid = fork();
76
77 if (pid < 0)
78 {
79 log<level::ERR>("Fork failed while starting mp reboot");
80 failed = true;
81 }
82 else if (pid == 0)
83 {
84 sbeEnterMpReboot(target);
85 std::exit(EXIT_SUCCESS);
86 }
87 else
88 {
89 pidList.push_back(std::move(pid));
90 }
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060091 }
92
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050093 for (auto& p : pidList)
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060094 {
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050095 int status = 0;
96 waitpid(p, &status, 0);
97 if (WEXITSTATUS(status))
98 {
99 log<level::ERR>("Memory preserving reboot failed");
100 failed = true;
101 }
102 }
103
104 if (failed)
105 {
106 std::exit(EXIT_FAILURE);
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -0600107 }
108}
109
Brad Bishop63508a72020-10-27 18:55:01 -0400110REGISTER_PROCEDURE("enterMpReboot", enterMpReboot)
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -0600111
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -0500112} // namespace misc
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -0600113} // namespace openpower