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> |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 19 | #include <NVMeMCTPContext.hpp> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 20 | #include <NVMeSensor.hpp> |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 21 | #include <boost/asio/deadline_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> |
| 94 | provideRootBusContext(boost::asio::io_service& io, NVMEMap& map, |
| 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 = |
| 104 | #if HAVE_NVME_MI_MCTP |
| 105 | std::make_shared<NVMeMCTPContext>(io, rootBus); |
| 106 | #else |
| 107 | std::make_shared<NVMeBasicContext>(io, rootBus); |
| 108 | #endif |
| 109 | map[rootBus] = context; |
| 110 | |
| 111 | return context; |
| 112 | } |
| 113 | |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 114 | static void handleSensorConfigurations( |
| 115 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 116 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 117 | const ManagedObjectType& sensorConfigurations) |
| 118 | { |
| 119 | // todo: it'd be better to only update the ones we care about |
| 120 | for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap) |
| 121 | { |
| 122 | if (nvmeContextPtr) |
| 123 | { |
| 124 | nvmeContextPtr->close(); |
| 125 | } |
| 126 | } |
| 127 | nvmeDeviceMap.clear(); |
| 128 | |
| 129 | // iterate through all found configurations |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 130 | for (const auto& [interfacePath, sensorData] : sensorConfigurations) |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 131 | { |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 132 | // find base configuration |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 133 | auto sensorBase = sensorData.find(NVMeSensor::configType); |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame] | 134 | if (sensorBase != sensorData.end()) |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 135 | { |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [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); |
| 142 | |
| 143 | if (!(busNumber && sensorName && rootBus)) |
| 144 | { |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | std::vector<thresholds::Threshold> sensorThresholds; |
| 149 | if (!parseThresholdsFromConfig(sensorData, sensorThresholds)) |
| 150 | { |
| 151 | std::cerr << "error populating thresholds for " << *sensorName |
| 152 | << "\n"; |
| 153 | } |
| 154 | |
| 155 | std::shared_ptr<NVMeSensor> sensorPtr = |
| 156 | std::make_shared<NVMeSensor>( |
| 157 | objectServer, io, dbusConnection, *sensorName, |
| 158 | std::move(sensorThresholds), interfacePath, *busNumber); |
| 159 | |
Andrew Jeffery | 57574ad | 2021-12-02 16:35:52 +1030 | [diff] [blame] | 160 | provideRootBusContext(io, nvmeDeviceMap, *rootBus) |
| 161 | ->addSensor(sensorPtr); |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 162 | } |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 163 | } |
| 164 | for (const auto& [_, context] : nvmeDeviceMap) |
| 165 | { |
| 166 | context->pollNVMeDevices(); |
| 167 | } |
| 168 | } |
| 169 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 170 | void createSensors(boost::asio::io_service& io, |
| 171 | sdbusplus::asio::object_server& objectServer, |
| 172 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 173 | { |
| 174 | |
| 175 | auto getter = std::make_shared<GetSensorConfiguration>( |
Ed Tanous | 86d8301 | 2022-02-18 09:51:47 -0800 | [diff] [blame] | 176 | dbusConnection, [&io, &objectServer, &dbusConnection]( |
| 177 | const ManagedObjectType& sensorConfigurations) { |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 178 | handleSensorConfigurations(io, objectServer, dbusConnection, |
| 179 | sensorConfigurations); |
Ed Tanous | 86d8301 | 2022-02-18 09:51:47 -0800 | [diff] [blame] | 180 | }); |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 181 | getter->getConfiguration(std::vector<std::string>{NVMeSensor::configType}); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 184 | static void interfaceRemoved(sdbusplus::message::message& message, |
| 185 | NVMEMap& contexts) |
| 186 | { |
| 187 | if (message.is_method_error()) |
| 188 | { |
| 189 | std::cerr << "interfacesRemoved callback method error\n"; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | sdbusplus::message::object_path path; |
| 194 | std::vector<std::string> interfaces; |
| 195 | |
| 196 | message.read(path, interfaces); |
| 197 | |
| 198 | for (auto& [_, context] : contexts) |
| 199 | { |
| 200 | std::optional<std::shared_ptr<NVMeSensor>> sensor = |
| 201 | context->getSensorAtPath(path); |
| 202 | if (!sensor) |
| 203 | { |
| 204 | continue; |
| 205 | } |
| 206 | |
| 207 | auto interface = std::find(interfaces.begin(), interfaces.end(), |
| 208 | (*sensor)->objectType); |
| 209 | if (interface == interfaces.end()) |
| 210 | { |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | context->removeSensor(sensor.value()); |
| 215 | } |
| 216 | } |
| 217 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 218 | int main() |
| 219 | { |
| 220 | boost::asio::io_service io; |
| 221 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 222 | systemBus->request_name("xyz.openbmc_project.NVMeSensor"); |
| 223 | sdbusplus::asio::object_server objectServer(systemBus); |
Andrew Jeffery | 6f25e7e | 2021-05-24 12:52:53 +0930 | [diff] [blame] | 224 | #if HAVE_NVME_MI_MCTP |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 225 | nvmeMCTP::init(); |
Andrew Jeffery | 6f25e7e | 2021-05-24 12:52:53 +0930 | [diff] [blame] | 226 | #endif |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 227 | |
| 228 | io.post([&]() { createSensors(io, objectServer, systemBus); }); |
| 229 | |
| 230 | boost::asio::deadline_timer filterTimer(io); |
| 231 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 232 | [&filterTimer, &io, &objectServer, |
James Feist | 7d7579f | 2020-09-02 14:13:08 -0700 | [diff] [blame] | 233 | &systemBus](sdbusplus::message::message&) { |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 234 | // this implicitly cancels the timer |
| 235 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 236 | |
| 237 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 238 | if (ec == boost::asio::error::operation_aborted) |
| 239 | { |
| 240 | return; // we're being canceled |
| 241 | } |
Andrew Jeffery | 7279f93 | 2021-05-25 13:35:27 +0930 | [diff] [blame] | 242 | |
| 243 | if (ec) |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 244 | { |
| 245 | std::cerr << "Error: " << ec.message() << "\n"; |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | createSensors(io, objectServer, systemBus); |
| 250 | }); |
| 251 | }; |
| 252 | |
| 253 | sdbusplus::bus::match::match configMatch( |
| 254 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 255 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 256 | std::string(inventoryPath) + "',arg0namespace='" + |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 257 | std::string(NVMeSensor::configType) + "'", |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 258 | eventHandler); |
| 259 | |
Andrew Jeffery | afd55b9 | 2021-12-08 10:58:17 +1030 | [diff] [blame] | 260 | // Watch for entity-manager to remove configuration interfaces |
| 261 | // so the corresponding sensors can be removed. |
| 262 | auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 263 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 264 | "type='signal',member='InterfacesRemoved',arg0path='" + |
| 265 | std::string(inventoryPath) + "/'", |
| 266 | [](sdbusplus::message::message& msg) { |
| 267 | interfaceRemoved(msg, nvmeDeviceMap); |
| 268 | }); |
| 269 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 270 | setupManufacturingModeMatch(*systemBus); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 271 | io.run(); |
| 272 | } |