blob: 392dee136e6716ea935d66ed36cf396bec4725e3 [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
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000022#include <phosphor-logging/lg2.hpp>
23
Patrick Williamsfbf47032023-07-17 12:27:34 -050024#include <format>
Matt Spinlere9d33b62021-11-09 13:27:26 -060025#include <iostream>
26
27static constexpr auto cardFileName = "pcie_cards.json";
28
29namespace phosphor::fan::control::json
30{
31
32namespace fs = std::filesystem;
33using namespace phosphor::fan;
34
35PCIeCardMetadata::PCIeCardMetadata(const std::vector<std::string>& systemNames)
36{
37 loadCards(systemNames);
38}
39
40void PCIeCardMetadata::loadCards(const std::vector<std::string>& systemNames)
41{
42 const auto defaultPath = fs::path{"control"} / cardFileName;
43
Matt Spinler3d0c3692024-12-12 13:01:56 -060044 // First look in /etc/phosphor-fan-presence/control/
Matt Spinlere9d33b62021-11-09 13:27:26 -060045 auto confFile = fs::path{confOverridePath} / defaultPath;
46
Matt Spinler3d0c3692024-12-12 13:01:56 -060047 // 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/
Matt Spinlere9d33b62021-11-09 13:27:26 -060071 if (!fs::exists(confFile))
72 {
73 confFile = fs::path{confBasePath} / defaultPath;
74 }
75
76 if (fs::exists(confFile))
77 {
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050078 FlightRecorder::instance().log(
79 "main",
Patrick Williamsfbf47032023-07-17 12:27:34 -050080 std::format("Loading configuration from {}", confFile.string()));
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050081 load(JsonConfig::load(confFile));
82 FlightRecorder::instance().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -050083 "main", std::format("Configuration({}) loaded successfully",
Matthew Barthb5c2bbe2022-03-14 15:32:13 -050084 confFile.string()));
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000085 lg2::info("Configuration({CONF_FILE}) loaded successfully", "CONF_FILE",
86 confFile);
Matt Spinlere9d33b62021-11-09 13:27:26 -060087 }
Matt Spinlere9d33b62021-11-09 13:27:26 -060088}
89
90void PCIeCardMetadata::load(const nlohmann::json& json)
91{
92 if (!json.contains("cards") || !json.at("cards").is_array())
93 {
94 throw std::runtime_error{
Patrick Williamsfbf47032023-07-17 12:27:34 -050095 std::format("Missing 'cards' array in PCIe card JSON")};
Matt Spinlere9d33b62021-11-09 13:27:26 -060096 }
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;
Patrick Williamsdfddd642024-08-16 15:21:51 -0400109 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);
Matt Spinlere9d33b62021-11-09 13:27:26 -0600113 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
138void 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
Patrick Williamsdfddd642024-08-16 15:21:51 -0400154std::optional<std::variant<int32_t, bool>> PCIeCardMetadata::lookup(
155 uint16_t deviceID, uint16_t vendorID, uint16_t subsystemID,
156 uint16_t subsystemVendorID) const
Matt Spinlere9d33b62021-11-09 13:27:26 -0600157{
Anwaar Hadi64b5ac22025-04-04 23:54:53 +0000158 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);
Patrick Williamsdfddd642024-08-16 15:21:51 -0400163 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 });
Matt Spinlere9d33b62021-11-09 13:27:26 -0600171
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