blob: 2621674f81253e7c5fa3f6e83b00f6713f3a9e55 [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 Jeffery710562a2021-12-02 14:55:55 +103067static std::filesystem::path deriveRootBusPath(int busNumber)
68{
69 return "/sys/bus/i2c/devices/i2c-" + std::to_string(busNumber) +
70 "/mux_device";
71}
72
Andrew Jefferyf690f0c2021-12-02 11:42:58 +103073static void handleSensorConfigurations(
74 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
75 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
76 const ManagedObjectType& sensorConfigurations)
77{
78 // todo: it'd be better to only update the ones we care about
79 for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap)
80 {
81 if (nvmeContextPtr)
82 {
83 nvmeContextPtr->close();
84 }
85 }
86 nvmeDeviceMap.clear();
87
88 // iterate through all found configurations
89 for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor :
90 sensorConfigurations)
91 {
92 const SensorData& sensorData = sensor.second;
93 const std::string& interfacePath = sensor.first.str;
94 const std::pair<std::string, boost::container::flat_map<
95 std::string, BasicVariantType>>*
96 baseConfiguration = nullptr;
97
98 // find base configuration
99 auto sensorBase = sensor.second.find(sensorType);
100 if (sensorBase != sensor.second.end())
101 {
102 baseConfiguration = &(*sensorBase);
103 }
104
105 if (baseConfiguration == nullptr)
106 {
107 continue;
108 }
Andrew Jeffery34123562021-12-02 14:38:41 +1030109
110 std::optional<int> busNumber =
111 extractBusNumber(sensor.first, baseConfiguration->second);
112 if (!busNumber)
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030113 {
114 continue;
115 }
116
Andrew Jefferye671f052021-12-02 14:47:20 +1030117 std::optional<std::string> sensorName =
118 extractSensorName(sensor.first, baseConfiguration->second);
119 if (!sensorName)
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030120 {
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030121 continue;
122 }
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030123
124 std::vector<thresholds::Threshold> sensorThresholds;
125
126 if (!parseThresholdsFromConfig(sensorData, sensorThresholds))
127 {
Andrew Jefferye671f052021-12-02 14:47:20 +1030128 std::cerr << "error populating thresholds for " << *sensorName
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030129 << "\n";
130 }
131
Andrew Jeffery34123562021-12-02 14:38:41 +1030132 int rootBus = *busNumber;
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030133
Andrew Jeffery710562a2021-12-02 14:55:55 +1030134 std::filesystem::path muxPath = deriveRootBusPath(*busNumber);
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030135
136 if (std::filesystem::is_symlink(muxPath))
137 {
138 std::string rootName =
139 std::filesystem::read_symlink(muxPath).filename();
140 size_t dash = rootName.find('-');
141 if (dash == std::string::npos)
142 {
143 std::cerr << "Error finding root bus for " << rootName << "\n";
144 continue;
145 }
146 rootBus = std::stoi(rootName.substr(0, dash));
147 }
148
149 std::shared_ptr<NVMeContext> context;
150 auto findRoot = nvmeDeviceMap.find(rootBus);
151 if (findRoot != nvmeDeviceMap.end())
152 {
153 context = findRoot->second;
154 }
155 else
156 {
157#if HAVE_NVME_MI_MCTP
158 context = std::make_shared<NVMeMCTPContext>(io, rootBus);
159#else
160 context = std::make_shared<NVMeBasicContext>(io, rootBus);
161#endif
162 nvmeDeviceMap[rootBus] = context;
163 }
164
165 std::shared_ptr<NVMeSensor> sensorPtr = std::make_shared<NVMeSensor>(
Andrew Jefferye671f052021-12-02 14:47:20 +1030166 objectServer, io, dbusConnection, *sensorName,
Andrew Jeffery34123562021-12-02 14:38:41 +1030167 std::move(sensorThresholds), interfacePath, *busNumber);
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030168
169 context->addSensor(sensorPtr);
170 }
171 for (const auto& [_, context] : nvmeDeviceMap)
172 {
173 context->pollNVMeDevices();
174 }
175}
176
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700177void createSensors(boost::asio::io_service& io,
178 sdbusplus::asio::object_server& objectServer,
179 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
180{
181
182 auto getter = std::make_shared<GetSensorConfiguration>(
183 dbusConnection,
184 std::move([&io, &objectServer, &dbusConnection](
185 const ManagedObjectType& sensorConfigurations) {
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030186 handleSensorConfigurations(io, objectServer, dbusConnection,
187 sensorConfigurations);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700188 }));
189 getter->getConfiguration(std::vector<std::string>{sensorType});
190}
191
192int main()
193{
194 boost::asio::io_service io;
195 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
196 systemBus->request_name("xyz.openbmc_project.NVMeSensor");
197 sdbusplus::asio::object_server objectServer(systemBus);
Andrew Jeffery6f25e7e2021-05-24 12:52:53 +0930198#if HAVE_NVME_MI_MCTP
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700199 nvmeMCTP::init();
Andrew Jeffery6f25e7e2021-05-24 12:52:53 +0930200#endif
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700201
202 io.post([&]() { createSensors(io, objectServer, systemBus); });
203
204 boost::asio::deadline_timer filterTimer(io);
205 std::function<void(sdbusplus::message::message&)> eventHandler =
206 [&filterTimer, &io, &objectServer,
James Feist7d7579f2020-09-02 14:13:08 -0700207 &systemBus](sdbusplus::message::message&) {
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700208 // this implicitly cancels the timer
209 filterTimer.expires_from_now(boost::posix_time::seconds(1));
210
211 filterTimer.async_wait([&](const boost::system::error_code& ec) {
212 if (ec == boost::asio::error::operation_aborted)
213 {
214 return; // we're being canceled
215 }
Andrew Jeffery7279f932021-05-25 13:35:27 +0930216
217 if (ec)
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700218 {
219 std::cerr << "Error: " << ec.message() << "\n";
220 return;
221 }
222
223 createSensors(io, objectServer, systemBus);
224 });
225 };
226
227 sdbusplus::bus::match::match configMatch(
228 static_cast<sdbusplus::bus::bus&>(*systemBus),
229 "type='signal',member='PropertiesChanged',path_namespace='" +
230 std::string(inventoryPath) + "',arg0namespace='" +
231 std::string(sensorType) + "'",
232 eventHandler);
233
Bruce Lee1263c3d2021-06-04 15:16:33 +0800234 setupManufacturingModeMatch(*systemBus);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700235 io.run();
236}