Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "NVMeBasicContext.hpp" |
| 18 | #include "NVMeContext.hpp" |
| 19 | #include "NVMeSensor.hpp" |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 20 | #include "Thresholds.hpp" |
| 21 | #include "Utils.hpp" |
| 22 | #include "VariantVisitors.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 23 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 24 | #include <boost/asio/error.hpp> |
| 25 | #include <boost/asio/io_context.hpp> |
| 26 | #include <boost/asio/post.hpp> |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 27 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 28 | #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 Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 34 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 35 | #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 Jeffery | 3412356 | 2021-12-02 14:38:41 +1030 | [diff] [blame] | 44 | #include <optional> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 45 | #include <stdexcept> |
| 46 | #include <string> |
| 47 | #include <utility> |
| 48 | #include <variant> |
| 49 | #include <vector> |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 50 | |
Nnamdi Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 51 | static constexpr uint8_t nvmeMiDefaultSlaveAddr = 0x6A; |
| 52 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 53 | static NVMEMap nvmeDeviceMap; |
| 54 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 55 | NVMEMap& getNVMEMap() |
| 56 | { |
| 57 | return nvmeDeviceMap; |
| 58 | } |
| 59 | |
Andrew Jeffery | 3412356 | 2021-12-02 14:38:41 +1030 | [diff] [blame] | 60 | static 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 Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 74 | static 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 Jeffery | e671f05 | 2021-12-02 14:47:20 +1030 | [diff] [blame] | 89 | static 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 Jeffery | 710562a | 2021-12-02 14:55:55 +1030 | [diff] [blame] | 104 | static std::filesystem::path deriveRootBusPath(int busNumber) |
| 105 | { |
| 106 | return "/sys/bus/i2c/devices/i2c-" + std::to_string(busNumber) + |
| 107 | "/mux_device"; |
| 108 | } |
| 109 | |
Andrew Jeffery | 66ab840 | 2021-12-02 15:03:51 +1030 | [diff] [blame] | 110 | static 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 Tanous | b074528 | 2024-04-03 18:57:45 -0700 | [diff] [blame] | 121 | return busNumber; |
Andrew Jeffery | 66ab840 | 2021-12-02 15:03:51 +1030 | [diff] [blame] | 122 | } |
| 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 Jeffery | c7a89e5 | 2021-12-02 15:10:23 +1030 | [diff] [blame] | 135 | static std::shared_ptr<NVMeContext> |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 136 | provideRootBusContext(boost::asio::io_context& io, NVMEMap& map, |
Andrew Jeffery | c7a89e5 | 2021-12-02 15:10:23 +1030 | [diff] [blame] | 137 | 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 Jeffery | c7a89e5 | 2021-12-02 15:10:23 +1030 | [diff] [blame] | 146 | std::make_shared<NVMeBasicContext>(io, rootBus); |
Andrew Jeffery | c7a89e5 | 2021-12-02 15:10:23 +1030 | [diff] [blame] | 147 | map[rootBus] = context; |
| 148 | |
| 149 | return context; |
| 150 | } |
| 151 | |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 152 | static void handleSensorConfigurations( |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 153 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 154 | 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 Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 168 | for (const auto& [interfacePath, sensorData] : sensorConfigurations) |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 169 | { |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 170 | // find base configuration |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 171 | auto sensorBase = |
| 172 | sensorData.find(configInterfaceName(NVMeSensor::sensorType)); |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 173 | if (sensorBase == sensorData.end()) |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 174 | { |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 175 | continue; |
| 176 | } |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 177 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 178 | const SensorBaseConfigMap& sensorConfig = sensorBase->second; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 179 | std::optional<int> busNumber = extractBusNumber(interfacePath, |
| 180 | sensorConfig); |
| 181 | std::optional<std::string> sensorName = extractSensorName(interfacePath, |
| 182 | sensorConfig); |
Nnamdi Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 183 | uint8_t slaveAddr = extractSlaveAddr(interfacePath, sensorConfig); |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 184 | std::optional<int> rootBus = deriveRootBus(busNumber); |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 185 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 186 | if (!(busNumber && sensorName && rootBus)) |
| 187 | { |
| 188 | continue; |
| 189 | } |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 190 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 191 | std::vector<thresholds::Threshold> sensorThresholds; |
| 192 | if (!parseThresholdsFromConfig(sensorData, sensorThresholds)) |
| 193 | { |
| 194 | std::cerr << "error populating thresholds for " << *sensorName |
| 195 | << "\n"; |
| 196 | } |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 197 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 198 | try |
| 199 | { |
| 200 | // May throw for an invalid rootBus |
| 201 | std::shared_ptr<NVMeContext> context = |
| 202 | provideRootBusContext(io, nvmeDeviceMap, *rootBus); |
Andrew Jeffery | 25e20bd | 2022-03-15 22:26:04 +1030 | [diff] [blame] | 203 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 204 | // 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 Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 209 | std::move(sensorThresholds), interfacePath, *busNumber, |
| 210 | slaveAddr); |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 211 | |
| 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 Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 219 | } |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 220 | } |
| 221 | for (const auto& [_, context] : nvmeDeviceMap) |
| 222 | { |
| 223 | context->pollNVMeDevices(); |
| 224 | } |
| 225 | } |
| 226 | |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 227 | void createSensors(boost::asio::io_context& io, |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 228 | sdbusplus::asio::object_server& objectServer, |
| 229 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 230 | { |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 231 | auto getter = std::make_shared<GetSensorConfiguration>( |
Ed Tanous | 86d8301 | 2022-02-18 09:51:47 -0800 | [diff] [blame] | 232 | dbusConnection, [&io, &objectServer, &dbusConnection]( |
| 233 | const ManagedObjectType& sensorConfigurations) { |
Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 234 | handleSensorConfigurations(io, objectServer, dbusConnection, |
| 235 | sensorConfigurations); |
| 236 | }); |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 237 | getter->getConfiguration(std::vector<std::string>{NVMeSensor::sensorType}); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 240 | static void interfaceRemoved(sdbusplus::message_t& message, NVMEMap& contexts) |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 241 | { |
| 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 Spinler | 55832f3 | 2023-06-07 10:24:00 -0500 | [diff] [blame] | 263 | (*sensor)->configInterface); |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 264 | if (interface == interfaces.end()) |
| 265 | { |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | context->removeSensor(sensor.value()); |
| 270 | } |
| 271 | } |
| 272 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 273 | int main() |
| 274 | { |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 275 | boost::asio::io_context io; |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 276 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 277 | systemBus->request_name("xyz.openbmc_project.NVMeSensor"); |
Johnathan Mantey | 661d437 | 2022-10-27 09:00:59 -0700 | [diff] [blame] | 278 | sdbusplus::asio::object_server objectServer(systemBus, true); |
Andrew Jeffery | ea148ec | 2023-01-17 15:43:33 +1030 | [diff] [blame] | 279 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 280 | |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 281 | boost::asio::post(io, |
| 282 | [&]() { createSensors(io, objectServer, systemBus); }); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 283 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 284 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 285 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 286 | [&filterTimer, &io, &objectServer, &systemBus](sdbusplus::message_t&) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 287 | // this implicitly cancels the timer |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 288 | filterTimer.expires_after(std::chrono::seconds(1)); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 289 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 290 | 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 Jeffery | 7279f93 | 2021-05-25 13:35:27 +0930 | [diff] [blame] | 295 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 296 | if (ec) |
| 297 | { |
| 298 | std::cerr << "Error: " << ec.message() << "\n"; |
| 299 | return; |
| 300 | } |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 301 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 302 | createSensors(io, objectServer, systemBus); |
| 303 | }); |
| 304 | }; |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 305 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 306 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 307 | setupPropertiesChangedMatches( |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 308 | *systemBus, std::to_array<const char*>({NVMeSensor::sensorType}), |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 309 | eventHandler); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 310 | |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 311 | // Watch for entity-manager to remove configuration interfaces |
| 312 | // so the corresponding sensors can be removed. |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 313 | auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 314 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 315 | "type='signal',member='InterfacesRemoved',arg0path='" + |
| 316 | std::string(inventoryPath) + "/'", |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 317 | [](sdbusplus::message_t& msg) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 318 | interfaceRemoved(msg, nvmeDeviceMap); |
Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 319 | }); |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 320 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 321 | setupManufacturingModeMatch(*systemBus); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 322 | io.run(); |
| 323 | } |