Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 1 | /** |
Patrick Venture | e84b4dd | 2018-11-01 16:06:31 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 IBM Corporation |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 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 | */ |
Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 16 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 17 | #include "targeting.hpp" |
| 18 | |
Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 19 | #include <endian.h> |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 20 | |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 21 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 22 | #include <phosphor-logging/elog.hpp> |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 23 | #include <phosphor-logging/log.hpp> |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 24 | #include <xyz/openbmc_project/Common/File/error.hpp> |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 25 | |
Brad Bishop | 5e5d445 | 2020-10-27 19:46:13 -0400 | [diff] [blame] | 26 | #include <filesystem> |
| 27 | #include <regex> |
| 28 | |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 29 | namespace openpower |
| 30 | { |
| 31 | namespace targeting |
| 32 | { |
| 33 | |
| 34 | using namespace phosphor::logging; |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 35 | namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 36 | |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 37 | int Target::getCFAMFD() |
| 38 | { |
| 39 | if (cfamFD.get() == nullptr) |
| 40 | { |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 41 | cfamFD = |
| 42 | std::make_unique<openpower::util::FileDescriptor>(getCFAMPath()); |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | return cfamFD->get(); |
| 46 | } |
| 47 | |
Michael Tritz | be40716 | 2017-03-30 16:52:24 -0500 | [diff] [blame] | 48 | std::unique_ptr<Target>& Targeting::getTarget(size_t pos) |
| 49 | { |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 50 | auto search = [pos](const auto& t) { return t->getPos() == pos; }; |
Michael Tritz | be40716 | 2017-03-30 16:52:24 -0500 | [diff] [blame] | 51 | |
| 52 | auto target = find_if(targets.begin(), targets.end(), search); |
| 53 | if (target == targets.end()) |
| 54 | { |
| 55 | throw std::runtime_error("Target not found: " + std::to_string(pos)); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | return *target; |
| 60 | } |
| 61 | } |
| 62 | |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 63 | Targeting::Targeting(const std::string& fsiMasterDev, |
| 64 | const std::string& fsiSlaveDir) : |
| 65 | fsiMasterPath(fsiMasterDev), |
| 66 | fsiSlaveBasePath(fsiSlaveDir) |
| 67 | { |
Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 68 | std::regex exp{"fsi1/slave@([0-9]{2}):00", std::regex::extended}; |
| 69 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 70 | // Always create P0, the FSI master. |
Joel Stanley | afa1d22 | 2019-09-27 15:35:04 +0930 | [diff] [blame] | 71 | targets.push_back(std::make_unique<Target>(0, fsiMasterPath)); |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 72 | try |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 73 | { |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 74 | // Find the the remaining P9s dynamically based on which files show up |
Brad Bishop | 56d14d0 | 2020-10-09 15:28:51 -0400 | [diff] [blame] | 75 | for (auto& file : std::filesystem::directory_iterator(fsiSlaveBasePath)) |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 76 | { |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 77 | std::smatch match; |
| 78 | std::string path = file.path(); |
| 79 | if (std::regex_search(path, match, exp)) |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 80 | { |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 81 | auto pos = atoi(match[1].str().c_str()); |
| 82 | if (pos == 0) |
| 83 | { |
| 84 | log<level::ERR>("Unexpected FSI slave device name found", |
| 85 | entry("DEVICE_NAME=%s", path.c_str())); |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | path += "/raw"; |
| 90 | |
Joel Stanley | afa1d22 | 2019-09-27 15:35:04 +0930 | [diff] [blame] | 91 | targets.push_back(std::make_unique<Target>(pos, path)); |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 92 | } |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 93 | } |
| 94 | } |
Brad Bishop | 56d14d0 | 2020-10-09 15:28:51 -0400 | [diff] [blame] | 95 | catch (std::filesystem::filesystem_error& e) |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 96 | { |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 97 | using metadata = xyz::openbmc_project::Common::File::Open; |
| 98 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 99 | elog<file_error::Open>(metadata::ERRNO(e.code().value()), |
| 100 | metadata::PATH(e.path1().c_str())); |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 101 | } |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 102 | |
| 103 | auto sortTargets = [](const std::unique_ptr<Target>& left, |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 104 | const std::unique_ptr<Target>& right) { |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 105 | return left->getPos() < right->getPos(); |
| 106 | }; |
| 107 | std::sort(targets.begin(), targets.end(), sortTargets); |
| 108 | } |
| 109 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 110 | } // namespace targeting |
| 111 | } // namespace openpower |