blob: 1e392af7847d72bb425b702b3346f2c014d7c125 [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
Matt Spinler3d0c3692024-12-12 13:01:56 -060042 // First look in /etc/phosphor-fan-presence/control/
Matt Spinlere9d33b62021-11-09 13:27:26 -060043 auto confFile = fs::path{confOverridePath} / defaultPath;
44
Matt Spinler3d0c3692024-12-12 13:01:56 -060045 // 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 Spinlere9d33b62021-11-09 13:27:26 -060069 if (!fs::exists(confFile))
70 {
71 confFile = fs::path{confBasePath} / defaultPath;
72 }
73
74 if (fs::exists(confFile))
75 {
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050076 FlightRecorder::instance().log(
77 "main",
Patrick Williamsfbf47032023-07-17 12:27:34 -050078 std::format("Loading configuration from {}", confFile.string()));
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050079 load(JsonConfig::load(confFile));
80 FlightRecorder::instance().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -050081 "main", std::format("Configuration({}) loaded successfully",
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050082 confFile.string()));
Patrick Williamsfbf47032023-07-17 12:27:34 -050083 log<level::INFO>(std::format("Configuration({}) loaded successfully",
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050084 confFile.string())
85 .c_str());
Matt Spinlere9d33b62021-11-09 13:27:26 -060086 }
Matt Spinlere9d33b62021-11-09 13:27:26 -060087}
88
89void PCIeCardMetadata::load(const nlohmann::json& json)
90{
91 if (!json.contains("cards") || !json.at("cards").is_array())
92 {
93 throw std::runtime_error{
Patrick Williamsfbf47032023-07-17 12:27:34 -050094 std::format("Missing 'cards' array in PCIe card JSON")};
Matt Spinlere9d33b62021-11-09 13:27:26 -060095 }
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 Williamsdfddd642024-08-16 15:21:51 -0400108 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 Spinlere9d33b62021-11-09 13:27:26 -0600112 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
137void 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 Williamsdfddd642024-08-16 15:21:51 -0400153std::optional<std::variant<int32_t, bool>> PCIeCardMetadata::lookup(
154 uint16_t deviceID, uint16_t vendorID, uint16_t subsystemID,
155 uint16_t subsystemVendorID) const
Matt Spinlere9d33b62021-11-09 13:27:26 -0600156{
Patrick Williamsfbf47032023-07-17 12:27:34 -0500157 log<level::DEBUG>(std::format("Lookup {:#x} ${:#x} {:#x} {:#x}", deviceID,
Matt Spinlere9d33b62021-11-09 13:27:26 -0600158 vendorID, subsystemID, subsystemVendorID)
159 .c_str());
Patrick Williamsdfddd642024-08-16 15:21:51 -0400160 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 Spinlere9d33b62021-11-09 13:27:26 -0600168
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