blob: 1b48248ca4aa33267f0f4e0be686ab2f4fdbe597 [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 Jeffery66ab8402021-12-02 15:03:51 +103073static std::optional<int> deriveRootBus(std::optional<int> busNumber)
74{
75 if (!busNumber)
76 {
77 return std::nullopt;
78 }
79
80 std::filesystem::path muxPath = deriveRootBusPath(*busNumber);
81
82 if (!std::filesystem::is_symlink(muxPath))
83 {
84 return *busNumber;
85 }
86
87 std::string rootName = std::filesystem::read_symlink(muxPath).filename();
88 size_t dash = rootName.find('-');
89 if (dash == std::string::npos)
90 {
91 std::cerr << "Error finding root bus for " << rootName << "\n";
92 return std::nullopt;
93 }
94
95 return std::stoi(rootName.substr(0, dash));
96}
97
Andrew Jefferyf690f0c2021-12-02 11:42:58 +103098static void handleSensorConfigurations(
99 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
100 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
101 const ManagedObjectType& sensorConfigurations)
102{
103 // todo: it'd be better to only update the ones we care about
104 for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap)
105 {
106 if (nvmeContextPtr)
107 {
108 nvmeContextPtr->close();
109 }
110 }
111 nvmeDeviceMap.clear();
112
113 // iterate through all found configurations
114 for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor :
115 sensorConfigurations)
116 {
117 const SensorData& sensorData = sensor.second;
118 const std::string& interfacePath = sensor.first.str;
119 const std::pair<std::string, boost::container::flat_map<
120 std::string, BasicVariantType>>*
121 baseConfiguration = nullptr;
122
123 // find base configuration
124 auto sensorBase = sensor.second.find(sensorType);
125 if (sensorBase != sensor.second.end())
126 {
127 baseConfiguration = &(*sensorBase);
128 }
129
130 if (baseConfiguration == nullptr)
131 {
132 continue;
133 }
Andrew Jeffery34123562021-12-02 14:38:41 +1030134
135 std::optional<int> busNumber =
136 extractBusNumber(sensor.first, baseConfiguration->second);
137 if (!busNumber)
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030138 {
139 continue;
140 }
141
Andrew Jefferye671f052021-12-02 14:47:20 +1030142 std::optional<std::string> sensorName =
143 extractSensorName(sensor.first, baseConfiguration->second);
144 if (!sensorName)
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030145 {
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030146 continue;
147 }
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030148
149 std::vector<thresholds::Threshold> sensorThresholds;
150
151 if (!parseThresholdsFromConfig(sensorData, sensorThresholds))
152 {
Andrew Jefferye671f052021-12-02 14:47:20 +1030153 std::cerr << "error populating thresholds for " << *sensorName
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030154 << "\n";
155 }
156
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030157 std::optional<int> rootBus = deriveRootBus(busNumber);
158 if (!rootBus)
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030159 {
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030160 continue;
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030161 }
162
163 std::shared_ptr<NVMeContext> context;
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030164 auto findRoot = nvmeDeviceMap.find(*rootBus);
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030165 if (findRoot != nvmeDeviceMap.end())
166 {
167 context = findRoot->second;
168 }
169 else
170 {
171#if HAVE_NVME_MI_MCTP
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030172 context = std::make_shared<NVMeMCTPContext>(io, *rootBus);
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030173#else
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030174 context = std::make_shared<NVMeBasicContext>(io, *rootBus);
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030175#endif
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030176 nvmeDeviceMap[*rootBus] = context;
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030177 }
178
179 std::shared_ptr<NVMeSensor> sensorPtr = std::make_shared<NVMeSensor>(
Andrew Jefferye671f052021-12-02 14:47:20 +1030180 objectServer, io, dbusConnection, *sensorName,
Andrew Jeffery34123562021-12-02 14:38:41 +1030181 std::move(sensorThresholds), interfacePath, *busNumber);
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030182
183 context->addSensor(sensorPtr);
184 }
185 for (const auto& [_, context] : nvmeDeviceMap)
186 {
187 context->pollNVMeDevices();
188 }
189}
190
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700191void createSensors(boost::asio::io_service& io,
192 sdbusplus::asio::object_server& objectServer,
193 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
194{
195
196 auto getter = std::make_shared<GetSensorConfiguration>(
197 dbusConnection,
198 std::move([&io, &objectServer, &dbusConnection](
199 const ManagedObjectType& sensorConfigurations) {
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030200 handleSensorConfigurations(io, objectServer, dbusConnection,
201 sensorConfigurations);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700202 }));
203 getter->getConfiguration(std::vector<std::string>{sensorType});
204}
205
206int main()
207{
208 boost::asio::io_service io;
209 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
210 systemBus->request_name("xyz.openbmc_project.NVMeSensor");
211 sdbusplus::asio::object_server objectServer(systemBus);
Andrew Jeffery6f25e7e2021-05-24 12:52:53 +0930212#if HAVE_NVME_MI_MCTP
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700213 nvmeMCTP::init();
Andrew Jeffery6f25e7e2021-05-24 12:52:53 +0930214#endif
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700215
216 io.post([&]() { createSensors(io, objectServer, systemBus); });
217
218 boost::asio::deadline_timer filterTimer(io);
219 std::function<void(sdbusplus::message::message&)> eventHandler =
220 [&filterTimer, &io, &objectServer,
James Feist7d7579f2020-09-02 14:13:08 -0700221 &systemBus](sdbusplus::message::message&) {
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700222 // this implicitly cancels the timer
223 filterTimer.expires_from_now(boost::posix_time::seconds(1));
224
225 filterTimer.async_wait([&](const boost::system::error_code& ec) {
226 if (ec == boost::asio::error::operation_aborted)
227 {
228 return; // we're being canceled
229 }
Andrew Jeffery7279f932021-05-25 13:35:27 +0930230
231 if (ec)
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700232 {
233 std::cerr << "Error: " << ec.message() << "\n";
234 return;
235 }
236
237 createSensors(io, objectServer, systemBus);
238 });
239 };
240
241 sdbusplus::bus::match::match configMatch(
242 static_cast<sdbusplus::bus::bus&>(*systemBus),
243 "type='signal',member='PropertiesChanged',path_namespace='" +
244 std::string(inventoryPath) + "',arg0namespace='" +
245 std::string(sensorType) + "'",
246 eventHandler);
247
Bruce Lee1263c3d2021-06-04 15:16:33 +0800248 setupManufacturingModeMatch(*systemBus);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700249 io.run();
250}