blob: c8c1b37945c90b828ad87d906aef8e6aa2040713 [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 }
Matt Spinlere9d33b62021-11-09 13:27:26 -060094}
95
96void PCIeCardMetadata::load(const nlohmann::json& json)
97{
98 if (!json.contains("cards") || !json.at("cards").is_array())
99 {
100 throw std::runtime_error{
Patrick Williamsfbf47032023-07-17 12:27:34 -0500101 std::format("Missing 'cards' array in PCIe card JSON")};
Matt Spinlere9d33b62021-11-09 13:27:26 -0600102 }
103
104 for (const auto& card : json.at("cards"))
105 {
106 if (!card.contains("vendor_id") || !card.contains("device_id") ||
107 !card.contains("subsystem_vendor_id") ||
108 !card.contains("subsystem_id") ||
109 !(card.contains("has_temp_sensor") || card.contains("floor_index")))
110 {
111 throw std::runtime_error{"Invalid PCIe card json"};
112 }
113
114 Metadata data;
Patrick Williamsdfddd642024-08-16 15:21:51 -0400115 data.vendorID =
116 std::stoul(card.at("vendor_id").get<std::string>(), nullptr, 16);
117 data.deviceID =
118 std::stoul(card.at("device_id").get<std::string>(), nullptr, 16);
Matt Spinlere9d33b62021-11-09 13:27:26 -0600119 data.subsystemVendorID = std::stoul(
120 card.at("subsystem_vendor_id").get<std::string>(), nullptr, 16);
121 data.subsystemID =
122 std::stoul(card.at("subsystem_id").get<std::string>(), nullptr, 16);
123
124 data.hasTempSensor = card.value("has_temp_sensor", false);
125 data.floorIndex = card.value("floor_index", -1);
126
127 auto iter = std::find(_cards.begin(), _cards.end(), data);
128 if (iter != _cards.end())
129 {
130 iter->vendorID = data.vendorID;
131 iter->deviceID = data.deviceID;
132 iter->subsystemVendorID = data.subsystemVendorID;
133 iter->subsystemID = data.subsystemID;
134 iter->floorIndex = data.floorIndex;
135 iter->hasTempSensor = data.hasTempSensor;
136 }
137 else
138 {
139 _cards.push_back(std::move(data));
140 }
141 }
142}
143
144void PCIeCardMetadata::dump() const
145{
146 for (const auto& entry : _cards)
147 {
148 std::cerr << "--------------------------------------------------"
149 << "\n";
150 std::cerr << "vendorID: " << std::hex << entry.vendorID << "\n";
151 std::cerr << "deviceID: " << entry.deviceID << "\n";
152 std::cerr << "subsysVendorID: " << entry.subsystemVendorID << "\n";
153 std::cerr << "subsystemID: " << entry.subsystemID << "\n";
154 std::cerr << "hasTempSensor: " << std::dec << entry.hasTempSensor
155 << "\n";
156 std::cerr << "floorIndex: " << entry.floorIndex << "\n";
157 }
158}
159
Patrick Williamsdfddd642024-08-16 15:21:51 -0400160std::optional<std::variant<int32_t, bool>> PCIeCardMetadata::lookup(
161 uint16_t deviceID, uint16_t vendorID, uint16_t subsystemID,
162 uint16_t subsystemVendorID) const
Matt Spinlere9d33b62021-11-09 13:27:26 -0600163{
Patrick Williamsfbf47032023-07-17 12:27:34 -0500164 log<level::DEBUG>(std::format("Lookup {:#x} ${:#x} {:#x} {:#x}", deviceID,
Matt Spinlere9d33b62021-11-09 13:27:26 -0600165 vendorID, subsystemID, subsystemVendorID)
166 .c_str());
Patrick Williamsdfddd642024-08-16 15:21:51 -0400167 auto card = std::find_if(
168 _cards.begin(), _cards.end(),
169 [&deviceID, &vendorID, &subsystemID,
170 &subsystemVendorID](const auto& card) {
171 return (deviceID == card.deviceID) && (vendorID == card.vendorID) &&
172 (subsystemID == card.subsystemID) &&
173 (subsystemVendorID == card.subsystemVendorID);
174 });
Matt Spinlere9d33b62021-11-09 13:27:26 -0600175
176 if (card != _cards.end())
177 {
178 if (card->hasTempSensor)
179 {
180 return true;
181 }
182 return card->floorIndex;
183 }
184 return std::nullopt;
185}
186
187} // namespace phosphor::fan::control::json