blob: 5f0222deaa2e066f81645cf2acfdf0b4f8facd8e [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 Jefferye73bd0a2023-01-25 10:39:57 +103017#include "NVMeBasicContext.hpp"
18#include "NVMeContext.hpp"
19#include "NVMeSensor.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include "Thresholds.hpp"
21#include "Utils.hpp"
22#include "VariantVisitors.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103023
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include <boost/asio/error.hpp>
25#include <boost/asio/io_context.hpp>
26#include <boost/asio/post.hpp>
Ed Tanous9b4a20e2022-09-06 08:47:11 -070027#include <boost/asio/steady_timer.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30#include <sdbusplus/bus.hpp>
31#include <sdbusplus/bus/match.hpp>
32#include <sdbusplus/message.hpp>
33#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070034
Ed Tanouseacbfdd2024-04-04 12:00:24 -070035#include <algorithm>
36#include <array>
37#include <chrono>
38#include <cstddef>
39#include <cstdint>
40#include <filesystem>
41#include <functional>
42#include <iostream>
43#include <memory>
Andrew Jeffery34123562021-12-02 14:38:41 +103044#include <optional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070045#include <stdexcept>
46#include <string>
47#include <utility>
48#include <variant>
49#include <vector>
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070050
Nnamdi Ajah06cd9882023-02-15 13:21:32 +010051static constexpr uint8_t nvmeMiDefaultSlaveAddr = 0x6A;
52
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070053static NVMEMap nvmeDeviceMap;
54
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070055NVMEMap& getNVMEMap()
56{
57 return nvmeDeviceMap;
58}
59
Andrew Jeffery34123562021-12-02 14:38:41 +103060static std::optional<int>
61 extractBusNumber(const std::string& path,
62 const SensorBaseConfigMap& properties)
63{
64 auto findBus = properties.find("Bus");
65 if (findBus == properties.end())
66 {
67 std::cerr << "could not determine bus number for " << path << "\n";
68 return std::nullopt;
69 }
70
71 return std::visit(VariantToIntVisitor(), findBus->second);
72}
73
Nnamdi Ajah06cd9882023-02-15 13:21:32 +010074static uint8_t extractSlaveAddr(const std::string& path,
75 const SensorBaseConfigMap& properties)
76{
77 auto findSlaveAddr = properties.find("Address");
78 if (findSlaveAddr == properties.end())
79 {
80 std::cerr << "could not determine slave address for " << path << "\n"
81 << "using default as specified in nvme-mi"
82 << "\n";
83 return nvmeMiDefaultSlaveAddr;
84 }
85
86 return std::visit(VariantToUnsignedIntVisitor(), findSlaveAddr->second);
87}
88
Andrew Jefferye671f052021-12-02 14:47:20 +103089static std::optional<std::string>
90 extractSensorName(const std::string& path,
91 const SensorBaseConfigMap& properties)
92{
93 auto findSensorName = properties.find("Name");
94 if (findSensorName == properties.end())
95 {
96 std::cerr << "could not determine configuration name for " << path
97 << "\n";
98 return std::nullopt;
99 }
100
101 return std::get<std::string>(findSensorName->second);
102}
103
Andrew Jeffery710562a2021-12-02 14:55:55 +1030104static std::filesystem::path deriveRootBusPath(int busNumber)
105{
106 return "/sys/bus/i2c/devices/i2c-" + std::to_string(busNumber) +
107 "/mux_device";
108}
109
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030110static std::optional<int> deriveRootBus(std::optional<int> busNumber)
111{
112 if (!busNumber)
113 {
114 return std::nullopt;
115 }
116
117 std::filesystem::path muxPath = deriveRootBusPath(*busNumber);
118
119 if (!std::filesystem::is_symlink(muxPath))
120 {
Ed Tanousb0745282024-04-03 18:57:45 -0700121 return busNumber;
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030122 }
123
124 std::string rootName = std::filesystem::read_symlink(muxPath).filename();
125 size_t dash = rootName.find('-');
126 if (dash == std::string::npos)
127 {
128 std::cerr << "Error finding root bus for " << rootName << "\n";
129 return std::nullopt;
130 }
131
132 return std::stoi(rootName.substr(0, dash));
133}
134
Andrew Jefferyc7a89e52021-12-02 15:10:23 +1030135static std::shared_ptr<NVMeContext>
Ed Tanous1f978632023-02-28 18:16:39 -0800136 provideRootBusContext(boost::asio::io_context& io, NVMEMap& map,
Andrew Jefferyc7a89e52021-12-02 15:10:23 +1030137 int rootBus)
138{
139 auto findRoot = map.find(rootBus);
140 if (findRoot != map.end())
141 {
142 return findRoot->second;
143 }
144
145 std::shared_ptr<NVMeContext> context =
Andrew Jefferyc7a89e52021-12-02 15:10:23 +1030146 std::make_shared<NVMeBasicContext>(io, rootBus);
Andrew Jefferyc7a89e52021-12-02 15:10:23 +1030147 map[rootBus] = context;
148
149 return context;
150}
151
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030152static void handleSensorConfigurations(
Ed Tanous1f978632023-02-28 18:16:39 -0800153 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030154 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
155 const ManagedObjectType& sensorConfigurations)
156{
157 // todo: it'd be better to only update the ones we care about
158 for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap)
159 {
160 if (nvmeContextPtr)
161 {
162 nvmeContextPtr->close();
163 }
164 }
165 nvmeDeviceMap.clear();
166
167 // iterate through all found configurations
Andrew Jefferyee164342021-12-02 16:02:19 +1030168 for (const auto& [interfacePath, sensorData] : sensorConfigurations)
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030169 {
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030170 // find base configuration
Zev Weiss054aad82022-08-18 01:37:34 -0700171 auto sensorBase =
172 sensorData.find(configInterfaceName(NVMeSensor::sensorType));
Zev Weissefae44c2022-08-16 15:39:20 -0700173 if (sensorBase == sensorData.end())
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030174 {
Zev Weissefae44c2022-08-16 15:39:20 -0700175 continue;
176 }
Andrew Jefferyee164342021-12-02 16:02:19 +1030177
Zev Weissefae44c2022-08-16 15:39:20 -0700178 const SensorBaseConfigMap& sensorConfig = sensorBase->second;
Patrick Williams779c96a2023-05-10 07:50:42 -0500179 std::optional<int> busNumber = extractBusNumber(interfacePath,
180 sensorConfig);
181 std::optional<std::string> sensorName = extractSensorName(interfacePath,
182 sensorConfig);
Nnamdi Ajah06cd9882023-02-15 13:21:32 +0100183 uint8_t slaveAddr = extractSlaveAddr(interfacePath, sensorConfig);
Zev Weissefae44c2022-08-16 15:39:20 -0700184 std::optional<int> rootBus = deriveRootBus(busNumber);
Andrew Jefferyee164342021-12-02 16:02:19 +1030185
Zev Weissefae44c2022-08-16 15:39:20 -0700186 if (!(busNumber && sensorName && rootBus))
187 {
188 continue;
189 }
Andrew Jefferyee164342021-12-02 16:02:19 +1030190
Zev Weissefae44c2022-08-16 15:39:20 -0700191 std::vector<thresholds::Threshold> sensorThresholds;
192 if (!parseThresholdsFromConfig(sensorData, sensorThresholds))
193 {
194 std::cerr << "error populating thresholds for " << *sensorName
195 << "\n";
196 }
Andrew Jefferyee164342021-12-02 16:02:19 +1030197
Zev Weissefae44c2022-08-16 15:39:20 -0700198 try
199 {
200 // May throw for an invalid rootBus
201 std::shared_ptr<NVMeContext> context =
202 provideRootBusContext(io, nvmeDeviceMap, *rootBus);
Andrew Jeffery25e20bd2022-03-15 22:26:04 +1030203
Zev Weissefae44c2022-08-16 15:39:20 -0700204 // Construct the sensor after grabbing the context so we don't
205 // glitch D-Bus May throw for an invalid busNumber
206 std::shared_ptr<NVMeSensor> sensorPtr =
207 std::make_shared<NVMeSensor>(
208 objectServer, io, dbusConnection, *sensorName,
Nnamdi Ajah06cd9882023-02-15 13:21:32 +0100209 std::move(sensorThresholds), interfacePath, *busNumber,
210 slaveAddr);
Zev Weissefae44c2022-08-16 15:39:20 -0700211
212 context->addSensor(sensorPtr);
213 }
214 catch (const std::invalid_argument& ex)
215 {
216 std::cerr << "Failed to add sensor for "
217 << std::string(interfacePath) << ": " << ex.what()
218 << "\n";
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030219 }
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030220 }
221 for (const auto& [_, context] : nvmeDeviceMap)
222 {
223 context->pollNVMeDevices();
224 }
225}
226
Ed Tanous1f978632023-02-28 18:16:39 -0800227void createSensors(boost::asio::io_context& io,
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700228 sdbusplus::asio::object_server& objectServer,
229 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
230{
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700231 auto getter = std::make_shared<GetSensorConfiguration>(
Ed Tanous86d83012022-02-18 09:51:47 -0800232 dbusConnection, [&io, &objectServer, &dbusConnection](
233 const ManagedObjectType& sensorConfigurations) {
Patrick Williams597e8422023-10-20 11:19:01 -0500234 handleSensorConfigurations(io, objectServer, dbusConnection,
235 sensorConfigurations);
236 });
Zev Weiss054aad82022-08-18 01:37:34 -0700237 getter->getConfiguration(std::vector<std::string>{NVMeSensor::sensorType});
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700238}
239
Patrick Williams92f8f512022-07-22 19:26:55 -0500240static void interfaceRemoved(sdbusplus::message_t& message, NVMEMap& contexts)
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030241{
242 if (message.is_method_error())
243 {
244 std::cerr << "interfacesRemoved callback method error\n";
245 return;
246 }
247
248 sdbusplus::message::object_path path;
249 std::vector<std::string> interfaces;
250
251 message.read(path, interfaces);
252
253 for (auto& [_, context] : contexts)
254 {
255 std::optional<std::shared_ptr<NVMeSensor>> sensor =
256 context->getSensorAtPath(path);
257 if (!sensor)
258 {
259 continue;
260 }
261
262 auto interface = std::find(interfaces.begin(), interfaces.end(),
Matt Spinler55832f32023-06-07 10:24:00 -0500263 (*sensor)->configInterface);
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030264 if (interface == interfaces.end())
265 {
266 continue;
267 }
268
269 context->removeSensor(sensor.value());
270 }
271}
272
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700273int main()
274{
Ed Tanous1f978632023-02-28 18:16:39 -0800275 boost::asio::io_context io;
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700276 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
277 systemBus->request_name("xyz.openbmc_project.NVMeSensor");
Johnathan Mantey661d4372022-10-27 09:00:59 -0700278 sdbusplus::asio::object_server objectServer(systemBus, true);
Andrew Jefferyea148ec2023-01-17 15:43:33 +1030279 objectServer.add_manager("/xyz/openbmc_project/sensors");
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700280
Ed Tanous83db50c2023-03-01 10:20:24 -0800281 boost::asio::post(io,
282 [&]() { createSensors(io, objectServer, systemBus); });
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700283
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700284 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500285 std::function<void(sdbusplus::message_t&)> eventHandler =
286 [&filterTimer, &io, &objectServer, &systemBus](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700287 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800288 filterTimer.expires_after(std::chrono::seconds(1));
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700289
Ed Tanousbb679322022-05-16 16:10:00 -0700290 filterTimer.async_wait([&](const boost::system::error_code& ec) {
291 if (ec == boost::asio::error::operation_aborted)
292 {
293 return; // we're being canceled
294 }
Andrew Jeffery7279f932021-05-25 13:35:27 +0930295
Ed Tanousbb679322022-05-16 16:10:00 -0700296 if (ec)
297 {
298 std::cerr << "Error: " << ec.message() << "\n";
299 return;
300 }
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700301
Ed Tanousbb679322022-05-16 16:10:00 -0700302 createSensors(io, objectServer, systemBus);
303 });
304 };
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700305
Zev Weiss214d9712022-08-12 12:54:31 -0700306 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
307 setupPropertiesChangedMatches(
Zev Weiss054aad82022-08-18 01:37:34 -0700308 *systemBus, std::to_array<const char*>({NVMeSensor::sensorType}),
Zev Weiss214d9712022-08-12 12:54:31 -0700309 eventHandler);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700310
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030311 // Watch for entity-manager to remove configuration interfaces
312 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500313 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
314 static_cast<sdbusplus::bus_t&>(*systemBus),
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030315 "type='signal',member='InterfacesRemoved',arg0path='" +
316 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500317 [](sdbusplus::message_t& msg) {
Ed Tanousbb679322022-05-16 16:10:00 -0700318 interfaceRemoved(msg, nvmeDeviceMap);
Patrick Williams597e8422023-10-20 11:19:01 -0500319 });
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030320
Bruce Lee1263c3d2021-06-04 15:16:33 +0800321 setupManufacturingModeMatch(*systemBus);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700322 io.run();
323}