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