Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2022 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 | */ |
| 16 | |
| 17 | #include "pcie_card_metadata.hpp" |
| 18 | |
| 19 | #include "json_config.hpp" |
Matthew Barth | b5c2bbe | 2022-03-14 15:32:13 -0500 | [diff] [blame] | 20 | #include "utils/flight_recorder.hpp" |
| 21 | |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 22 | #include <format> |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 23 | #include <iostream> |
| 24 | |
| 25 | static constexpr auto cardFileName = "pcie_cards.json"; |
| 26 | |
| 27 | namespace phosphor::fan::control::json |
| 28 | { |
| 29 | |
| 30 | namespace fs = std::filesystem; |
| 31 | using namespace phosphor::fan; |
| 32 | |
| 33 | PCIeCardMetadata::PCIeCardMetadata(const std::vector<std::string>& systemNames) |
| 34 | { |
| 35 | loadCards(systemNames); |
| 36 | } |
| 37 | |
| 38 | void PCIeCardMetadata::loadCards(const std::vector<std::string>& systemNames) |
| 39 | { |
| 40 | const auto defaultPath = fs::path{"control"} / cardFileName; |
| 41 | |
Matt Spinler | 3d0c369 | 2024-12-12 13:01:56 -0600 | [diff] [blame] | 42 | // First look in /etc/phosphor-fan-presence/control/ |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 43 | auto confFile = fs::path{confOverridePath} / defaultPath; |
| 44 | |
Matt Spinler | 3d0c369 | 2024-12-12 13:01:56 -0600 | [diff] [blame] | 45 | // Next look in the system subdirectories, first under /etc then /usr. |
| 46 | if (!fs::exists(confFile)) |
| 47 | { |
| 48 | for (const auto& systemName : systemNames) |
| 49 | { |
| 50 | const auto basePath = |
| 51 | fs::path{"control"} / systemName / cardFileName; |
| 52 | |
| 53 | // Look in the /etc override location first |
| 54 | confFile = fs::path{confOverridePath} / basePath; |
| 55 | |
| 56 | if (!fs::exists(confFile)) |
| 57 | { |
| 58 | confFile = fs::path{confBasePath} / basePath; |
| 59 | } |
| 60 | |
| 61 | if (fs::exists(confFile)) |
| 62 | { |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Next look in /usr/share/phosphor-fan-presence/control/ |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 69 | if (!fs::exists(confFile)) |
| 70 | { |
| 71 | confFile = fs::path{confBasePath} / defaultPath; |
| 72 | } |
| 73 | |
| 74 | if (fs::exists(confFile)) |
| 75 | { |
Matthew Barth | b5c2bbe | 2022-03-14 15:32:13 -0500 | [diff] [blame] | 76 | FlightRecorder::instance().log( |
| 77 | "main", |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 78 | std::format("Loading configuration from {}", confFile.string())); |
Matthew Barth | b5c2bbe | 2022-03-14 15:32:13 -0500 | [diff] [blame] | 79 | load(JsonConfig::load(confFile)); |
| 80 | FlightRecorder::instance().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 81 | "main", std::format("Configuration({}) loaded successfully", |
Matthew Barth | b5c2bbe | 2022-03-14 15:32:13 -0500 | [diff] [blame] | 82 | confFile.string())); |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 83 | log<level::INFO>(std::format("Configuration({}) loaded successfully", |
Matthew Barth | b5c2bbe | 2022-03-14 15:32:13 -0500 | [diff] [blame] | 84 | confFile.string()) |
| 85 | .c_str()); |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 86 | } |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void PCIeCardMetadata::load(const nlohmann::json& json) |
| 90 | { |
| 91 | if (!json.contains("cards") || !json.at("cards").is_array()) |
| 92 | { |
| 93 | throw std::runtime_error{ |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 94 | std::format("Missing 'cards' array in PCIe card JSON")}; |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | for (const auto& card : json.at("cards")) |
| 98 | { |
| 99 | if (!card.contains("vendor_id") || !card.contains("device_id") || |
| 100 | !card.contains("subsystem_vendor_id") || |
| 101 | !card.contains("subsystem_id") || |
| 102 | !(card.contains("has_temp_sensor") || card.contains("floor_index"))) |
| 103 | { |
| 104 | throw std::runtime_error{"Invalid PCIe card json"}; |
| 105 | } |
| 106 | |
| 107 | Metadata data; |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 108 | data.vendorID = |
| 109 | std::stoul(card.at("vendor_id").get<std::string>(), nullptr, 16); |
| 110 | data.deviceID = |
| 111 | std::stoul(card.at("device_id").get<std::string>(), nullptr, 16); |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 112 | data.subsystemVendorID = std::stoul( |
| 113 | card.at("subsystem_vendor_id").get<std::string>(), nullptr, 16); |
| 114 | data.subsystemID = |
| 115 | std::stoul(card.at("subsystem_id").get<std::string>(), nullptr, 16); |
| 116 | |
| 117 | data.hasTempSensor = card.value("has_temp_sensor", false); |
| 118 | data.floorIndex = card.value("floor_index", -1); |
| 119 | |
| 120 | auto iter = std::find(_cards.begin(), _cards.end(), data); |
| 121 | if (iter != _cards.end()) |
| 122 | { |
| 123 | iter->vendorID = data.vendorID; |
| 124 | iter->deviceID = data.deviceID; |
| 125 | iter->subsystemVendorID = data.subsystemVendorID; |
| 126 | iter->subsystemID = data.subsystemID; |
| 127 | iter->floorIndex = data.floorIndex; |
| 128 | iter->hasTempSensor = data.hasTempSensor; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | _cards.push_back(std::move(data)); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void PCIeCardMetadata::dump() const |
| 138 | { |
| 139 | for (const auto& entry : _cards) |
| 140 | { |
| 141 | std::cerr << "--------------------------------------------------" |
| 142 | << "\n"; |
| 143 | std::cerr << "vendorID: " << std::hex << entry.vendorID << "\n"; |
| 144 | std::cerr << "deviceID: " << entry.deviceID << "\n"; |
| 145 | std::cerr << "subsysVendorID: " << entry.subsystemVendorID << "\n"; |
| 146 | std::cerr << "subsystemID: " << entry.subsystemID << "\n"; |
| 147 | std::cerr << "hasTempSensor: " << std::dec << entry.hasTempSensor |
| 148 | << "\n"; |
| 149 | std::cerr << "floorIndex: " << entry.floorIndex << "\n"; |
| 150 | } |
| 151 | } |
| 152 | |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 153 | std::optional<std::variant<int32_t, bool>> PCIeCardMetadata::lookup( |
| 154 | uint16_t deviceID, uint16_t vendorID, uint16_t subsystemID, |
| 155 | uint16_t subsystemVendorID) const |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 156 | { |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 157 | log<level::DEBUG>(std::format("Lookup {:#x} ${:#x} {:#x} {:#x}", deviceID, |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 158 | vendorID, subsystemID, subsystemVendorID) |
| 159 | .c_str()); |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 160 | auto card = std::find_if( |
| 161 | _cards.begin(), _cards.end(), |
| 162 | [&deviceID, &vendorID, &subsystemID, |
| 163 | &subsystemVendorID](const auto& card) { |
| 164 | return (deviceID == card.deviceID) && (vendorID == card.vendorID) && |
| 165 | (subsystemID == card.subsystemID) && |
| 166 | (subsystemVendorID == card.subsystemVendorID); |
| 167 | }); |
Matt Spinler | e9d33b6 | 2021-11-09 13:27:26 -0600 | [diff] [blame] | 168 | |
| 169 | if (card != _cards.end()) |
| 170 | { |
| 171 | if (card->hasTempSensor) |
| 172 | { |
| 173 | return true; |
| 174 | } |
| 175 | return card->floorIndex; |
| 176 | } |
| 177 | return std::nullopt; |
| 178 | } |
| 179 | |
| 180 | } // namespace phosphor::fan::control::json |