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 |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 128 | auto sensorBase = sensorData.find(NVMeSensor::configType); |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 129 | if (sensorBase == sensorData.end()) |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 130 | { |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 131 | continue; |
| 132 | } |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 133 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 134 | const SensorBaseConfigMap& sensorConfig = sensorBase->second; |
| 135 | std::optional<int> busNumber = |
| 136 | extractBusNumber(interfacePath, sensorConfig); |
| 137 | std::optional<std::string> sensorName = |
| 138 | extractSensorName(interfacePath, sensorConfig); |
| 139 | std::optional<int> rootBus = deriveRootBus(busNumber); |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 140 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 141 | if (!(busNumber && sensorName && rootBus)) |
| 142 | { |
| 143 | continue; |
| 144 | } |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 145 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 146 | std::vector<thresholds::Threshold> sensorThresholds; |
| 147 | if (!parseThresholdsFromConfig(sensorData, sensorThresholds)) |
| 148 | { |
| 149 | std::cerr << "error populating thresholds for " << *sensorName |
| 150 | << "\n"; |
| 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 | try |
| 154 | { |
| 155 | // May throw for an invalid rootBus |
| 156 | std::shared_ptr<NVMeContext> context = |
| 157 | provideRootBusContext(io, nvmeDeviceMap, *rootBus); |
Andrew Jeffery | 25e20bd | 2022-03-15 22:26:04 +1030 | [diff] [blame] | 158 | |
Zev Weiss | efae44c | 2022-08-16 15:39:20 -0700 | [diff] [blame] | 159 | // Construct the sensor after grabbing the context so we don't |
| 160 | // glitch D-Bus May throw for an invalid busNumber |
| 161 | std::shared_ptr<NVMeSensor> sensorPtr = |
| 162 | std::make_shared<NVMeSensor>( |
| 163 | objectServer, io, dbusConnection, *sensorName, |
| 164 | std::move(sensorThresholds), interfacePath, *busNumber); |
| 165 | |
| 166 | context->addSensor(sensorPtr); |
| 167 | } |
| 168 | catch (const std::invalid_argument& ex) |
| 169 | { |
| 170 | std::cerr << "Failed to add sensor for " |
| 171 | << std::string(interfacePath) << ": " << ex.what() |
| 172 | << "\n"; |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 173 | } |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 174 | } |
| 175 | for (const auto& [_, context] : nvmeDeviceMap) |
| 176 | { |
| 177 | context->pollNVMeDevices(); |
| 178 | } |
| 179 | } |
| 180 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 181 | void createSensors(boost::asio::io_service& io, |
| 182 | sdbusplus::asio::object_server& objectServer, |
| 183 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 184 | { |
| 185 | |
| 186 | auto getter = std::make_shared<GetSensorConfiguration>( |
Ed Tanous | 86d8301 | 2022-02-18 09:51:47 -0800 | [diff] [blame] | 187 | dbusConnection, [&io, &objectServer, &dbusConnection]( |
| 188 | const ManagedObjectType& sensorConfigurations) { |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 189 | handleSensorConfigurations(io, objectServer, dbusConnection, |
| 190 | sensorConfigurations); |
Ed Tanous | 86d8301 | 2022-02-18 09:51:47 -0800 | [diff] [blame] | 191 | }); |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 192 | getter->getConfiguration(std::vector<std::string>{NVMeSensor::configType}); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 195 | static void interfaceRemoved(sdbusplus::message_t& message, NVMEMap& contexts) |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 196 | { |
| 197 | if (message.is_method_error()) |
| 198 | { |
| 199 | std::cerr << "interfacesRemoved callback method error\n"; |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | sdbusplus::message::object_path path; |
| 204 | std::vector<std::string> interfaces; |
| 205 | |
| 206 | message.read(path, interfaces); |
| 207 | |
| 208 | for (auto& [_, context] : contexts) |
| 209 | { |
| 210 | std::optional<std::shared_ptr<NVMeSensor>> sensor = |
| 211 | context->getSensorAtPath(path); |
| 212 | if (!sensor) |
| 213 | { |
| 214 | continue; |
| 215 | } |
| 216 | |
| 217 | auto interface = std::find(interfaces.begin(), interfaces.end(), |
| 218 | (*sensor)->objectType); |
| 219 | if (interface == interfaces.end()) |
| 220 | { |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | context->removeSensor(sensor.value()); |
| 225 | } |
| 226 | } |
| 227 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 228 | int main() |
| 229 | { |
| 230 | boost::asio::io_service io; |
| 231 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 232 | systemBus->request_name("xyz.openbmc_project.NVMeSensor"); |
| 233 | sdbusplus::asio::object_server objectServer(systemBus); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 234 | |
| 235 | io.post([&]() { createSensors(io, objectServer, systemBus); }); |
| 236 | |
| 237 | boost::asio::deadline_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 238 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 239 | [&filterTimer, &io, &objectServer, &systemBus](sdbusplus::message_t&) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 240 | // this implicitly cancels the timer |
| 241 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 242 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 243 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 244 | if (ec == boost::asio::error::operation_aborted) |
| 245 | { |
| 246 | return; // we're being canceled |
| 247 | } |
Andrew Jeffery | 7279f93 | 2021-05-25 13:35:27 +0930 | [diff] [blame] | 248 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 249 | if (ec) |
| 250 | { |
| 251 | std::cerr << "Error: " << ec.message() << "\n"; |
| 252 | return; |
| 253 | } |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 254 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 255 | createSensors(io, objectServer, systemBus); |
| 256 | }); |
| 257 | }; |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 258 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 259 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 260 | setupPropertiesChangedMatches( |
| 261 | *systemBus, std::to_array<const char*>({NVMeSensor::configType}), |
| 262 | eventHandler); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 263 | |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 264 | // Watch for entity-manager to remove configuration interfaces |
| 265 | // so the corresponding sensors can be removed. |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 266 | auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 267 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 268 | "type='signal',member='InterfacesRemoved',arg0path='" + |
| 269 | std::string(inventoryPath) + "/'", |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 270 | [](sdbusplus::message_t& msg) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 271 | interfaceRemoved(msg, nvmeDeviceMap); |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 272 | }); |
| 273 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 274 | setupManufacturingModeMatch(*systemBus); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 275 | io.run(); |
| 276 | } |