blob: 37bed58cc66221a18118fe7e7e8fa5cc39a5dd92 [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
Brad Bishop56d14d02020-10-09 15:28:51 -040021#include <filesystem>
Matt Spinlera231ceb2017-10-04 11:26:09 -050022#include <phosphor-logging/elog-errors.hpp>
Patrick Venturef78d9042018-11-01 15:39:53 -070023#include <phosphor-logging/elog.hpp>
Matt Spinler2c05aa72017-02-28 09:48:13 -060024#include <phosphor-logging/log.hpp>
25#include <regex>
Matt Spinlera231ceb2017-10-04 11:26:09 -050026#include <xyz/openbmc_project/Common/File/error.hpp>
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050027
Matt Spinler2c05aa72017-02-28 09:48:13 -060028namespace openpower
29{
30namespace targeting
31{
32
33using namespace phosphor::logging;
Matt Spinlera231ceb2017-10-04 11:26:09 -050034namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
Matt Spinler2c05aa72017-02-28 09:48:13 -060035
Matt Spinlerc3bffed2017-03-10 09:05:30 -060036int Target::getCFAMFD()
37{
38 if (cfamFD.get() == nullptr)
39 {
Patrick Venturef78d9042018-11-01 15:39:53 -070040 cfamFD =
41 std::make_unique<openpower::util::FileDescriptor>(getCFAMPath());
Matt Spinlerc3bffed2017-03-10 09:05:30 -060042 }
43
44 return cfamFD->get();
45}
46
Michael Tritzbe407162017-03-30 16:52:24 -050047std::unique_ptr<Target>& Targeting::getTarget(size_t pos)
48{
Patrick Venturef78d9042018-11-01 15:39:53 -070049 auto search = [pos](const auto& t) { return t->getPos() == pos; };
Michael Tritzbe407162017-03-30 16:52:24 -050050
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
Matt Spinler2c05aa72017-02-28 09:48:13 -060062Targeting::Targeting(const std::string& fsiMasterDev,
63 const std::string& fsiSlaveDir) :
64 fsiMasterPath(fsiMasterDev),
65 fsiSlaveBasePath(fsiSlaveDir)
66{
Edward A. James8316b772017-04-24 14:20:48 -050067 std::regex exp{"fsi1/slave@([0-9]{2}):00", std::regex::extended};
68
Patrick Venturef78d9042018-11-01 15:39:53 -070069 // Always create P0, the FSI master.
Joel Stanleyafa1d222019-09-27 15:35:04 +093070 targets.push_back(std::make_unique<Target>(0, fsiMasterPath));
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050071 try
Matt Spinler2c05aa72017-02-28 09:48:13 -060072 {
Patrick Venturef78d9042018-11-01 15:39:53 -070073 // Find the the remaining P9s dynamically based on which files show up
Brad Bishop56d14d02020-10-09 15:28:51 -040074 for (auto& file : std::filesystem::directory_iterator(fsiSlaveBasePath))
Matt Spinler2c05aa72017-02-28 09:48:13 -060075 {
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050076 std::smatch match;
77 std::string path = file.path();
78 if (std::regex_search(path, match, exp))
Matt Spinler2c05aa72017-02-28 09:48:13 -060079 {
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050080 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
Joel Stanleyafa1d222019-09-27 15:35:04 +093090 targets.push_back(std::make_unique<Target>(pos, path));
Matt Spinler2c05aa72017-02-28 09:48:13 -060091 }
Matt Spinler2c05aa72017-02-28 09:48:13 -060092 }
93 }
Brad Bishop56d14d02020-10-09 15:28:51 -040094 catch (std::filesystem::filesystem_error& e)
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050095 {
Matt Spinlera231ceb2017-10-04 11:26:09 -050096 using metadata = xyz::openbmc_project::Common::File::Open;
97
Patrick Venturef78d9042018-11-01 15:39:53 -070098 elog<file_error::Open>(metadata::ERRNO(e.code().value()),
99 metadata::PATH(e.path1().c_str()));
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -0500100 }
Matt Spinler2c05aa72017-02-28 09:48:13 -0600101
102 auto sortTargets = [](const std::unique_ptr<Target>& left,
Patrick Venturef78d9042018-11-01 15:39:53 -0700103 const std::unique_ptr<Target>& right) {
Matt Spinler2c05aa72017-02-28 09:48:13 -0600104 return left->getPos() < right->getPos();
105 };
106 std::sort(targets.begin(), targets.end(), sortTargets);
107}
108
Patrick Venturef78d9042018-11-01 15:39:53 -0700109} // namespace targeting
110} // namespace openpower