blob: 803208155b2c593d30d3012cefced34695466435 [file] [log] [blame]
Matt Spinlere9d33b62021-11-09 13:27:26 -06001/**
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 Barthb5c2bbe2022-03-14 15:32:13 -050020#include "utils/flight_recorder.hpp"
21
Patrick Williamsfbf47032023-07-17 12:27:34 -050022#include <format>
Matt Spinlere9d33b62021-11-09 13:27:26 -060023#include <iostream>
24
25static constexpr auto cardFileName = "pcie_cards.json";
26
27namespace phosphor::fan::control::json
28{
29
30namespace fs = std::filesystem;
31using namespace phosphor::fan;
32
33PCIeCardMetadata::PCIeCardMetadata(const std::vector<std::string>& systemNames)
34{
35 loadCards(systemNames);
36}
37
38void PCIeCardMetadata::loadCards(const std::vector<std::string>& systemNames)
39{
40 const auto defaultPath = fs::path{"control"} / cardFileName;
41
42 // Look in the override location first
43 auto confFile = fs::path{confOverridePath} / defaultPath;
44
45 if (!fs::exists(confFile))
46 {
47 confFile = fs::path{confBasePath} / defaultPath;
48 }
49
50 if (fs::exists(confFile))
51 {
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050052 FlightRecorder::instance().log(
53 "main",
Patrick Williamsfbf47032023-07-17 12:27:34 -050054 std::format("Loading configuration from {}", confFile.string()));
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050055 load(JsonConfig::load(confFile));
56 FlightRecorder::instance().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -050057 "main", std::format("Configuration({}) loaded successfully",
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050058 confFile.string()));
Patrick Williamsfbf47032023-07-17 12:27:34 -050059 log<level::INFO>(std::format("Configuration({}) loaded successfully",
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050060 confFile.string())
61 .c_str());
Matt Spinlere9d33b62021-11-09 13:27:26 -060062 }
63
64 // Go from least specific to most specific in the system names so files in
65 // the latter category can override ones in the former.
66 for (auto nameIt = systemNames.rbegin(); nameIt != systemNames.rend();
67 ++nameIt)
68 {
69 const auto basePath = fs::path{"control"} / *nameIt / cardFileName;
70
71 // Look in the override location first
72 auto confFile = fs::path{confOverridePath} / basePath;
73
74 if (!fs::exists(confFile))
75 {
76 confFile = fs::path{confBasePath} / basePath;
77 }
78
79 if (fs::exists(confFile))
80 {
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050081 FlightRecorder::instance().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -050082 "main", std::format("Loading configuration from {}",
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050083 confFile.string()));
84 load(JsonConfig::load(confFile));
85 FlightRecorder::instance().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -050086 "main", std::format("Configuration({}) loaded successfully",
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050087 confFile.string()));
88 log<level::INFO>(
Patrick Williamsfbf47032023-07-17 12:27:34 -050089 std::format("Configuration({}) loaded successfully",
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050090 confFile.string())
Matt Spinlere9d33b62021-11-09 13:27:26 -060091 .c_str());
Matt Spinlere9d33b62021-11-09 13:27:26 -060092 }
93 }
94
95 if (_cards.empty())
96 {
97 throw std::runtime_error{
98 "No valid PCIe card entries found in any JSON"};
99 }
100}
101
102void PCIeCardMetadata::load(const nlohmann::json& json)
103{
104 if (!json.contains("cards") || !json.at("cards").is_array())
105 {
106 throw std::runtime_error{
Patrick Williamsfbf47032023-07-17 12:27:34 -0500107 std::format("Missing 'cards' array in PCIe card JSON")};
Matt Spinlere9d33b62021-11-09 13:27:26 -0600108 }
109
110 for (const auto& card : json.at("cards"))
111 {
112 if (!card.contains("vendor_id") || !card.contains("device_id") ||
113 !card.contains("subsystem_vendor_id") ||
114 !card.contains("subsystem_id") ||
115 !(card.contains("has_temp_sensor") || card.contains("floor_index")))
116 {
117 throw std::runtime_error{"Invalid PCIe card json"};
118 }
119
120 Metadata data;
Patrick Williams61b73292023-05-10 07:50:12 -0500121 data.vendorID = std::stoul(card.at("vendor_id").get<std::string>(),
122 nullptr, 16);
123 data.deviceID = std::stoul(card.at("device_id").get<std::string>(),
124 nullptr, 16);
Matt Spinlere9d33b62021-11-09 13:27:26 -0600125 data.subsystemVendorID = std::stoul(
126 card.at("subsystem_vendor_id").get<std::string>(), nullptr, 16);
127 data.subsystemID =
128 std::stoul(card.at("subsystem_id").get<std::string>(), nullptr, 16);
129
130 data.hasTempSensor = card.value("has_temp_sensor", false);
131 data.floorIndex = card.value("floor_index", -1);
132
133 auto iter = std::find(_cards.begin(), _cards.end(), data);
134 if (iter != _cards.end())
135 {
136 iter->vendorID = data.vendorID;
137 iter->deviceID = data.deviceID;
138 iter->subsystemVendorID = data.subsystemVendorID;
139 iter->subsystemID = data.subsystemID;
140 iter->floorIndex = data.floorIndex;
141 iter->hasTempSensor = data.hasTempSensor;
142 }
143 else
144 {
145 _cards.push_back(std::move(data));
146 }
147 }
148}
149
150void PCIeCardMetadata::dump() const
151{
152 for (const auto& entry : _cards)
153 {
154 std::cerr << "--------------------------------------------------"
155 << "\n";
156 std::cerr << "vendorID: " << std::hex << entry.vendorID << "\n";
157 std::cerr << "deviceID: " << entry.deviceID << "\n";
158 std::cerr << "subsysVendorID: " << entry.subsystemVendorID << "\n";
159 std::cerr << "subsystemID: " << entry.subsystemID << "\n";
160 std::cerr << "hasTempSensor: " << std::dec << entry.hasTempSensor
161 << "\n";
162 std::cerr << "floorIndex: " << entry.floorIndex << "\n";
163 }
164}
165
166std::optional<std::variant<int32_t, bool>>
167 PCIeCardMetadata::lookup(uint16_t deviceID, uint16_t vendorID,
168 uint16_t subsystemID,
169 uint16_t subsystemVendorID) const
170{
Patrick Williamsfbf47032023-07-17 12:27:34 -0500171 log<level::DEBUG>(std::format("Lookup {:#x} ${:#x} {:#x} {:#x}", deviceID,
Matt Spinlere9d33b62021-11-09 13:27:26 -0600172 vendorID, subsystemID, subsystemVendorID)
173 .c_str());
Patrick Williams61b73292023-05-10 07:50:12 -0500174 auto card = std::find_if(_cards.begin(), _cards.end(),
175 [&deviceID, &vendorID, &subsystemID,
176 &subsystemVendorID](const auto& card) {
177 return (deviceID == card.deviceID) && (vendorID == card.vendorID) &&
178 (subsystemID == card.subsystemID) &&
179 (subsystemVendorID == card.subsystemVendorID);
180 });
Matt Spinlere9d33b62021-11-09 13:27:26 -0600181
182 if (card != _cards.end())
183 {
184 if (card->hasTempSensor)
185 {
186 return true;
187 }
188 return card->floorIndex;
189 }
190 return std::nullopt;
191}
192
193} // namespace phosphor::fan::control::json