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
265e5d4451SBrad Bishop #include <filesystem>
275e5d4451SBrad Bishop #include <regex>
285e5d4451SBrad 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
getCFAMFD()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
getTarget(size_t pos)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
Targeting(const std::string & fsiMasterDev,const std::string & fsiSlaveDir)632c05aa76SMatt Spinler Targeting::Targeting(const std::string& fsiMasterDev,
642c05aa76SMatt Spinler const std::string& fsiSlaveDir) :
65*1e43be06SPatrick Williams fsiMasterPath(fsiMasterDev), fsiSlaveBasePath(fsiSlaveDir)
662c05aa76SMatt Spinler {
678316b77bSEdward A. James std::regex exp{"fsi1/slave@([0-9]{2}):00", std::regex::extended};
688316b77bSEdward A. James
692c05aa76SMatt Spinler // Always create P0, the FSI master.
70afa1d226SJoel Stanley targets.push_back(std::make_unique<Target>(0, fsiMasterPath));
7118b0786aSDhruvaraj Subhashchandran try
7218b0786aSDhruvaraj Subhashchandran {
732c05aa76SMatt Spinler // Find the the remaining P9s dynamically based on which files show up
7456d14d04SBrad Bishop for (auto& file : std::filesystem::directory_iterator(fsiSlaveBasePath))
752c05aa76SMatt Spinler {
762c05aa76SMatt Spinler std::smatch match;
772c05aa76SMatt Spinler std::string path = file.path();
782c05aa76SMatt Spinler if (std::regex_search(path, match, exp))
792c05aa76SMatt Spinler {
802c05aa76SMatt Spinler auto pos = atoi(match[1].str().c_str());
812c05aa76SMatt Spinler if (pos == 0)
822c05aa76SMatt Spinler {
832c05aa76SMatt Spinler log<level::ERR>("Unexpected FSI slave device name found",
84fabe92e8SMatt Spinler entry("DEVICE_NAME=%s", path.c_str()));
852c05aa76SMatt Spinler continue;
862c05aa76SMatt Spinler }
872c05aa76SMatt Spinler
882c05aa76SMatt Spinler path += "/raw";
892c05aa76SMatt Spinler
90afa1d226SJoel Stanley targets.push_back(std::make_unique<Target>(pos, path));
912c05aa76SMatt Spinler }
922c05aa76SMatt Spinler }
9318b0786aSDhruvaraj Subhashchandran }
941a9a5a6aSPatrick Williams catch (const std::filesystem::filesystem_error& e)
9518b0786aSDhruvaraj Subhashchandran {
96a231ceb4SMatt Spinler using metadata = xyz::openbmc_project::Common::File::Open;
97a231ceb4SMatt Spinler
98f78d9042SPatrick Venture elog<file_error::Open>(metadata::ERRNO(e.code().value()),
99a231ceb4SMatt Spinler metadata::PATH(e.path1().c_str()));
10018b0786aSDhruvaraj Subhashchandran }
1012c05aa76SMatt Spinler
1022c05aa76SMatt Spinler auto sortTargets = [](const std::unique_ptr<Target>& left,
103f78d9042SPatrick Venture const std::unique_ptr<Target>& right) {
1042c05aa76SMatt Spinler return left->getPos() < right->getPos();
1052c05aa76SMatt Spinler };
1062c05aa76SMatt Spinler std::sort(targets.begin(), targets.end(), sortTargets);
1072c05aa76SMatt Spinler }
1082c05aa76SMatt Spinler
109f78d9042SPatrick Venture } // namespace targeting
110f78d9042SPatrick Venture } // namespace openpower
111