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 <fmt/format.h>
23 
24 #include <iostream>
25 
26 static constexpr auto cardFileName = "pcie_cards.json";
27 
28 namespace phosphor::fan::control::json
29 {
30 
31 namespace fs = std::filesystem;
32 using namespace phosphor::fan;
33 
34 PCIeCardMetadata::PCIeCardMetadata(const std::vector<std::string>& systemNames)
35 {
36     loadCards(systemNames);
37 }
38 
39 void PCIeCardMetadata::loadCards(const std::vector<std::string>& systemNames)
40 {
41     const auto defaultPath = fs::path{"control"} / cardFileName;
42 
43     // Look in the override location first
44     auto confFile = fs::path{confOverridePath} / defaultPath;
45 
46     if (!fs::exists(confFile))
47     {
48         confFile = fs::path{confBasePath} / defaultPath;
49     }
50 
51     if (fs::exists(confFile))
52     {
53         FlightRecorder::instance().log(
54             "main",
55             fmt::format("Loading configuration from {}", confFile.string()));
56         load(JsonConfig::load(confFile));
57         FlightRecorder::instance().log(
58             "main", fmt::format("Configuration({}) loaded successfully",
59                                 confFile.string()));
60         log<level::INFO>(fmt::format("Configuration({}) loaded successfully",
61                                      confFile.string())
62                              .c_str());
63     }
64 
65     // Go from least specific to most specific in the system names so files in
66     // the latter category can override ones in the former.
67     for (auto nameIt = systemNames.rbegin(); nameIt != systemNames.rend();
68          ++nameIt)
69     {
70         const auto basePath = fs::path{"control"} / *nameIt / cardFileName;
71 
72         // Look in the override location first
73         auto confFile = fs::path{confOverridePath} / basePath;
74 
75         if (!fs::exists(confFile))
76         {
77             confFile = fs::path{confBasePath} / basePath;
78         }
79 
80         if (fs::exists(confFile))
81         {
82             FlightRecorder::instance().log(
83                 "main", fmt::format("Loading configuration from {}",
84                                     confFile.string()));
85             load(JsonConfig::load(confFile));
86             FlightRecorder::instance().log(
87                 "main", fmt::format("Configuration({}) loaded successfully",
88                                     confFile.string()));
89             log<level::INFO>(
90                 fmt::format("Configuration({}) loaded successfully",
91                             confFile.string())
92                     .c_str());
93         }
94     }
95 
96     if (_cards.empty())
97     {
98         throw std::runtime_error{
99             "No valid PCIe card entries found in any JSON"};
100     }
101 }
102 
103 void PCIeCardMetadata::load(const nlohmann::json& json)
104 {
105     if (!json.contains("cards") || !json.at("cards").is_array())
106     {
107         throw std::runtime_error{
108             fmt::format("Missing 'cards' array in PCIe card JSON")};
109     }
110 
111     for (const auto& card : json.at("cards"))
112     {
113         if (!card.contains("vendor_id") || !card.contains("device_id") ||
114             !card.contains("subsystem_vendor_id") ||
115             !card.contains("subsystem_id") ||
116             !(card.contains("has_temp_sensor") || card.contains("floor_index")))
117         {
118             throw std::runtime_error{"Invalid PCIe card json"};
119         }
120 
121         Metadata data;
122         data.vendorID = std::stoul(card.at("vendor_id").get<std::string>(),
123                                    nullptr, 16);
124         data.deviceID = std::stoul(card.at("device_id").get<std::string>(),
125                                    nullptr, 16);
126         data.subsystemVendorID = std::stoul(
127             card.at("subsystem_vendor_id").get<std::string>(), nullptr, 16);
128         data.subsystemID =
129             std::stoul(card.at("subsystem_id").get<std::string>(), nullptr, 16);
130 
131         data.hasTempSensor = card.value("has_temp_sensor", false);
132         data.floorIndex = card.value("floor_index", -1);
133 
134         auto iter = std::find(_cards.begin(), _cards.end(), data);
135         if (iter != _cards.end())
136         {
137             iter->vendorID = data.vendorID;
138             iter->deviceID = data.deviceID;
139             iter->subsystemVendorID = data.subsystemVendorID;
140             iter->subsystemID = data.subsystemID;
141             iter->floorIndex = data.floorIndex;
142             iter->hasTempSensor = data.hasTempSensor;
143         }
144         else
145         {
146             _cards.push_back(std::move(data));
147         }
148     }
149 }
150 
151 void PCIeCardMetadata::dump() const
152 {
153     for (const auto& entry : _cards)
154     {
155         std::cerr << "--------------------------------------------------"
156                   << "\n";
157         std::cerr << "vendorID: " << std::hex << entry.vendorID << "\n";
158         std::cerr << "deviceID: " << entry.deviceID << "\n";
159         std::cerr << "subsysVendorID: " << entry.subsystemVendorID << "\n";
160         std::cerr << "subsystemID: " << entry.subsystemID << "\n";
161         std::cerr << "hasTempSensor: " << std::dec << entry.hasTempSensor
162                   << "\n";
163         std::cerr << "floorIndex: " << entry.floorIndex << "\n";
164     }
165 }
166 
167 std::optional<std::variant<int32_t, bool>>
168     PCIeCardMetadata::lookup(uint16_t deviceID, uint16_t vendorID,
169                              uint16_t subsystemID,
170                              uint16_t subsystemVendorID) const
171 {
172     log<level::DEBUG>(fmt::format("Lookup {:#x} ${:#x} {:#x} {:#x}", deviceID,
173                                   vendorID, subsystemID, subsystemVendorID)
174                           .c_str());
175     auto card = std::find_if(_cards.begin(), _cards.end(),
176                              [&deviceID, &vendorID, &subsystemID,
177                               &subsystemVendorID](const auto& card) {
178         return (deviceID == card.deviceID) && (vendorID == card.vendorID) &&
179                (subsystemID == card.subsystemID) &&
180                (subsystemVendorID == card.subsystemVendorID);
181     });
182 
183     if (card != _cards.end())
184     {
185         if (card->hasTempSensor)
186         {
187             return true;
188         }
189         return card->floorIndex;
190     }
191     return std::nullopt;
192 }
193 
194 } // namespace phosphor::fan::control::json
195