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 <format>
23 #include <iostream>
24 
25 static constexpr auto cardFileName = "pcie_cards.json";
26 
27 namespace phosphor::fan::control::json
28 {
29 
30 namespace fs = std::filesystem;
31 using namespace phosphor::fan;
32 
33 PCIeCardMetadata::PCIeCardMetadata(const std::vector<std::string>& systemNames)
34 {
35     loadCards(systemNames);
36 }
37 
38 void 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     {
52         FlightRecorder::instance().log(
53             "main",
54             std::format("Loading configuration from {}", confFile.string()));
55         load(JsonConfig::load(confFile));
56         FlightRecorder::instance().log(
57             "main", std::format("Configuration({}) loaded successfully",
58                                 confFile.string()));
59         log<level::INFO>(std::format("Configuration({}) loaded successfully",
60                                      confFile.string())
61                              .c_str());
62     }
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         {
81             FlightRecorder::instance().log(
82                 "main", std::format("Loading configuration from {}",
83                                     confFile.string()));
84             load(JsonConfig::load(confFile));
85             FlightRecorder::instance().log(
86                 "main", std::format("Configuration({}) loaded successfully",
87                                     confFile.string()));
88             log<level::INFO>(
89                 std::format("Configuration({}) loaded successfully",
90                             confFile.string())
91                     .c_str());
92         }
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 
102 void PCIeCardMetadata::load(const nlohmann::json& json)
103 {
104     if (!json.contains("cards") || !json.at("cards").is_array())
105     {
106         throw std::runtime_error{
107             std::format("Missing 'cards' array in PCIe card JSON")};
108     }
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;
121         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);
125         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 
150 void 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 
166 std::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 {
171     log<level::DEBUG>(std::format("Lookup {:#x} ${:#x} {:#x} {:#x}", deviceID,
172                                   vendorID, subsystemID, subsystemVendorID)
173                           .c_str());
174     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     });
181 
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
194