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