blob: 301bf7d472c651c771d2b2f7dcbe2cae7006f878 [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
Jayanth Othayothf8c14bc2021-06-29 06:13:19 -050025#include <fmt/format.h>
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050026#include <sys/wait.h>
27#include <unistd.h>
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060028
29#include <phosphor-logging/log.hpp>
Brad Bishop5e5d4452020-10-27 19:46:13 -040030
Brad Bishop63508a72020-10-27 18:55:01 -040031#include <system_error>
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060032#include <vector>
33
34namespace openpower
35{
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050036namespace misc
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060037{
38
39/**
40 * @brief Calls sbe_enter_mpipl on the SBE in the provided target.
41 * @return void
42 */
43void sbeEnterMpReboot(struct pdbg_target* tgt)
44{
45 using namespace phosphor::logging;
46 int error = 0;
47 if ((error = sbe_mpipl_enter(tgt)) < 0)
48 {
49 log<level::ERR>("Failed to initiate memory preserving reboot");
50 // TODO Create a PEL in the future for this failure case.
51 throw std::system_error(error, std::generic_category(),
52 "Failed to initiate memory preserving reboot");
53 }
Jayanth Othayothf8c14bc2021-06-29 06:13:19 -050054
55 log<level::INFO>(
56 fmt::format("Enter MPIPL completed on proc({})", pdbg_target_index(tgt))
57 .c_str());
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060058}
59
60/**
61 * @brief initiate memory preserving reboot on each SBE.
62 * @return void
63 */
64void enterMpReboot()
65{
66 using namespace phosphor::logging;
67 struct pdbg_target* target;
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050068 std::vector<pid_t> pidList;
69 bool failed = false;
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060070 pdbg_targets_init(NULL);
71
72 log<level::INFO>("Starting memory preserving reboot");
73 pdbg_for_each_class_target("pib", target)
74 {
75 if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED)
76 {
77 continue;
78 }
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050079
80 pid_t pid = fork();
81
82 if (pid < 0)
83 {
84 log<level::ERR>("Fork failed while starting mp reboot");
85 failed = true;
86 }
87 else if (pid == 0)
88 {
89 sbeEnterMpReboot(target);
90 std::exit(EXIT_SUCCESS);
91 }
92 else
93 {
94 pidList.push_back(std::move(pid));
95 }
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060096 }
97
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -050098 for (auto& p : pidList)
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -060099 {
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -0500100 int status = 0;
101 waitpid(p, &status, 0);
102 if (WEXITSTATUS(status))
103 {
104 log<level::ERR>("Memory preserving reboot failed");
105 failed = true;
106 }
107 }
108
109 if (failed)
110 {
111 std::exit(EXIT_FAILURE);
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -0600112 }
113}
114
Brad Bishop63508a72020-10-27 18:55:01 -0400115REGISTER_PROCEDURE("enterMpReboot", enterMpReboot)
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -0600116
Dhruvaraj Subhashchandran285f73e2020-05-15 03:35:02 -0500117} // namespace misc
Dhruvaraj Subhashchandran08fc2642020-02-17 01:47:05 -0600118} // namespace openpower