Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 1 | /** |
| 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. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 16 | |
| 17 | #include <endian.h> |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 18 | #include <experimental/filesystem> |
| 19 | #include <phosphor-logging/log.hpp> |
| 20 | #include <regex> |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 21 | #include <phosphor-logging/elog.hpp> |
| 22 | #include "elog-errors.hpp" |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 23 | #include "targeting.hpp" |
| 24 | |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 25 | |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 26 | namespace openpower |
| 27 | { |
| 28 | namespace targeting |
| 29 | { |
| 30 | |
| 31 | using namespace phosphor::logging; |
| 32 | namespace fs = std::experimental::filesystem; |
| 33 | |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 34 | int 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 Tritz | be40716 | 2017-03-30 16:52:24 -0500 | [diff] [blame] | 45 | std::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 Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 63 | |
Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 64 | static uint32_t noEndianSwap(uint32_t data) |
| 65 | { |
| 66 | return data; |
| 67 | } |
| 68 | |
| 69 | static uint32_t endianSwap(uint32_t data) |
| 70 | { |
| 71 | return htobe32(data); |
| 72 | } |
| 73 | |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 74 | Targeting::Targeting(const std::string& fsiMasterDev, |
| 75 | const std::string& fsiSlaveDir) : |
| 76 | fsiMasterPath(fsiMasterDev), |
| 77 | fsiSlaveBasePath(fsiSlaveDir) |
| 78 | { |
Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 79 | 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 Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 95 | //Always create P0, the FSI master. |
Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 96 | targets.push_back(std::make_unique<Target>(0, fsiMasterPath, swapper)); |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 97 | try |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 98 | { |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 99 | //Find the the remaining P9s dynamically based on which files show up |
| 100 | for (auto& file : fs::directory_iterator(fsiSlaveBasePath)) |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 101 | { |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 102 | std::smatch match; |
| 103 | std::string path = file.path(); |
| 104 | if (std::regex_search(path, match, exp)) |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 105 | { |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 106 | 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 Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 117 | } |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 118 | } |
| 119 | } |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 120 | 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 Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 126 | |
| 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 | } |