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 | |
| 26 | static constexpr const char* sensorType = |
| 27 | "xyz.openbmc_project.Configuration.NVME1000"; |
| 28 | |
| 29 | static NVMEMap nvmeDeviceMap; |
| 30 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 31 | static constexpr bool debug = false; |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 32 | |
| 33 | NVMEMap& getNVMEMap() |
| 34 | { |
| 35 | return nvmeDeviceMap; |
| 36 | } |
| 37 | |
Andrew Jeffery | 3412356 | 2021-12-02 14:38:41 +1030 | [diff] [blame] | 38 | static std::optional<int> |
| 39 | extractBusNumber(const std::string& path, |
| 40 | const SensorBaseConfigMap& properties) |
| 41 | { |
| 42 | auto findBus = properties.find("Bus"); |
| 43 | if (findBus == properties.end()) |
| 44 | { |
| 45 | std::cerr << "could not determine bus number for " << path << "\n"; |
| 46 | return std::nullopt; |
| 47 | } |
| 48 | |
| 49 | return std::visit(VariantToIntVisitor(), findBus->second); |
| 50 | } |
| 51 | |
Andrew Jeffery | e671f05 | 2021-12-02 14:47:20 +1030 | [diff] [blame] | 52 | static std::optional<std::string> |
| 53 | extractSensorName(const std::string& path, |
| 54 | const SensorBaseConfigMap& properties) |
| 55 | { |
| 56 | auto findSensorName = properties.find("Name"); |
| 57 | if (findSensorName == properties.end()) |
| 58 | { |
| 59 | std::cerr << "could not determine configuration name for " << path |
| 60 | << "\n"; |
| 61 | return std::nullopt; |
| 62 | } |
| 63 | |
| 64 | return std::get<std::string>(findSensorName->second); |
| 65 | } |
| 66 | |
Andrew Jeffery | 710562a | 2021-12-02 14:55:55 +1030 | [diff] [blame] | 67 | static std::filesystem::path deriveRootBusPath(int busNumber) |
| 68 | { |
| 69 | return "/sys/bus/i2c/devices/i2c-" + std::to_string(busNumber) + |
| 70 | "/mux_device"; |
| 71 | } |
| 72 | |
Andrew Jeffery | 66ab840 | 2021-12-02 15:03:51 +1030 | [diff] [blame] | 73 | static std::optional<int> deriveRootBus(std::optional<int> busNumber) |
| 74 | { |
| 75 | if (!busNumber) |
| 76 | { |
| 77 | return std::nullopt; |
| 78 | } |
| 79 | |
| 80 | std::filesystem::path muxPath = deriveRootBusPath(*busNumber); |
| 81 | |
| 82 | if (!std::filesystem::is_symlink(muxPath)) |
| 83 | { |
| 84 | return *busNumber; |
| 85 | } |
| 86 | |
| 87 | std::string rootName = std::filesystem::read_symlink(muxPath).filename(); |
| 88 | size_t dash = rootName.find('-'); |
| 89 | if (dash == std::string::npos) |
| 90 | { |
| 91 | std::cerr << "Error finding root bus for " << rootName << "\n"; |
| 92 | return std::nullopt; |
| 93 | } |
| 94 | |
| 95 | return std::stoi(rootName.substr(0, dash)); |
| 96 | } |
| 97 | |
Andrew Jeffery | c7a89e5 | 2021-12-02 15:10:23 +1030 | [diff] [blame] | 98 | static std::shared_ptr<NVMeContext> |
| 99 | provideRootBusContext(boost::asio::io_service& io, NVMEMap& map, |
| 100 | int rootBus) |
| 101 | { |
| 102 | auto findRoot = map.find(rootBus); |
| 103 | if (findRoot != map.end()) |
| 104 | { |
| 105 | return findRoot->second; |
| 106 | } |
| 107 | |
| 108 | std::shared_ptr<NVMeContext> context = |
| 109 | #if HAVE_NVME_MI_MCTP |
| 110 | std::make_shared<NVMeMCTPContext>(io, rootBus); |
| 111 | #else |
| 112 | std::make_shared<NVMeBasicContext>(io, rootBus); |
| 113 | #endif |
| 114 | map[rootBus] = context; |
| 115 | |
| 116 | return context; |
| 117 | } |
| 118 | |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 119 | static void handleSensorConfigurations( |
| 120 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 121 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 122 | const ManagedObjectType& sensorConfigurations) |
| 123 | { |
| 124 | // todo: it'd be better to only update the ones we care about |
| 125 | for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap) |
| 126 | { |
| 127 | if (nvmeContextPtr) |
| 128 | { |
| 129 | nvmeContextPtr->close(); |
| 130 | } |
| 131 | } |
| 132 | nvmeDeviceMap.clear(); |
| 133 | |
| 134 | // iterate through all found configurations |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame^] | 135 | for (const auto& [interfacePath, sensorData] : sensorConfigurations) |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 136 | { |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 137 | // find base configuration |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame^] | 138 | auto sensorBase = sensorData.find(sensorType); |
| 139 | if (sensorBase != sensorData.end()) |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 140 | { |
Andrew Jeffery | ee16434 | 2021-12-02 16:02:19 +1030 | [diff] [blame^] | 141 | const SensorBaseConfigMap& sensorConfig = sensorBase->second; |
| 142 | std::optional<int> busNumber = |
| 143 | extractBusNumber(interfacePath, sensorConfig); |
| 144 | std::optional<std::string> sensorName = |
| 145 | extractSensorName(interfacePath, sensorConfig); |
| 146 | std::optional<int> rootBus = deriveRootBus(busNumber); |
| 147 | |
| 148 | if (!(busNumber && sensorName && rootBus)) |
| 149 | { |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | std::vector<thresholds::Threshold> sensorThresholds; |
| 154 | if (!parseThresholdsFromConfig(sensorData, sensorThresholds)) |
| 155 | { |
| 156 | std::cerr << "error populating thresholds for " << *sensorName |
| 157 | << "\n"; |
| 158 | } |
| 159 | |
| 160 | std::shared_ptr<NVMeSensor> sensorPtr = |
| 161 | std::make_shared<NVMeSensor>( |
| 162 | objectServer, io, dbusConnection, *sensorName, |
| 163 | std::move(sensorThresholds), interfacePath, *busNumber); |
| 164 | |
| 165 | std::shared_ptr<NVMeContext> context = |
| 166 | provideRootBusContext(io, nvmeDeviceMap, *rootBus); |
| 167 | context->addSensor(sensorPtr); |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 168 | } |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 169 | } |
| 170 | for (const auto& [_, context] : nvmeDeviceMap) |
| 171 | { |
| 172 | context->pollNVMeDevices(); |
| 173 | } |
| 174 | } |
| 175 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 176 | void createSensors(boost::asio::io_service& io, |
| 177 | sdbusplus::asio::object_server& objectServer, |
| 178 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 179 | { |
| 180 | |
| 181 | auto getter = std::make_shared<GetSensorConfiguration>( |
| 182 | dbusConnection, |
| 183 | std::move([&io, &objectServer, &dbusConnection]( |
| 184 | const ManagedObjectType& sensorConfigurations) { |
Andrew Jeffery | f690f0c | 2021-12-02 11:42:58 +1030 | [diff] [blame] | 185 | handleSensorConfigurations(io, objectServer, dbusConnection, |
| 186 | sensorConfigurations); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 187 | })); |
| 188 | getter->getConfiguration(std::vector<std::string>{sensorType}); |
| 189 | } |
| 190 | |
| 191 | int main() |
| 192 | { |
| 193 | boost::asio::io_service io; |
| 194 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 195 | systemBus->request_name("xyz.openbmc_project.NVMeSensor"); |
| 196 | sdbusplus::asio::object_server objectServer(systemBus); |
Andrew Jeffery | 6f25e7e | 2021-05-24 12:52:53 +0930 | [diff] [blame] | 197 | #if HAVE_NVME_MI_MCTP |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 198 | nvmeMCTP::init(); |
Andrew Jeffery | 6f25e7e | 2021-05-24 12:52:53 +0930 | [diff] [blame] | 199 | #endif |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 200 | |
| 201 | io.post([&]() { createSensors(io, objectServer, systemBus); }); |
| 202 | |
| 203 | boost::asio::deadline_timer filterTimer(io); |
| 204 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 205 | [&filterTimer, &io, &objectServer, |
James Feist | 7d7579f | 2020-09-02 14:13:08 -0700 | [diff] [blame] | 206 | &systemBus](sdbusplus::message::message&) { |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 207 | // this implicitly cancels the timer |
| 208 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 209 | |
| 210 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 211 | if (ec == boost::asio::error::operation_aborted) |
| 212 | { |
| 213 | return; // we're being canceled |
| 214 | } |
Andrew Jeffery | 7279f93 | 2021-05-25 13:35:27 +0930 | [diff] [blame] | 215 | |
| 216 | if (ec) |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 217 | { |
| 218 | std::cerr << "Error: " << ec.message() << "\n"; |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | createSensors(io, objectServer, systemBus); |
| 223 | }); |
| 224 | }; |
| 225 | |
| 226 | sdbusplus::bus::match::match configMatch( |
| 227 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 228 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 229 | std::string(inventoryPath) + "',arg0namespace='" + |
| 230 | std::string(sensorType) + "'", |
| 231 | eventHandler); |
| 232 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 233 | setupManufacturingModeMatch(*systemBus); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 234 | io.run(); |
| 235 | } |