blob: 99e559ff027add4f5da829fbabf5ff9fde55320c [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
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060019#include <libpdbg.h>
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050020#include <sys/wait.h>
21#include <unistd.h>
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060022
23#include <phosphor-logging/log.hpp>
Brad Bishop5e5d4452020-10-27 19:46:13 -040024
Brad Bishop63508a72020-10-27 18:55:01 -040025#include <system_error>
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060026#include <vector>
27
28namespace openpower
29{
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050030namespace misc
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060031{
32
33/**
34 * @brief Calls sbe_enter_mpipl on the SBE in the provided target.
35 * @return void
36 */
37void sbeEnterMpReboot(struct pdbg_target* tgt)
38{
39 using namespace phosphor::logging;
40 int error = 0;
41 if ((error = sbe_mpipl_enter(tgt)) < 0)
42 {
43 log<level::ERR>("Failed to initiate memory preserving reboot");
44 // TODO Create a PEL in the future for this failure case.
45 throw std::system_error(error, std::generic_category(),
46 "Failed to initiate memory preserving reboot");
47 }
48}
49
50/**
51 * @brief initiate memory preserving reboot on each SBE.
52 * @return void
53 */
54void enterMpReboot()
55{
56 using namespace phosphor::logging;
57 struct pdbg_target* target;
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050058 std::vector<pid_t> pidList;
59 bool failed = false;
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060060 pdbg_targets_init(NULL);
61
62 log<level::INFO>("Starting memory preserving reboot");
63 pdbg_for_each_class_target("pib", target)
64 {
65 if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED)
66 {
67 continue;
68 }
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050069
70 pid_t pid = fork();
71
72 if (pid < 0)
73 {
74 log<level::ERR>("Fork failed while starting mp reboot");
75 failed = true;
76 }
77 else if (pid == 0)
78 {
79 sbeEnterMpReboot(target);
80 std::exit(EXIT_SUCCESS);
81 }
82 else
83 {
84 pidList.push_back(std::move(pid));
85 }
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060086 }
87
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050088 for (auto& p : pidList)
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060089 {
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050090 int status = 0;
91 waitpid(p, &status, 0);
92 if (WEXITSTATUS(status))
93 {
94 log<level::ERR>("Memory preserving reboot failed");
95 failed = true;
96 }
97 }
98
99 if (failed)
100 {
101 std::exit(EXIT_FAILURE);
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -0600102 }
103}
104
Brad Bishop63508a72020-10-27 18:55:01 -0400105REGISTER_PROCEDURE("enterMpReboot", enterMpReboot)
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -0600106
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -0500107} // namespace misc
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -0600108} // namespace openpower