blob: 2fe295da3b82d85230826e6ad5d419199478b3e9 [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
Patrick Williams2aaf7172024-08-16 15:20:40 -040060static std::optional<int> extractBusNumber(
61 const std::string& path, const SensorBaseConfigMap& properties)
Andrew Jeffery34123562021-12-02 14:38:41 +103062{
63 auto findBus = properties.find("Bus");
64 if (findBus == properties.end())
65 {
66 std::cerr << "could not determine bus number for " << path << "\n";
67 return std::nullopt;
68 }
69
70 return std::visit(VariantToIntVisitor(), findBus->second);
71}
72
Nnamdi Ajah06cd9882023-02-15 13:21:32 +010073static uint8_t extractSlaveAddr(const std::string& path,
74 const SensorBaseConfigMap& properties)
75{
76 auto findSlaveAddr = properties.find("Address");
77 if (findSlaveAddr == properties.end())
78 {
79 std::cerr << "could not determine slave address for " << path << "\n"
80 << "using default as specified in nvme-mi"
81 << "\n";
82 return nvmeMiDefaultSlaveAddr;
83 }
84
85 return std::visit(VariantToUnsignedIntVisitor(), findSlaveAddr->second);
86}
87
Patrick Williams2aaf7172024-08-16 15:20:40 -040088static std::optional<std::string> extractSensorName(
89 const std::string& path, const SensorBaseConfigMap& properties)
Andrew Jefferye671f052021-12-02 14:47:20 +103090{
91 auto findSensorName = properties.find("Name");
92 if (findSensorName == properties.end())
93 {
94 std::cerr << "could not determine configuration name for " << path
95 << "\n";
96 return std::nullopt;
97 }
98
99 return std::get<std::string>(findSensorName->second);
100}
101
Andrew Jeffery710562a2021-12-02 14:55:55 +1030102static std::filesystem::path deriveRootBusPath(int busNumber)
103{
104 return "/sys/bus/i2c/devices/i2c-" + std::to_string(busNumber) +
105 "/mux_device";
106}
107
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030108static std::optional<int> deriveRootBus(std::optional<int> busNumber)
109{
110 if (!busNumber)
111 {
112 return std::nullopt;
113 }
114
115 std::filesystem::path muxPath = deriveRootBusPath(*busNumber);
116
117 if (!std::filesystem::is_symlink(muxPath))
118 {
Ed Tanousb0745282024-04-03 18:57:45 -0700119 return busNumber;
Andrew Jeffery66ab8402021-12-02 15:03:51 +1030120 }
121
122 std::string rootName = std::filesystem::read_symlink(muxPath).filename();
123 size_t dash = rootName.find('-');
124 if (dash == std::string::npos)
125 {
126 std::cerr << "Error finding root bus for " << rootName << "\n";
127 return std::nullopt;
128 }
129
130 return std::stoi(rootName.substr(0, dash));
131}
132
Patrick Williams2aaf7172024-08-16 15:20:40 -0400133static std::shared_ptr<NVMeContext> provideRootBusContext(
134 boost::asio::io_context& io, NVMEMap& map, int rootBus)
Andrew Jefferyc7a89e52021-12-02 15:10:23 +1030135{
136 auto findRoot = map.find(rootBus);
137 if (findRoot != map.end())
138 {
139 return findRoot->second;
140 }
141
142 std::shared_ptr<NVMeContext> context =
Andrew Jefferyc7a89e52021-12-02 15:10:23 +1030143 std::make_shared<NVMeBasicContext>(io, rootBus);
Andrew Jefferyc7a89e52021-12-02 15:10:23 +1030144 map[rootBus] = context;
145
146 return context;
147}
148
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030149static void handleSensorConfigurations(
Ed Tanous1f978632023-02-28 18:16:39 -0800150 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030151 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
152 const ManagedObjectType& sensorConfigurations)
153{
154 // todo: it'd be better to only update the ones we care about
155 for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap)
156 {
157 if (nvmeContextPtr)
158 {
159 nvmeContextPtr->close();
160 }
161 }
162 nvmeDeviceMap.clear();
163
164 // iterate through all found configurations
Andrew Jefferyee164342021-12-02 16:02:19 +1030165 for (const auto& [interfacePath, sensorData] : sensorConfigurations)
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030166 {
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030167 // find base configuration
Zev Weiss054aad82022-08-18 01:37:34 -0700168 auto sensorBase =
169 sensorData.find(configInterfaceName(NVMeSensor::sensorType));
Zev Weissefae44c2022-08-16 15:39:20 -0700170 if (sensorBase == sensorData.end())
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030171 {
Zev Weissefae44c2022-08-16 15:39:20 -0700172 continue;
173 }
Andrew Jefferyee164342021-12-02 16:02:19 +1030174
Zev Weissefae44c2022-08-16 15:39:20 -0700175 const SensorBaseConfigMap& sensorConfig = sensorBase->second;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400176 std::optional<int> busNumber =
177 extractBusNumber(interfacePath, sensorConfig);
178 std::optional<std::string> sensorName =
179 extractSensorName(interfacePath, sensorConfig);
Nnamdi Ajah06cd9882023-02-15 13:21:32 +0100180 uint8_t slaveAddr = extractSlaveAddr(interfacePath, sensorConfig);
Zev Weissefae44c2022-08-16 15:39:20 -0700181 std::optional<int> rootBus = deriveRootBus(busNumber);
Andrew Jefferyee164342021-12-02 16:02:19 +1030182
Zev Weissefae44c2022-08-16 15:39:20 -0700183 if (!(busNumber && sensorName && rootBus))
184 {
185 continue;
186 }
Andrew Jefferyee164342021-12-02 16:02:19 +1030187
Zev Weissefae44c2022-08-16 15:39:20 -0700188 std::vector<thresholds::Threshold> sensorThresholds;
189 if (!parseThresholdsFromConfig(sensorData, sensorThresholds))
190 {
191 std::cerr << "error populating thresholds for " << *sensorName
192 << "\n";
193 }
Andrew Jefferyee164342021-12-02 16:02:19 +1030194
Zev Weissefae44c2022-08-16 15:39:20 -0700195 try
196 {
197 // May throw for an invalid rootBus
198 std::shared_ptr<NVMeContext> context =
199 provideRootBusContext(io, nvmeDeviceMap, *rootBus);
Andrew Jeffery25e20bd2022-03-15 22:26:04 +1030200
Zev Weissefae44c2022-08-16 15:39:20 -0700201 // Construct the sensor after grabbing the context so we don't
202 // glitch D-Bus May throw for an invalid busNumber
203 std::shared_ptr<NVMeSensor> sensorPtr =
204 std::make_shared<NVMeSensor>(
205 objectServer, io, dbusConnection, *sensorName,
Nnamdi Ajah06cd9882023-02-15 13:21:32 +0100206 std::move(sensorThresholds), interfacePath, *busNumber,
207 slaveAddr);
Zev Weissefae44c2022-08-16 15:39:20 -0700208
209 context->addSensor(sensorPtr);
210 }
211 catch (const std::invalid_argument& ex)
212 {
213 std::cerr << "Failed to add sensor for "
214 << std::string(interfacePath) << ": " << ex.what()
215 << "\n";
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030216 }
Andrew Jefferyf690f0c2021-12-02 11:42:58 +1030217 }
218 for (const auto& [_, context] : nvmeDeviceMap)
219 {
220 context->pollNVMeDevices();
221 }
222}
223
Ed Tanous1f978632023-02-28 18:16:39 -0800224void createSensors(boost::asio::io_context& io,
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700225 sdbusplus::asio::object_server& objectServer,
226 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
227{
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700228 auto getter = std::make_shared<GetSensorConfiguration>(
Ed Tanous86d83012022-02-18 09:51:47 -0800229 dbusConnection, [&io, &objectServer, &dbusConnection](
230 const ManagedObjectType& sensorConfigurations) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400231 handleSensorConfigurations(io, objectServer, dbusConnection,
232 sensorConfigurations);
233 });
Zev Weiss054aad82022-08-18 01:37:34 -0700234 getter->getConfiguration(std::vector<std::string>{NVMeSensor::sensorType});
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700235}
236
Patrick Williams92f8f512022-07-22 19:26:55 -0500237static void interfaceRemoved(sdbusplus::message_t& message, NVMEMap& contexts)
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030238{
239 if (message.is_method_error())
240 {
241 std::cerr << "interfacesRemoved callback method error\n";
242 return;
243 }
244
245 sdbusplus::message::object_path path;
246 std::vector<std::string> interfaces;
247
248 message.read(path, interfaces);
249
250 for (auto& [_, context] : contexts)
251 {
252 std::optional<std::shared_ptr<NVMeSensor>> sensor =
253 context->getSensorAtPath(path);
254 if (!sensor)
255 {
256 continue;
257 }
258
259 auto interface = std::find(interfaces.begin(), interfaces.end(),
Matt Spinler55832f32023-06-07 10:24:00 -0500260 (*sensor)->configInterface);
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030261 if (interface == interfaces.end())
262 {
263 continue;
264 }
265
266 context->removeSensor(sensor.value());
267 }
268}
269
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700270int main()
271{
Ed Tanous1f978632023-02-28 18:16:39 -0800272 boost::asio::io_context io;
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700273 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
274 systemBus->request_name("xyz.openbmc_project.NVMeSensor");
Johnathan Mantey661d4372022-10-27 09:00:59 -0700275 sdbusplus::asio::object_server objectServer(systemBus, true);
Andrew Jefferyea148ec2023-01-17 15:43:33 +1030276 objectServer.add_manager("/xyz/openbmc_project/sensors");
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700277
Ed Tanous83db50c2023-03-01 10:20:24 -0800278 boost::asio::post(io,
279 [&]() { createSensors(io, objectServer, systemBus); });
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700280
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700281 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500282 std::function<void(sdbusplus::message_t&)> eventHandler =
283 [&filterTimer, &io, &objectServer, &systemBus](sdbusplus::message_t&) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400284 // this implicitly cancels the timer
285 filterTimer.expires_after(std::chrono::seconds(1));
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700286
Patrick Williams2aaf7172024-08-16 15:20:40 -0400287 filterTimer.async_wait([&](const boost::system::error_code& ec) {
288 if (ec == boost::asio::error::operation_aborted)
289 {
290 return; // we're being canceled
291 }
Andrew Jeffery7279f932021-05-25 13:35:27 +0930292
Patrick Williams2aaf7172024-08-16 15:20:40 -0400293 if (ec)
294 {
295 std::cerr << "Error: " << ec.message() << "\n";
296 return;
297 }
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700298
Patrick Williams2aaf7172024-08-16 15:20:40 -0400299 createSensors(io, objectServer, systemBus);
300 });
301 };
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700302
Zev Weiss214d9712022-08-12 12:54:31 -0700303 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
304 setupPropertiesChangedMatches(
Zev Weiss054aad82022-08-18 01:37:34 -0700305 *systemBus, std::to_array<const char*>({NVMeSensor::sensorType}),
Zev Weiss214d9712022-08-12 12:54:31 -0700306 eventHandler);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700307
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030308 // Watch for entity-manager to remove configuration interfaces
309 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500310 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
311 static_cast<sdbusplus::bus_t&>(*systemBus),
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030312 "type='signal',member='InterfacesRemoved',arg0path='" +
313 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500314 [](sdbusplus::message_t& msg) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400315 interfaceRemoved(msg, nvmeDeviceMap);
316 });
Andrew Jefferyafd55b92021-12-08 10:58:17 +1030317
Bruce Lee1263c3d2021-06-04 15:16:33 +0800318 setupManufacturingModeMatch(*systemBus);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700319 io.run();
320}