xref: /openbmc/openpower-proc-control/targeting.cpp (revision 5e5d44516abb293483a6fb80175c8858a4b1aa90)
12c05aa76SMatt Spinler /**
2e84b4ddbSPatrick Venture  * Copyright (C) 2017 IBM Corporation
32c05aa76SMatt Spinler  *
42c05aa76SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
52c05aa76SMatt Spinler  * you may not use this file except in compliance with the License.
62c05aa76SMatt Spinler  * You may obtain a copy of the License at
72c05aa76SMatt Spinler  *
82c05aa76SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
92c05aa76SMatt Spinler  *
102c05aa76SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
112c05aa76SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
122c05aa76SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132c05aa76SMatt Spinler  * See the License for the specific language governing permissions and
142c05aa76SMatt Spinler  * limitations under the License.
152c05aa76SMatt Spinler  */
168316b77bSEdward A. James 
17f78d9042SPatrick Venture #include "targeting.hpp"
18f78d9042SPatrick Venture 
198316b77bSEdward A. James #include <endian.h>
20f78d9042SPatrick Venture 
21a231ceb4SMatt Spinler #include <phosphor-logging/elog-errors.hpp>
22f78d9042SPatrick Venture #include <phosphor-logging/elog.hpp>
232c05aa76SMatt Spinler #include <phosphor-logging/log.hpp>
24a231ceb4SMatt Spinler #include <xyz/openbmc_project/Common/File/error.hpp>
2518b0786aSDhruvaraj Subhashchandran 
26*5e5d4451SBrad Bishop #include <filesystem>
27*5e5d4451SBrad Bishop #include <regex>
28*5e5d4451SBrad Bishop 
292c05aa76SMatt Spinler namespace openpower
302c05aa76SMatt Spinler {
312c05aa76SMatt Spinler namespace targeting
322c05aa76SMatt Spinler {
332c05aa76SMatt Spinler 
342c05aa76SMatt Spinler using namespace phosphor::logging;
35a231ceb4SMatt Spinler namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
362c05aa76SMatt Spinler 
37c3bffed7SMatt Spinler int Target::getCFAMFD()
38c3bffed7SMatt Spinler {
39c3bffed7SMatt Spinler     if (cfamFD.get() == nullptr)
40c3bffed7SMatt Spinler     {
41f78d9042SPatrick Venture         cfamFD =
42f78d9042SPatrick Venture             std::make_unique<openpower::util::FileDescriptor>(getCFAMPath());
43c3bffed7SMatt Spinler     }
44c3bffed7SMatt Spinler 
45c3bffed7SMatt Spinler     return cfamFD->get();
46c3bffed7SMatt Spinler }
47c3bffed7SMatt Spinler 
48be407166SMichael Tritz std::unique_ptr<Target>& Targeting::getTarget(size_t pos)
49be407166SMichael Tritz {
50f78d9042SPatrick Venture     auto search = [pos](const auto& t) { return t->getPos() == pos; };
51be407166SMichael Tritz 
52be407166SMichael Tritz     auto target = find_if(targets.begin(), targets.end(), search);
53be407166SMichael Tritz     if (target == targets.end())
54be407166SMichael Tritz     {
55be407166SMichael Tritz         throw std::runtime_error("Target not found: " + std::to_string(pos));
56be407166SMichael Tritz     }
57be407166SMichael Tritz     else
58be407166SMichael Tritz     {
59be407166SMichael Tritz         return *target;
60be407166SMichael Tritz     }
61be407166SMichael Tritz }
62be407166SMichael Tritz 
632c05aa76SMatt Spinler Targeting::Targeting(const std::string& fsiMasterDev,
642c05aa76SMatt Spinler                      const std::string& fsiSlaveDir) :
652c05aa76SMatt Spinler     fsiMasterPath(fsiMasterDev),
662c05aa76SMatt Spinler     fsiSlaveBasePath(fsiSlaveDir)
672c05aa76SMatt Spinler {
688316b77bSEdward A. James     std::regex exp{"fsi1/slave@([0-9]{2}):00", std::regex::extended};
698316b77bSEdward A. James 
702c05aa76SMatt Spinler     // Always create P0, the FSI master.
71afa1d226SJoel Stanley     targets.push_back(std::make_unique<Target>(0, fsiMasterPath));
7218b0786aSDhruvaraj Subhashchandran     try
7318b0786aSDhruvaraj Subhashchandran     {
742c05aa76SMatt Spinler         // Find the the remaining P9s dynamically based on which files show up
7556d14d04SBrad Bishop         for (auto& file : std::filesystem::directory_iterator(fsiSlaveBasePath))
762c05aa76SMatt Spinler         {
772c05aa76SMatt Spinler             std::smatch match;
782c05aa76SMatt Spinler             std::string path = file.path();
792c05aa76SMatt Spinler             if (std::regex_search(path, match, exp))
802c05aa76SMatt Spinler             {
812c05aa76SMatt Spinler                 auto pos = atoi(match[1].str().c_str());
822c05aa76SMatt Spinler                 if (pos == 0)
832c05aa76SMatt Spinler                 {
842c05aa76SMatt Spinler                     log<level::ERR>("Unexpected FSI slave device name found",
85fabe92e8SMatt Spinler                                     entry("DEVICE_NAME=%s", path.c_str()));
862c05aa76SMatt Spinler                     continue;
872c05aa76SMatt Spinler                 }
882c05aa76SMatt Spinler 
892c05aa76SMatt Spinler                 path += "/raw";
902c05aa76SMatt Spinler 
91afa1d226SJoel Stanley                 targets.push_back(std::make_unique<Target>(pos, path));
922c05aa76SMatt Spinler             }
932c05aa76SMatt Spinler         }
9418b0786aSDhruvaraj Subhashchandran     }
9556d14d04SBrad Bishop     catch (std::filesystem::filesystem_error& e)
9618b0786aSDhruvaraj Subhashchandran     {
97a231ceb4SMatt Spinler         using metadata = xyz::openbmc_project::Common::File::Open;
98a231ceb4SMatt Spinler 
99f78d9042SPatrick Venture         elog<file_error::Open>(metadata::ERRNO(e.code().value()),
100a231ceb4SMatt Spinler                                metadata::PATH(e.path1().c_str()));
10118b0786aSDhruvaraj Subhashchandran     }
1022c05aa76SMatt Spinler 
1032c05aa76SMatt Spinler     auto sortTargets = [](const std::unique_ptr<Target>& left,
104f78d9042SPatrick Venture                           const std::unique_ptr<Target>& right) {
1052c05aa76SMatt Spinler         return left->getPos() < right->getPos();
1062c05aa76SMatt Spinler     };
1072c05aa76SMatt Spinler     std::sort(targets.begin(), targets.end(), sortTargets);
1082c05aa76SMatt Spinler }
1092c05aa76SMatt Spinler 
110f78d9042SPatrick Venture } // namespace targeting
111f78d9042SPatrick Venture } // namespace openpower
112