12c05aa76SMatt Spinler /** 22c05aa76SMatt Spinler * Copyright © 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 */ 162c05aa76SMatt Spinler #include <experimental/filesystem> 172c05aa76SMatt Spinler #include <phosphor-logging/log.hpp> 182c05aa76SMatt Spinler #include <regex> 192c05aa76SMatt Spinler #include "targeting.hpp" 202c05aa76SMatt Spinler 212c05aa76SMatt Spinler namespace openpower 222c05aa76SMatt Spinler { 232c05aa76SMatt Spinler namespace targeting 242c05aa76SMatt Spinler { 252c05aa76SMatt Spinler 262c05aa76SMatt Spinler using namespace phosphor::logging; 272c05aa76SMatt Spinler namespace fs = std::experimental::filesystem; 282c05aa76SMatt Spinler 29c3bffed7SMatt Spinler int Target::getCFAMFD() 30c3bffed7SMatt Spinler { 31c3bffed7SMatt Spinler if (cfamFD.get() == nullptr) 32c3bffed7SMatt Spinler { 33c3bffed7SMatt Spinler cfamFD = std::make_unique< 34c3bffed7SMatt Spinler openpower::util::FileDescriptor>(getCFAMPath()); 35c3bffed7SMatt Spinler } 36c3bffed7SMatt Spinler 37c3bffed7SMatt Spinler return cfamFD->get(); 38c3bffed7SMatt Spinler } 39c3bffed7SMatt Spinler 40*be407166SMichael Tritz std::unique_ptr<Target>& Targeting::getTarget(size_t pos) 41*be407166SMichael Tritz { 42*be407166SMichael Tritz auto search = [pos](const auto& t) 43*be407166SMichael Tritz { 44*be407166SMichael Tritz return t->getPos() == pos; 45*be407166SMichael Tritz }; 46*be407166SMichael Tritz 47*be407166SMichael Tritz auto target = find_if(targets.begin(), targets.end(), search); 48*be407166SMichael Tritz if (target == targets.end()) 49*be407166SMichael Tritz { 50*be407166SMichael Tritz throw std::runtime_error("Target not found: " + std::to_string(pos)); 51*be407166SMichael Tritz } 52*be407166SMichael Tritz else 53*be407166SMichael Tritz { 54*be407166SMichael Tritz return *target; 55*be407166SMichael Tritz } 56*be407166SMichael Tritz } 57*be407166SMichael Tritz 58c3bffed7SMatt Spinler 592c05aa76SMatt Spinler Targeting::Targeting(const std::string& fsiMasterDev, 602c05aa76SMatt Spinler const std::string& fsiSlaveDir) : 612c05aa76SMatt Spinler fsiMasterPath(fsiMasterDev), 622c05aa76SMatt Spinler fsiSlaveBasePath(fsiSlaveDir) 632c05aa76SMatt Spinler { 642c05aa76SMatt Spinler //Always create P0, the FSI master. 652c05aa76SMatt Spinler targets.push_back(std::make_unique<Target>(0, fsiMasterPath)); 662c05aa76SMatt Spinler 672c05aa76SMatt Spinler //Find the the remaining P9s dynamically based on which files show up 68fabe92e8SMatt Spinler std::regex exp{"hub@00/slave@([0-9]{2}):00", std::regex::extended}; 692c05aa76SMatt Spinler 702c05aa76SMatt Spinler for (auto& file : fs::directory_iterator(fsiSlaveBasePath)) 712c05aa76SMatt Spinler { 722c05aa76SMatt Spinler std::smatch match; 732c05aa76SMatt Spinler std::string path = file.path(); 742c05aa76SMatt Spinler if (std::regex_search(path, match, exp)) 752c05aa76SMatt Spinler { 762c05aa76SMatt Spinler auto pos = atoi(match[1].str().c_str()); 772c05aa76SMatt Spinler if (pos == 0) 782c05aa76SMatt Spinler { 792c05aa76SMatt Spinler log<level::ERR>("Unexpected FSI slave device name found", 80fabe92e8SMatt Spinler entry("DEVICE_NAME=%s", path.c_str())); 812c05aa76SMatt Spinler continue; 822c05aa76SMatt Spinler } 832c05aa76SMatt Spinler 842c05aa76SMatt Spinler path += "/raw"; 852c05aa76SMatt Spinler 862c05aa76SMatt Spinler targets.push_back(std::make_unique<Target>(pos, path)); 872c05aa76SMatt Spinler } 882c05aa76SMatt Spinler } 892c05aa76SMatt Spinler 902c05aa76SMatt Spinler auto sortTargets = [](const std::unique_ptr<Target>& left, 912c05aa76SMatt Spinler const std::unique_ptr<Target>& right) 922c05aa76SMatt Spinler { 932c05aa76SMatt Spinler return left->getPos() < right->getPos(); 942c05aa76SMatt Spinler }; 952c05aa76SMatt Spinler std::sort(targets.begin(), targets.end(), sortTargets); 962c05aa76SMatt Spinler } 972c05aa76SMatt Spinler 982c05aa76SMatt Spinler } 992c05aa76SMatt Spinler } 100