| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 1 | /** | 
| Patrick Venture | e84b4dd | 2018-11-01 16:06:31 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 IBM Corporation | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 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 |  | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 17 | #include "targeting.hpp" | 
|  | 18 |  | 
| Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 19 | #include <endian.h> | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 20 |  | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 21 | #include <experimental/filesystem> | 
| Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 22 | #include <phosphor-logging/elog-errors.hpp> | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 23 | #include <phosphor-logging/elog.hpp> | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 24 | #include <phosphor-logging/log.hpp> | 
|  | 25 | #include <regex> | 
| Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 26 | #include <xyz/openbmc_project/Common/File/error.hpp> | 
| Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 27 |  | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 28 | namespace openpower | 
|  | 29 | { | 
|  | 30 | namespace targeting | 
|  | 31 | { | 
|  | 32 |  | 
|  | 33 | using namespace phosphor::logging; | 
|  | 34 | namespace fs = std::experimental::filesystem; | 
| Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 35 | namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 36 |  | 
| Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 37 | int Target::getCFAMFD() | 
|  | 38 | { | 
|  | 39 | if (cfamFD.get() == nullptr) | 
|  | 40 | { | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 41 | cfamFD = | 
|  | 42 | std::make_unique<openpower::util::FileDescriptor>(getCFAMPath()); | 
| Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
|  | 45 | return cfamFD->get(); | 
|  | 46 | } | 
|  | 47 |  | 
| Michael Tritz | be40716 | 2017-03-30 16:52:24 -0500 | [diff] [blame] | 48 | std::unique_ptr<Target>& Targeting::getTarget(size_t pos) | 
|  | 49 | { | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 50 | auto search = [pos](const auto& t) { return t->getPos() == pos; }; | 
| Michael Tritz | be40716 | 2017-03-30 16:52:24 -0500 | [diff] [blame] | 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 |  | 
| Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 63 | static uint32_t noEndianSwap(uint32_t data) | 
|  | 64 | { | 
|  | 65 | return data; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | static uint32_t endianSwap(uint32_t data) | 
|  | 69 | { | 
|  | 70 | return htobe32(data); | 
|  | 71 | } | 
|  | 72 |  | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 73 | Targeting::Targeting(const std::string& fsiMasterDev, | 
|  | 74 | const std::string& fsiSlaveDir) : | 
|  | 75 | fsiMasterPath(fsiMasterDev), | 
|  | 76 | fsiSlaveBasePath(fsiSlaveDir) | 
|  | 77 | { | 
| Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 78 | swap_endian_t swapper = endianSwap; | 
|  | 79 | std::regex exp{"fsi1/slave@([0-9]{2}):00", std::regex::extended}; | 
|  | 80 |  | 
|  | 81 | if (!fs::exists(fsiMasterPath)) | 
|  | 82 | { | 
|  | 83 | std::regex expOld{"hub@00/slave@([0-9]{2}):00", std::regex::extended}; | 
|  | 84 |  | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 85 | // Fall back to old (4.7) path | 
| Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 86 | exp = expOld; | 
|  | 87 | fsiMasterPath = fsiMasterDevPathOld; | 
|  | 88 | fsiSlaveBasePath = fsiSlaveBaseDirOld; | 
|  | 89 |  | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 90 | // And don't swap the endianness of CFAM data | 
| Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 91 | swapper = noEndianSwap; | 
|  | 92 | } | 
|  | 93 |  | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 94 | // Always create P0, the FSI master. | 
| Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 95 | targets.push_back(std::make_unique<Target>(0, fsiMasterPath, swapper)); | 
| Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 96 | try | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 97 | { | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 98 | // Find the the remaining P9s dynamically based on which files show up | 
| Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 99 | for (auto& file : fs::directory_iterator(fsiSlaveBasePath)) | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 100 | { | 
| Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 101 | std::smatch match; | 
|  | 102 | std::string path = file.path(); | 
|  | 103 | if (std::regex_search(path, match, exp)) | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 104 | { | 
| Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 105 | auto pos = atoi(match[1].str().c_str()); | 
|  | 106 | if (pos == 0) | 
|  | 107 | { | 
|  | 108 | log<level::ERR>("Unexpected FSI slave device name found", | 
|  | 109 | entry("DEVICE_NAME=%s", path.c_str())); | 
|  | 110 | continue; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | path += "/raw"; | 
|  | 114 |  | 
|  | 115 | targets.push_back(std::make_unique<Target>(pos, path, swapper)); | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 116 | } | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 117 | } | 
|  | 118 | } | 
| Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 119 | catch (fs::filesystem_error& e) | 
|  | 120 | { | 
| Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 121 | using metadata = xyz::openbmc_project::Common::File::Open; | 
|  | 122 |  | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 123 | elog<file_error::Open>(metadata::ERRNO(e.code().value()), | 
|  | 124 | metadata::PATH(e.path1().c_str())); | 
| Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 125 | } | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 126 |  | 
|  | 127 | auto sortTargets = [](const std::unique_ptr<Target>& left, | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 128 | const std::unique_ptr<Target>& right) { | 
| Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 129 | return left->getPos() < right->getPos(); | 
|  | 130 | }; | 
|  | 131 | std::sort(targets.begin(), targets.end(), sortTargets); | 
|  | 132 | } | 
|  | 133 |  | 
| Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 134 | } // namespace targeting | 
|  | 135 | } // namespace openpower |