blob: ce3fc67ec3799f552eae083fd9b1b3046f046d06 [file] [log] [blame]
Matt Spinler2c05aa72017-02-28 09:48:13 -06001/**
2 * Copyright © 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 */
Edward A. James8316b772017-04-24 14:20:48 -050016
17#include <endian.h>
Matt Spinler2c05aa72017-02-28 09:48:13 -060018#include <experimental/filesystem>
19#include <phosphor-logging/log.hpp>
20#include <regex>
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050021#include <phosphor-logging/elog.hpp>
22#include "elog-errors.hpp"
Matt Spinler2c05aa72017-02-28 09:48:13 -060023#include "targeting.hpp"
24
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050025
Matt Spinler2c05aa72017-02-28 09:48:13 -060026namespace openpower
27{
28namespace targeting
29{
30
31using namespace phosphor::logging;
32namespace fs = std::experimental::filesystem;
33
Matt Spinlerc3bffed2017-03-10 09:05:30 -060034int Target::getCFAMFD()
35{
36 if (cfamFD.get() == nullptr)
37 {
38 cfamFD = std::make_unique<
39 openpower::util::FileDescriptor>(getCFAMPath());
40 }
41
42 return cfamFD->get();
43}
44
Michael Tritzbe407162017-03-30 16:52:24 -050045std::unique_ptr<Target>& Targeting::getTarget(size_t pos)
46{
47 auto search = [pos](const auto& t)
48 {
49 return t->getPos() == pos;
50 };
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
Matt Spinlerc3bffed2017-03-10 09:05:30 -060063
Edward A. James8316b772017-04-24 14:20:48 -050064static uint32_t noEndianSwap(uint32_t data)
65{
66 return data;
67}
68
69static uint32_t endianSwap(uint32_t data)
70{
71 return htobe32(data);
72}
73
Matt Spinler2c05aa72017-02-28 09:48:13 -060074Targeting::Targeting(const std::string& fsiMasterDev,
75 const std::string& fsiSlaveDir) :
76 fsiMasterPath(fsiMasterDev),
77 fsiSlaveBasePath(fsiSlaveDir)
78{
Edward A. James8316b772017-04-24 14:20:48 -050079 swap_endian_t swapper = endianSwap;
80 std::regex exp{"fsi1/slave@([0-9]{2}):00", std::regex::extended};
81
82 if (!fs::exists(fsiMasterPath))
83 {
84 std::regex expOld{"hub@00/slave@([0-9]{2}):00", std::regex::extended};
85
86 //Fall back to old (4.7) path
87 exp = expOld;
88 fsiMasterPath = fsiMasterDevPathOld;
89 fsiSlaveBasePath = fsiSlaveBaseDirOld;
90
91 //And don't swap the endianness of CFAM data
92 swapper = noEndianSwap;
93 }
94
Matt Spinler2c05aa72017-02-28 09:48:13 -060095 //Always create P0, the FSI master.
Edward A. James8316b772017-04-24 14:20:48 -050096 targets.push_back(std::make_unique<Target>(0, fsiMasterPath, swapper));
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050097 try
Matt Spinler2c05aa72017-02-28 09:48:13 -060098 {
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050099 //Find the the remaining P9s dynamically based on which files show up
100 for (auto& file : fs::directory_iterator(fsiSlaveBasePath))
Matt Spinler2c05aa72017-02-28 09:48:13 -0600101 {
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -0500102 std::smatch match;
103 std::string path = file.path();
104 if (std::regex_search(path, match, exp))
Matt Spinler2c05aa72017-02-28 09:48:13 -0600105 {
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -0500106 auto pos = atoi(match[1].str().c_str());
107 if (pos == 0)
108 {
109 log<level::ERR>("Unexpected FSI slave device name found",
110 entry("DEVICE_NAME=%s", path.c_str()));
111 continue;
112 }
113
114 path += "/raw";
115
116 targets.push_back(std::make_unique<Target>(pos, path, swapper));
Matt Spinler2c05aa72017-02-28 09:48:13 -0600117 }
Matt Spinler2c05aa72017-02-28 09:48:13 -0600118 }
119 }
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -0500120 catch (fs::filesystem_error& e)
121 {
122 elog<org::open_power::Proc::CFAM::OpenFailure>(
123 org::open_power::Proc::CFAM::OpenFailure::ERRNO(e.code().value()),
124 org::open_power::Proc::CFAM::OpenFailure::PATH(e.path1().c_str()));
125 }
Matt Spinler2c05aa72017-02-28 09:48:13 -0600126
127 auto sortTargets = [](const std::unique_ptr<Target>& left,
128 const std::unique_ptr<Target>& right)
129 {
130 return left->getPos() < right->getPos();
131 };
132 std::sort(targets.begin(), targets.end(), sortTargets);
133}
134
135}
136}