blob: ed932cf5f81735db98fb0836a30676c27f262d2e [file] [log] [blame]
Nikhil Potadeb669b6b2019-03-13 10:52:21 -07001/*
2// Copyright (c) 2019 Intel 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
Andrew Jefferye3e3c972021-05-26 14:37:07 +093017#include <NVMeBasicContext.hpp>
Andrew Jefferydae6e182021-05-21 16:23:07 +093018#include <NVMeContext.hpp>
Andrew Jefferya9d15082021-05-24 13:55:12 +093019#include <NVMeMCTPContext.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -070020#include <NVMeSensor.hpp>
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070021#include <boost/asio/deadline_timer.hpp>
James Feist38fb5982020-05-28 10:09:54 -070022
Andrew Jeffery34123562021-12-02 14:38:41 +103023#include <optional>
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070024#include <regex>
25
26static constexpr const char* sensorType =
27 "xyz.openbmc_project.Configuration.NVME1000";
28
29static NVMEMap nvmeDeviceMap;
30
Ed Tanous8a57ec02020-10-09 12:46:52 -070031static constexpr bool debug = false;
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070032
33NVMEMap& getNVMEMap()
34{
35 return nvmeDeviceMap;
36}
37
Andrew Jeffery34123562021-12-02 14:38:41 +103038static std::optional<int>
39 extractBusNumber(const std::string& path,
40 const SensorBaseConfigMap& properties)
41{
42 auto findBus = properties.find("Bus");
43 if (findBus == properties.end())
44 {
45 std::cerr << "could not determine bus number for " << path << "\n";
46 return std::nullopt;
47 }
48
49 return std::visit(VariantToIntVisitor(), findBus->second);
50}
51
Andrew Jefferye671f052021-12-02 14:47:20 +103052static std::optional<std::string>
53 extractSensorName(const std::string& path,
54 const SensorBaseConfigMap& properties)
55{
56 auto findSensorName = properties.find("Name");
57 if (findSensorName == properties.end())
58 {
59 std::cerr << "could not determine configuration name for " << path
60 << "\n";
61 return std::nullopt;
62 }
63
64 return std::get<std::string>(findSensorName->second);
65}
66
Andrew Jefferyf690f0c2021-12-02 11:42:58 +103067static void handleSensorConfigurations(
68 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
69 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
70 const ManagedObjectType& sensorConfigurations)
71{
72 // todo: it'd be better to only update the ones we care about
73 for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap)
74 {
75 if (nvmeContextPtr)
76 {
77 nvmeContextPtr->close();
78 }
79 }
80 nvmeDeviceMap.clear();
81
82 // iterate through all found configurations
83 for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor :
84 sensorConfigurations)
85 {
86 const SensorData& sensorData = sensor.second;
87 const std::string& interfacePath = sensor.first.str;
88 const std::pair<std::string, boost::container::flat_map<
89 std::string, BasicVariantType>>*
90 baseConfiguration = nullptr;
91
92 // find base configuration
93 auto sensorBase = sensor.second.find(sensorType);
94 if (sensorBase != sensor.second.end())
95 {
96 baseConfiguration = &(*sensorBase);
97 }
98
99 if (baseConfiguration == nullptr)
100 {
101 continue;
102 }
Andrew Jeffery34123562021-12-02 14:38:41 +1030103
104 std::optional<int> busNumber =
105 extractBusNumber(sensor.first, baseConfiguration->second);
106 if (!busNumber)
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030107 {
108 continue;
109 }
110
Andrew Jefferye671f052021-12-02 14:47:20 +1030111 std::optional<std::string> sensorName =
112 extractSensorName(sensor.first, baseConfiguration->second);
113 if (!sensorName)
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030114 {
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030115 continue;
116 }
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030117
118 std::vector<thresholds::Threshold> sensorThresholds;
119
120 if (!parseThresholdsFromConfig(sensorData, sensorThresholds))
121 {
Andrew Jefferye671f052021-12-02 14:47:20 +1030122 std::cerr << "error populating thresholds for " << *sensorName
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030123 << "\n";
124 }
125
Andrew Jeffery34123562021-12-02 14:38:41 +1030126 int rootBus = *busNumber;
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030127
128 std::string muxPath = "/sys/bus/i2c/devices/i2c-" +
Andrew Jeffery34123562021-12-02 14:38:41 +1030129 std::to_string(*busNumber) + "/mux_device";
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030130
131 if (std::filesystem::is_symlink(muxPath))
132 {
133 std::string rootName =
134 std::filesystem::read_symlink(muxPath).filename();
135 size_t dash = rootName.find('-');
136 if (dash == std::string::npos)
137 {
138 std::cerr << "Error finding root bus for " << rootName << "\n";
139 continue;
140 }
141 rootBus = std::stoi(rootName.substr(0, dash));
142 }
143
144 std::shared_ptr<NVMeContext> context;
145 auto findRoot = nvmeDeviceMap.find(rootBus);
146 if (findRoot != nvmeDeviceMap.end())
147 {
148 context = findRoot->second;
149 }
150 else
151 {
152#if HAVE_NVME_MI_MCTP
153 context = std::make_shared<NVMeMCTPContext>(io, rootBus);
154#else
155 context = std::make_shared<NVMeBasicContext>(io, rootBus);
156#endif
157 nvmeDeviceMap[rootBus] = context;
158 }
159
160 std::shared_ptr<NVMeSensor> sensorPtr = std::make_shared<NVMeSensor>(
Andrew Jefferye671f052021-12-02 14:47:20 +1030161 objectServer, io, dbusConnection, *sensorName,
Andrew Jeffery34123562021-12-02 14:38:41 +1030162 std::move(sensorThresholds), interfacePath, *busNumber);
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030163
164 context->addSensor(sensorPtr);
165 }
166 for (const auto& [_, context] : nvmeDeviceMap)
167 {
168 context->pollNVMeDevices();
169 }
170}
171
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700172void createSensors(boost::asio::io_service& io,
173 sdbusplus::asio::object_server& objectServer,
174 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
175{
176
177 auto getter = std::make_shared<GetSensorConfiguration>(
178 dbusConnection,
179 std::move([&io, &objectServer, &dbusConnection](
180 const ManagedObjectType& sensorConfigurations) {
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030181 handleSensorConfigurations(io, objectServer, dbusConnection,
182 sensorConfigurations);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700183 }));
184 getter->getConfiguration(std::vector<std::string>{sensorType});
185}
186
187int main()
188{
189 boost::asio::io_service io;
190 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
191 systemBus->request_name("xyz.openbmc_project.NVMeSensor");
192 sdbusplus::asio::object_server objectServer(systemBus);
Andrew Jeffery6f25e7e2021-05-24 12:52:53 +0930193#if HAVE_NVME_MI_MCTP
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700194 nvmeMCTP::init();
Andrew Jeffery6f25e7e2021-05-24 12:52:53 +0930195#endif
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700196
197 io.post([&]() { createSensors(io, objectServer, systemBus); });
198
199 boost::asio::deadline_timer filterTimer(io);
200 std::function<void(sdbusplus::message::message&)> eventHandler =
201 [&filterTimer, &io, &objectServer,
James Feist7d7579f2020-09-02 14:13:08 -0700202 &systemBus](sdbusplus::message::message&) {
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700203 // this implicitly cancels the timer
204 filterTimer.expires_from_now(boost::posix_time::seconds(1));
205
206 filterTimer.async_wait([&](const boost::system::error_code& ec) {
207 if (ec == boost::asio::error::operation_aborted)
208 {
209 return; // we're being canceled
210 }
Andrew Jeffery7279f932021-05-25 13:35:27 +0930211
212 if (ec)
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700213 {
214 std::cerr << "Error: " << ec.message() << "\n";
215 return;
216 }
217
218 createSensors(io, objectServer, systemBus);
219 });
220 };
221
222 sdbusplus::bus::match::match configMatch(
223 static_cast<sdbusplus::bus::bus&>(*systemBus),
224 "type='signal',member='PropertiesChanged',path_namespace='" +
225 std::string(inventoryPath) + "',arg0namespace='" +
226 std::string(sensorType) + "'",
227 eventHandler);
228
Bruce Lee1263c3d2021-06-04 15:16:33 +0800229 setupManufacturingModeMatch(*systemBus);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700230 io.run();
231}