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 
PCIeCardMetadata(const std::vector<std::string> & systemNames)33 PCIeCardMetadata::PCIeCardMetadata(const std::vector<std::string>& systemNames)
34 {
35     loadCards(systemNames);
36 }
37 
loadCards(const std::vector<std::string> & systemNames)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 
load(const nlohmann::json & json)96 void PCIeCardMetadata::load(const nlohmann::json& json)
97 {
98     if (!json.contains("cards") || !json.at("cards").is_array())
99     {
100         throw std::runtime_error{
101             std::format("Missing 'cards' array in PCIe card JSON")};
102     }
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;
115         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);
119         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 
dump() const144 void 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 
lookup(uint16_t deviceID,uint16_t vendorID,uint16_t subsystemID,uint16_t subsystemVendorID) const160 std::optional<std::variant<int32_t, bool>> PCIeCardMetadata::lookup(
161     uint16_t deviceID, uint16_t vendorID, uint16_t subsystemID,
162     uint16_t subsystemVendorID) const
163 {
164     log<level::DEBUG>(std::format("Lookup {:#x} ${:#x} {:#x} {:#x}", deviceID,
165                                   vendorID, subsystemID, subsystemVendorID)
166                           .c_str());
167     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         });
175 
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
188