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