blob: e568bcddce8a006b6b66619828ecf38efe99744a [file] [log] [blame]
Matt Spinler2c05aa72017-02-28 09:48:13 -06001/**
Patrick Venturee84b4dd2018-11-01 16:06:31 -07002 * Copyright (C) 2017 IBM Corporation
Matt Spinler2c05aa72017-02-28 09:48:13 -06003 *
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 */
Edward A. James8316b772017-04-24 14:20:48 -050016
Patrick Venturef78d9042018-11-01 15:39:53 -070017#include "targeting.hpp"
18
Edward A. James8316b772017-04-24 14:20:48 -050019#include <endian.h>
Patrick Venturef78d9042018-11-01 15:39:53 -070020
Matt Spinlera231ceb2017-10-04 11:26:09 -050021#include <phosphor-logging/elog-errors.hpp>
Patrick Venturef78d9042018-11-01 15:39:53 -070022#include <phosphor-logging/elog.hpp>
Matt Spinler2c05aa72017-02-28 09:48:13 -060023#include <phosphor-logging/log.hpp>
Matt Spinlera231ceb2017-10-04 11:26:09 -050024#include <xyz/openbmc_project/Common/File/error.hpp>
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050025
Brad Bishop5e5d4452020-10-27 19:46:13 -040026#include <filesystem>
27#include <regex>
28
Matt Spinler2c05aa72017-02-28 09:48:13 -060029namespace openpower
30{
31namespace targeting
32{
33
34using namespace phosphor::logging;
Matt Spinlera231ceb2017-10-04 11:26:09 -050035namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
Matt Spinler2c05aa72017-02-28 09:48:13 -060036
Matt Spinlerc3bffed2017-03-10 09:05:30 -060037int Target::getCFAMFD()
38{
39 if (cfamFD.get() == nullptr)
40 {
Patrick Venturef78d9042018-11-01 15:39:53 -070041 cfamFD =
42 std::make_unique<openpower::util::FileDescriptor>(getCFAMPath());
Matt Spinlerc3bffed2017-03-10 09:05:30 -060043 }
44
45 return cfamFD->get();
46}
47
Michael Tritzbe407162017-03-30 16:52:24 -050048std::unique_ptr<Target>& Targeting::getTarget(size_t pos)
49{
Patrick Venturef78d9042018-11-01 15:39:53 -070050 auto search = [pos](const auto& t) { return t->getPos() == pos; };
Michael Tritzbe407162017-03-30 16:52:24 -050051
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
Matt Spinler2c05aa72017-02-28 09:48:13 -060063Targeting::Targeting(const std::string& fsiMasterDev,
64 const std::string& fsiSlaveDir) :
65 fsiMasterPath(fsiMasterDev),
66 fsiSlaveBasePath(fsiSlaveDir)
67{
Edward A. James8316b772017-04-24 14:20:48 -050068 std::regex exp{"fsi1/slave@([0-9]{2}):00", std::regex::extended};
69
Patrick Venturef78d9042018-11-01 15:39:53 -070070 // Always create P0, the FSI master.
Joel Stanleyafa1d222019-09-27 15:35:04 +093071 targets.push_back(std::make_unique<Target>(0, fsiMasterPath));
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050072 try
Matt Spinler2c05aa72017-02-28 09:48:13 -060073 {
Patrick Venturef78d9042018-11-01 15:39:53 -070074 // Find the the remaining P9s dynamically based on which files show up
Brad Bishop56d14d02020-10-09 15:28:51 -040075 for (auto& file : std::filesystem::directory_iterator(fsiSlaveBasePath))
Matt Spinler2c05aa72017-02-28 09:48:13 -060076 {
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050077 std::smatch match;
78 std::string path = file.path();
79 if (std::regex_search(path, match, exp))
Matt Spinler2c05aa72017-02-28 09:48:13 -060080 {
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050081 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
Joel Stanleyafa1d222019-09-27 15:35:04 +093091 targets.push_back(std::make_unique<Target>(pos, path));
Matt Spinler2c05aa72017-02-28 09:48:13 -060092 }
Matt Spinler2c05aa72017-02-28 09:48:13 -060093 }
94 }
Patrick Williams1a9a5a62021-10-06 13:05:06 -050095 catch (const std::filesystem::filesystem_error& e)
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050096 {
Matt Spinlera231ceb2017-10-04 11:26:09 -050097 using metadata = xyz::openbmc_project::Common::File::Open;
98
Patrick Venturef78d9042018-11-01 15:39:53 -070099 elog<file_error::Open>(metadata::ERRNO(e.code().value()),
100 metadata::PATH(e.path1().c_str()));
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -0500101 }
Matt Spinler2c05aa72017-02-28 09:48:13 -0600102
103 auto sortTargets = [](const std::unique_ptr<Target>& left,
Patrick Venturef78d9042018-11-01 15:39:53 -0700104 const std::unique_ptr<Target>& right) {
Matt Spinler2c05aa72017-02-28 09:48:13 -0600105 return left->getPos() < right->getPos();
106 };
107 std::sort(targets.begin(), targets.end(), sortTargets);
108}
109
Patrick Venturef78d9042018-11-01 15:39:53 -0700110} // namespace targeting
111} // namespace openpower