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