James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 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 | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "TachSensor.hpp" |
| 18 | |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 19 | #include "PresenceGpio.hpp" |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 20 | #include "SensorPaths.hpp" |
| 21 | #include "Thresholds.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 22 | #include "Utils.hpp" |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 23 | #include "sensor.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 24 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 25 | #include <boost/asio/buffer.hpp> |
| 26 | #include <boost/asio/error.hpp> |
| 27 | #include <boost/asio/io_context.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 28 | #include <boost/asio/random_access_file.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 29 | #include <sdbusplus/asio/connection.hpp> |
| 30 | #include <sdbusplus/asio/object_server.hpp> |
| 31 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 32 | #include <charconv> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 33 | #include <chrono> |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 36 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 37 | #include <memory> |
| 38 | #include <optional> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 39 | #include <string> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 40 | #include <system_error> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 41 | #include <utility> |
| 42 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 43 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 44 | static constexpr unsigned int pwmPollMs = 500; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 45 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 46 | TachSensor::TachSensor( |
| 47 | const std::string& path, const std::string& objectType, |
| 48 | sdbusplus::asio::object_server& objectServer, |
| 49 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 50 | std::shared_ptr<PresenceGpio>& presenceGpio, |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 51 | std::optional<RedundancySensor>* redundancy, boost::asio::io_context& io, |
| 52 | const std::string& fanName, |
| 53 | std::vector<thresholds::Threshold>&& thresholdsIn, |
| 54 | const std::string& sensorConfiguration, |
| 55 | const std::pair<double, double>& limits, const PowerState& powerState, |
| 56 | const std::optional<std::string>& ledIn) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 57 | Sensor(escapeName(fanName), std::move(thresholdsIn), sensorConfiguration, |
| 58 | objectType, false, false, limits.second, limits.first, conn, |
| 59 | powerState), |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 60 | objServer(objectServer), redundancy(redundancy), presence(presenceGpio), |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 61 | inputDev(io, path, boost::asio::random_access_file::read_only), |
| 62 | waitTimer(io), path(path), led(ledIn) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 64 | sensorInterface = objectServer.add_interface( |
| 65 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 66 | "xyz.openbmc_project.Sensor.Value"); |
| 67 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 68 | for (const auto& threshold : thresholds) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 69 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 70 | std::string interface = thresholds::getInterface(threshold.level); |
| 71 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 72 | objectServer.add_interface( |
| 73 | "/xyz/openbmc_project/sensors/fan_tach/" + name, interface); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 74 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 75 | association = objectServer.add_interface( |
| 76 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 77 | association::interface); |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 78 | |
| 79 | if (presence) |
| 80 | { |
| 81 | itemIface = |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 82 | objectServer.add_interface("/xyz/openbmc_project/inventory/" + name, |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 83 | "xyz.openbmc_project.Inventory.Item"); |
| 84 | itemIface->register_property("PrettyName", |
| 85 | std::string()); // unused property |
| 86 | itemIface->register_property("Present", true); |
| 87 | itemIface->initialize(); |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 88 | itemAssoc = objectServer.add_interface( |
| 89 | "/xyz/openbmc_project/inventory/" + name, association::interface); |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 90 | itemAssoc->register_property( |
Glukhov Mikhail | 6e6561d | 2024-01-10 11:58:48 +0300 | [diff] [blame] | 91 | "Associations", |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 92 | std::vector<Association>{ |
| 93 | {"sensors", "inventory", |
| 94 | "/xyz/openbmc_project/sensors/fan_tach/" + name}}); |
| 95 | itemAssoc->initialize(); |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 96 | } |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 97 | setInitialProperties(sensor_paths::unitRPMs); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | TachSensor::~TachSensor() |
| 101 | { |
| 102 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 103 | inputDev.close(); |
| 104 | waitTimer.cancel(); |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 105 | for (const auto& iface : thresholdInterfaces) |
| 106 | { |
| 107 | objServer.remove_interface(iface); |
| 108 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 109 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 110 | objServer.remove_interface(association); |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 111 | objServer.remove_interface(itemIface); |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 112 | objServer.remove_interface(itemAssoc); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 115 | void TachSensor::setupRead() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 116 | { |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 117 | std::weak_ptr<TachSensor> weakRef = weak_from_this(); |
| 118 | inputDev.async_read_some_at( |
| 119 | 0, boost::asio::buffer(readBuf), |
| 120 | [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 121 | std::shared_ptr<TachSensor> self = weakRef.lock(); |
| 122 | if (self) |
| 123 | { |
| 124 | self->handleResponse(ec, bytesRead); |
| 125 | } |
| 126 | }); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void TachSensor::restartRead(size_t pollTime) |
| 130 | { |
| 131 | std::weak_ptr<TachSensor> weakRef = weak_from_this(); |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 132 | waitTimer.expires_after(std::chrono::milliseconds(pollTime)); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 133 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 134 | if (ec == boost::asio::error::operation_aborted) |
| 135 | { |
| 136 | return; // we're being canceled |
| 137 | } |
| 138 | std::shared_ptr<TachSensor> self = weakRef.lock(); |
| 139 | if (!self) |
| 140 | { |
| 141 | return; |
| 142 | } |
| 143 | self->setupRead(); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 144 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 147 | void TachSensor::handleResponse(const boost::system::error_code& err, |
| 148 | size_t bytesRead) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 149 | { |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 150 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 151 | (err == boost::asio::error::misc_errors::not_found)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 152 | { |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 153 | std::cerr << "TachSensor " << name << " removed " << path << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 154 | return; // we're being destroyed |
| 155 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 156 | bool missing = false; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 157 | size_t pollTime = pwmPollMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 158 | if (presence) |
| 159 | { |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 160 | if (!presence->isPresent()) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 161 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 162 | markAvailable(false); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 163 | missing = true; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 164 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 165 | } |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 166 | itemIface->set_property("Present", !missing); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 167 | } |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 168 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 169 | if (!missing) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 170 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 171 | if (!err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 172 | { |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 173 | const char* bufEnd = readBuf.data() + bytesRead; |
| 174 | int nvalue = 0; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 175 | std::from_chars_result ret = |
| 176 | std::from_chars(readBuf.data(), bufEnd, nvalue); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 177 | if (ret.ec != std::errc()) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 178 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 179 | incrementError(); |
| 180 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 181 | } |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 182 | else |
| 183 | { |
| 184 | updateValue(nvalue); |
| 185 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 186 | } |
| 187 | else |
| 188 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 189 | incrementError(); |
| 190 | pollTime = sensorFailedPollTimeMs; |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 191 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 192 | } |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 193 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 194 | restartRead(pollTime); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 197 | void TachSensor::checkThresholds() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 198 | { |
Zhikui Ren | 00bd56d | 2021-11-19 12:32:27 -0800 | [diff] [blame] | 199 | bool status = thresholds::checkThresholds(this); |
James Feist | 95e5490 | 2019-09-04 15:13:36 -0700 | [diff] [blame] | 200 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 201 | if ((redundancy != nullptr) && *redundancy) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 202 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 203 | (*redundancy) |
| 204 | ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 205 | } |
James Feist | 49a8ccd | 2020-09-16 16:09:52 -0700 | [diff] [blame] | 206 | |
| 207 | bool curLed = !status; |
| 208 | if (led && ledState != curLed) |
| 209 | { |
| 210 | ledState = curLed; |
| 211 | setLed(dbusConnection, *led, curLed); |
| 212 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 213 | } |
| 214 | |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 215 | RedundancySensor::RedundancySensor(size_t count, |
| 216 | const std::vector<std::string>& children, |
| 217 | sdbusplus::asio::object_server& objectServer, |
| 218 | const std::string& sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 219 | count(count), |
| 220 | iface(objectServer.add_interface( |
| 221 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 222 | "xyz.openbmc_project.Control.FanRedundancy")), |
| 223 | association(objectServer.add_interface( |
| 224 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 225 | association::interface)), |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 226 | objectServer(objectServer) |
| 227 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 228 | createAssociation(association, sensorConfiguration); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 229 | iface->register_property("Collection", children); |
| 230 | iface->register_property("Status", std::string("Full")); |
| 231 | iface->register_property("AllowedFailures", static_cast<uint8_t>(count)); |
| 232 | iface->initialize(); |
| 233 | } |
| 234 | RedundancySensor::~RedundancySensor() |
| 235 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 236 | objectServer.remove_interface(association); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 237 | objectServer.remove_interface(iface); |
| 238 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 239 | void RedundancySensor::update(const std::string& name, bool failed) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 240 | { |
| 241 | statuses[name] = failed; |
| 242 | size_t failedCount = 0; |
| 243 | |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 244 | std::string newState = redundancy::full; |
Zev Weiss | 0521a06 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 245 | for (const auto& [name, status] : statuses) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 246 | { |
Zev Weiss | 0521a06 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 247 | if (status) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 248 | { |
| 249 | failedCount++; |
| 250 | } |
| 251 | if (failedCount > count) |
| 252 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 253 | newState = redundancy::failed; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 254 | break; |
| 255 | } |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 256 | if (failedCount != 0U) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 257 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 258 | newState = redundancy::degraded; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 261 | if (state != newState) |
| 262 | { |
| 263 | if (state == redundancy::full) |
| 264 | { |
| 265 | logFanRedundancyLost(); |
| 266 | } |
| 267 | else if (newState == redundancy::full) |
| 268 | { |
| 269 | logFanRedundancyRestored(); |
| 270 | } |
| 271 | state = newState; |
| 272 | iface->set_property("Status", state); |
| 273 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 274 | } |