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 21*56d14d04SBrad Bishop #include <filesystem> 22a231ceb4SMatt Spinler #include <phosphor-logging/elog-errors.hpp> 23f78d9042SPatrick Venture #include <phosphor-logging/elog.hpp> 242c05aa76SMatt Spinler #include <phosphor-logging/log.hpp> 252c05aa76SMatt Spinler #include <regex> 26a231ceb4SMatt Spinler #include <xyz/openbmc_project/Common/File/error.hpp> 2718b0786aSDhruvaraj Subhashchandran 282c05aa76SMatt Spinler namespace openpower 292c05aa76SMatt Spinler { 302c05aa76SMatt Spinler namespace targeting 312c05aa76SMatt Spinler { 322c05aa76SMatt Spinler 332c05aa76SMatt Spinler using namespace phosphor::logging; 34a231ceb4SMatt Spinler namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; 352c05aa76SMatt Spinler 36c3bffed7SMatt Spinler int Target::getCFAMFD() 37c3bffed7SMatt Spinler { 38c3bffed7SMatt Spinler if (cfamFD.get() == nullptr) 39c3bffed7SMatt Spinler { 40f78d9042SPatrick Venture cfamFD = 41f78d9042SPatrick Venture std::make_unique<openpower::util::FileDescriptor>(getCFAMPath()); 42c3bffed7SMatt Spinler } 43c3bffed7SMatt Spinler 44c3bffed7SMatt Spinler return cfamFD->get(); 45c3bffed7SMatt Spinler } 46c3bffed7SMatt Spinler 47be407166SMichael Tritz std::unique_ptr<Target>& Targeting::getTarget(size_t pos) 48be407166SMichael Tritz { 49f78d9042SPatrick Venture auto search = [pos](const auto& t) { return t->getPos() == pos; }; 50be407166SMichael Tritz 51be407166SMichael Tritz auto target = find_if(targets.begin(), targets.end(), search); 52be407166SMichael Tritz if (target == targets.end()) 53be407166SMichael Tritz { 54be407166SMichael Tritz throw std::runtime_error("Target not found: " + std::to_string(pos)); 55be407166SMichael Tritz } 56be407166SMichael Tritz else 57be407166SMichael Tritz { 58be407166SMichael Tritz return *target; 59be407166SMichael Tritz } 60be407166SMichael Tritz } 61be407166SMichael Tritz 622c05aa76SMatt Spinler Targeting::Targeting(const std::string& fsiMasterDev, 632c05aa76SMatt Spinler const std::string& fsiSlaveDir) : 642c05aa76SMatt Spinler fsiMasterPath(fsiMasterDev), 652c05aa76SMatt Spinler 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 74*56d14d04SBrad 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 } 94*56d14d04SBrad Bishop catch (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