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