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