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" |
| 20 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 21 | #include <boost/asio/steady_timer.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 22 | |
Andrew Jeffery | 3412356 | 2021-12-02 14:38:41 +1030 | [diff] [blame] | 23 | #include <optional> |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 24 | #include <regex> |
| 25 | |
Nnamdi Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 26 | static constexpr uint8_t nvmeMiDefaultSlaveAddr = 0x6A; |
| 27 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 28 | static NVMEMap nvmeDeviceMap; |
| 29 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 30 | NVMEMap& getNVMEMap() |
| 31 | { |
| 32 | return nvmeDeviceMap; |
| 33 | } |
| 34 | |
Andrew Jeffery | 3412356 | 2021-12-02 14:38:41 +1030 | [diff] [blame] | 35 | static std::optional<int> |
| 36 | extractBusNumber(const std::string& path, |
| 37 | const SensorBaseConfigMap& properties) |
| 38 | { |
| 39 | auto findBus = properties.find("Bus"); |
| 40 | if (findBus == properties.end()) |
| 41 | { |
| 42 | std::cerr << "could not determine bus number for " << path << "\n"; |
| 43 | return std::nullopt; |
| 44 | } |
| 45 | |
| 46 | return std::visit(VariantToIntVisitor(), findBus->second); |
| 47 | } |
| 48 | |
Nnamdi Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 49 | static uint8_t extractSlaveAddr(const std::string& path, |
| 50 | const SensorBaseConfigMap& properties) |
| 51 | { |
| 52 | auto findSlaveAddr = properties.find("Address"); |
| 53 | if (findSlaveAddr == properties.end()) |
| 54 | { |
| 55 | std::cerr << "could not determine slave address for " << path << "\n" |
| 56 | << "using default as specified in nvme-mi" |
| 57 | << "\n"; |
| 58 | return nvmeMiDefaultSlaveAddr; |
| 59 | } |
| 60 | |
| 61 | return std::visit(VariantToUnsignedIntVisitor(), findSlaveAddr->second); |
| 62 | } |
| 63 | |
Andrew Jeffery | e671f05 | 2021-12-02 14:47:20 +1030 | [diff] [blame] | 64 | static std::optional<std::string> |
| 65 | extractSensorName(const std::string& path, |
| 66 | const SensorBaseConfigMap& properties) |
| 67 | { |
| 68 | auto findSensorName = properties.find("Name"); |
| 69 | if (findSensorName == properties.end()) |
| 70 | { |
| 71 | std::cerr << "could not determine configuration name for " << path |
| 72 | << "\n"; |
| 73 | return std::nullopt; |
| 74 | } |
| 75 | |
| 76 | return std::get<std::string>(findSensorName->second); |
| 77 | } |
| 78 | |
Andrew Jeffery | 710562a | 2021-12-02 14:55:55 +1030 | [diff] [blame] | 79 | static std::filesystem::path deriveRootBusPath(int busNumber) |
| 80 | { |
| 81 | return "/sys/bus/i2c/devices/i2c-" + std::to_string(busNumber) + |
| 82 | "/mux_device"; |
| 83 | } |
| 84 | |
Andrew Jeffery | 66ab840 | 2021-12-02 15:03:51 +1030 | [diff] [blame] | 85 | static std::optional<int> deriveRootBus(std::optional<int> busNumber) |
| 86 | { |
| 87 | if (!busNumber) |
| 88 | { |
| 89 | return std::nullopt; |
| 90 | } |
| 91 | |
| 92 | std::filesystem::path muxPath = deriveRootBusPath(*busNumber); |
| 93 | |
| 94 | if (!std::filesystem::is_symlink(muxPath)) |
| 95 | { |
Ed Tanous | b074528 | 2024-04-03 18:57:45 -0700 | [diff] [blame^] | 96 | return busNumber; |
Andrew Jeffery | 66ab840 | 2021-12-02 15:03:51 +1030 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | std::string rootName = std::filesystem::read_symlink(muxPath).filename(); |
| 100 | size_t dash = rootName.find('-'); |
| 101 | if (dash == std::string::npos) |
| 102 | { |
| 103 | std::cerr << "Error finding root bus for " << rootName << "\n"; |
| 104 | return std::nullopt; |
| 105 | } |
| 106 | |
| 107 | return std::stoi(rootName.substr(0, dash)); |
| 108 | } |
| 109 | |
Andrew Jeffery | c7a89e5 | 2021-12-02 15:10:23 +1030 | [diff] [blame] | 110 | static std::shared_ptr<NVMeContext> |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 111 | provideRootBusContext(boost::asio::io_context& io, NVMEMap& map, |
Andrew Jeffery | c7a89e5 | 2021-12-02 15:10:23 +1030 | [diff] [blame] | 112 | int rootBus) |
| 113 | { |
| 114 | auto findRoot = map.find(rootBus); |
| 115 | if (findRoot != map.end()) |
| 116 | { |
| 117 | return findRoot->second; |
| 118 | } |
| 119 | |
| 120 | std::shared_ptr<NVMeContext> context = |
Andrew Jeffery | c7a89e5 | 2021-12-02 15:10:23 +1030 | [diff] [blame] | 121 | std::make_shared<NVMeBasicContext>(io, rootBus); |
Andrew Jeffery | c7a89e5 | 2021-12-02 15:10:23 +1030 | [diff] [blame] | 122 | map[rootBus] = context; |
| 123 | |
| 124 | return context; |
| 125 | } |
| 126 | |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 127 | static void handleSensorConfigurations( |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 128 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 129 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 130 | const ManagedObjectType& sensorConfigurations) |
| 131 | { |
| 132 | // todo: it'd be better to only update the ones we care about |
| 133 | for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap) |
| 134 | { |
| 135 | if (nvmeContextPtr) |
| 136 | { |
| 137 | nvmeContextPtr->close(); |
| 138 | } |
| 139 | } |
| 140 | nvmeDeviceMap.clear(); |
| 141 | |
| 142 | // iterate through all found configurations |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 143 | for (const auto& [interfacePath, sensorData] : sensorConfigurations) |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 144 | { |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 145 | // find base configuration |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 146 | auto sensorBase = |
| 147 | sensorData.find(configInterfaceName(NVMeSensor::sensorType)); |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 148 | if (sensorBase == sensorData.end()) |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 149 | { |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 150 | continue; |
| 151 | } |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 152 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 153 | const SensorBaseConfigMap& sensorConfig = sensorBase->second; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 154 | std::optional<int> busNumber = extractBusNumber(interfacePath, |
| 155 | sensorConfig); |
| 156 | std::optional<std::string> sensorName = extractSensorName(interfacePath, |
| 157 | sensorConfig); |
Nnamdi Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 158 | uint8_t slaveAddr = extractSlaveAddr(interfacePath, sensorConfig); |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 159 | std::optional<int> rootBus = deriveRootBus(busNumber); |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 160 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 161 | if (!(busNumber && sensorName && rootBus)) |
| 162 | { |
| 163 | continue; |
| 164 | } |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 165 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 166 | std::vector<thresholds::Threshold> sensorThresholds; |
| 167 | if (!parseThresholdsFromConfig(sensorData, sensorThresholds)) |
| 168 | { |
| 169 | std::cerr << "error populating thresholds for " << *sensorName |
| 170 | << "\n"; |
| 171 | } |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 172 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 173 | try |
| 174 | { |
| 175 | // May throw for an invalid rootBus |
| 176 | std::shared_ptr<NVMeContext> context = |
| 177 | provideRootBusContext(io, nvmeDeviceMap, *rootBus); |
Andrew Jeffery | 25e20bd | 2022-03-15 22:26:04 +1030 | [diff] [blame] | 178 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 179 | // Construct the sensor after grabbing the context so we don't |
| 180 | // glitch D-Bus May throw for an invalid busNumber |
| 181 | std::shared_ptr<NVMeSensor> sensorPtr = |
| 182 | std::make_shared<NVMeSensor>( |
| 183 | objectServer, io, dbusConnection, *sensorName, |
Nnamdi Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 184 | std::move(sensorThresholds), interfacePath, *busNumber, |
| 185 | slaveAddr); |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 186 | |
| 187 | context->addSensor(sensorPtr); |
| 188 | } |
| 189 | catch (const std::invalid_argument& ex) |
| 190 | { |
| 191 | std::cerr << "Failed to add sensor for " |
| 192 | << std::string(interfacePath) << ": " << ex.what() |
| 193 | << "\n"; |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 194 | } |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 195 | } |
| 196 | for (const auto& [_, context] : nvmeDeviceMap) |
| 197 | { |
| 198 | context->pollNVMeDevices(); |
| 199 | } |
| 200 | } |
| 201 | |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 202 | void createSensors(boost::asio::io_context& io, |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 203 | sdbusplus::asio::object_server& objectServer, |
| 204 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 205 | { |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 206 | auto getter = std::make_shared<GetSensorConfiguration>( |
Ed Tanous | 86d8301 | 2022-02-18 09:51:47 -0800 | [diff] [blame] | 207 | dbusConnection, [&io, &objectServer, &dbusConnection]( |
| 208 | const ManagedObjectType& sensorConfigurations) { |
Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 209 | handleSensorConfigurations(io, objectServer, dbusConnection, |
| 210 | sensorConfigurations); |
| 211 | }); |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 212 | getter->getConfiguration(std::vector<std::string>{NVMeSensor::sensorType}); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 215 | static void interfaceRemoved(sdbusplus::message_t& message, NVMEMap& contexts) |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 216 | { |
| 217 | if (message.is_method_error()) |
| 218 | { |
| 219 | std::cerr << "interfacesRemoved callback method error\n"; |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | sdbusplus::message::object_path path; |
| 224 | std::vector<std::string> interfaces; |
| 225 | |
| 226 | message.read(path, interfaces); |
| 227 | |
| 228 | for (auto& [_, context] : contexts) |
| 229 | { |
| 230 | std::optional<std::shared_ptr<NVMeSensor>> sensor = |
| 231 | context->getSensorAtPath(path); |
| 232 | if (!sensor) |
| 233 | { |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | auto interface = std::find(interfaces.begin(), interfaces.end(), |
Matt Spinler | 55832f3 | 2023-06-07 10:24:00 -0500 | [diff] [blame] | 238 | (*sensor)->configInterface); |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 239 | if (interface == interfaces.end()) |
| 240 | { |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | context->removeSensor(sensor.value()); |
| 245 | } |
| 246 | } |
| 247 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 248 | int main() |
| 249 | { |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 250 | boost::asio::io_context io; |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 251 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 252 | systemBus->request_name("xyz.openbmc_project.NVMeSensor"); |
Johnathan Mantey | 661d437 | 2022-10-27 09:00:59 -0700 | [diff] [blame] | 253 | sdbusplus::asio::object_server objectServer(systemBus, true); |
Andrew Jeffery | ea148ec | 2023-01-17 15:43:33 +1030 | [diff] [blame] | 254 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 255 | |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 256 | boost::asio::post(io, |
| 257 | [&]() { createSensors(io, objectServer, systemBus); }); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 258 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 259 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 260 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 261 | [&filterTimer, &io, &objectServer, &systemBus](sdbusplus::message_t&) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 262 | // this implicitly cancels the timer |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 263 | filterTimer.expires_after(std::chrono::seconds(1)); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 264 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 265 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 266 | if (ec == boost::asio::error::operation_aborted) |
| 267 | { |
| 268 | return; // we're being canceled |
| 269 | } |
Andrew Jeffery | 7279f93 | 2021-05-25 13:35:27 +0930 | [diff] [blame] | 270 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 271 | if (ec) |
| 272 | { |
| 273 | std::cerr << "Error: " << ec.message() << "\n"; |
| 274 | return; |
| 275 | } |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 276 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 277 | createSensors(io, objectServer, systemBus); |
| 278 | }); |
| 279 | }; |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 280 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 281 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 282 | setupPropertiesChangedMatches( |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 283 | *systemBus, std::to_array<const char*>({NVMeSensor::sensorType}), |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 284 | eventHandler); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 285 | |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 286 | // Watch for entity-manager to remove configuration interfaces |
| 287 | // so the corresponding sensors can be removed. |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 288 | auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 289 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 290 | "type='signal',member='InterfacesRemoved',arg0path='" + |
| 291 | std::string(inventoryPath) + "/'", |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 292 | [](sdbusplus::message_t& msg) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 293 | interfaceRemoved(msg, nvmeDeviceMap); |
Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 294 | }); |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 295 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 296 | setupManufacturingModeMatch(*systemBus); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 297 | io.run(); |
| 298 | } |