| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 1 | /** | 
|  | 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 Bishop | 63508a7 | 2020-10-27 18:55:01 -0400 | [diff] [blame] | 17 | #include "registration.hpp" | 
|  | 18 |  | 
| Jayanth Othayoth | 785cf6a | 2021-06-28 00:16:35 -0500 | [diff] [blame] | 19 | extern "C" | 
|  | 20 | { | 
| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 21 | #include <libpdbg.h> | 
| Jayanth Othayoth | 785cf6a | 2021-06-28 00:16:35 -0500 | [diff] [blame] | 22 | #include <libpdbg_sbe.h> | 
|  | 23 | } | 
|  | 24 |  | 
| Jayanth Othayoth | f8c14bc | 2021-06-29 06:13:19 -0500 | [diff] [blame] | 25 | #include <fmt/format.h> | 
| Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 26 | #include <sys/wait.h> | 
|  | 27 | #include <unistd.h> | 
| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 28 |  | 
|  | 29 | #include <phosphor-logging/log.hpp> | 
| Brad Bishop | 5e5d445 | 2020-10-27 19:46:13 -0400 | [diff] [blame] | 30 |  | 
| Brad Bishop | 63508a7 | 2020-10-27 18:55:01 -0400 | [diff] [blame] | 31 | #include <system_error> | 
| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 32 | #include <vector> | 
|  | 33 |  | 
|  | 34 | namespace openpower | 
|  | 35 | { | 
| Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 36 | namespace misc | 
| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 37 | { | 
|  | 38 |  | 
|  | 39 | /** | 
|  | 40 | * @brief Calls sbe_enter_mpipl on the SBE in the provided target. | 
|  | 41 | * @return void | 
|  | 42 | */ | 
|  | 43 | void 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 Othayoth | f8c14bc | 2021-06-29 06:13:19 -0500 | [diff] [blame] | 54 |  | 
|  | 55 | log<level::INFO>( | 
|  | 56 | fmt::format("Enter MPIPL completed on proc({})", pdbg_target_index(tgt)) | 
|  | 57 | .c_str()); | 
| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
|  | 60 | /** | 
|  | 61 | * @brief initiate memory preserving reboot on each SBE. | 
|  | 62 | * @return void | 
|  | 63 | */ | 
|  | 64 | void enterMpReboot() | 
|  | 65 | { | 
|  | 66 | using namespace phosphor::logging; | 
|  | 67 | struct pdbg_target* target; | 
| Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 68 | std::vector<pid_t> pidList; | 
|  | 69 | bool failed = false; | 
| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 70 | 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 Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 79 |  | 
|  | 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 Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 98 | for (auto& p : pidList) | 
| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 99 | { | 
| Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 100 | 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 Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 112 | } | 
|  | 113 | } | 
|  | 114 |  | 
| Brad Bishop | 63508a7 | 2020-10-27 18:55:01 -0400 | [diff] [blame] | 115 | REGISTER_PROCEDURE("enterMpReboot", enterMpReboot) | 
| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 116 |  | 
| Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 117 | } // namespace misc | 
| Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 118 | } // namespace openpower |