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