blob: b31c1b7393c478944d5955239b76877ac5a5941a [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
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 Jefferye73bd0a2023-01-25 10:39:57 +103017#include "TachSensor.hpp"
18
Ed Tanouseacbfdd2024-04-04 12:00:24 -070019#include "SensorPaths.hpp"
20#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103021#include "Utils.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070022#include "sensor.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103023
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include <boost/asio/buffer.hpp>
25#include <boost/asio/error.hpp>
26#include <boost/asio/io_context.hpp>
27#include <boost/asio/posix/stream_descriptor.hpp>
28#include <boost/asio/random_access_file.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080029#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
Josh Lehan5170fe62022-08-03 13:17:41 -070033#include <charconv>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070034#include <chrono>
35#include <cstddef>
36#include <cstdint>
James Feist6714a252018-09-10 15:26:18 -070037#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070038#include <memory>
39#include <optional>
James Feist6714a252018-09-10 15:26:18 -070040#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070041#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070042#include <utility>
43#include <vector>
James Feist6714a252018-09-10 15:26:18 -070044
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070045static constexpr unsigned int pwmPollMs = 500;
James Feist6714a252018-09-10 15:26:18 -070046
James Feistd8705872019-02-08 13:26:09 -080047TachSensor::TachSensor(const std::string& path, const std::string& objectType,
48 sdbusplus::asio::object_server& objectServer,
49 std::shared_ptr<sdbusplus::asio::connection>& conn,
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +020050 std::shared_ptr<PresenceSensor>& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070051 std::optional<RedundancySensor>* redundancy,
Ed Tanous1f978632023-02-28 18:16:39 -080052 boost::asio::io_context& io, const std::string& fanName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080053 std::vector<thresholds::Threshold>&& thresholdsIn,
James Feistd8705872019-02-08 13:26:09 -080054 const std::string& sensorConfiguration,
Ed Tanousf69fbf92022-01-14 10:15:33 -080055 const std::pair<double, double>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070056 const PowerState& powerState,
57 const std::optional<std::string>& ledIn) :
Zhikui Renda98f092021-11-01 09:41:08 -070058 Sensor(escapeName(fanName), std::move(thresholdsIn), sensorConfiguration,
59 objectType, false, false, limits.second, limits.first, conn,
60 powerState),
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +020061 objServer(objectServer), redundancy(redundancy), presence(presenceSensor),
Josh Lehan5170fe62022-08-03 13:17:41 -070062 inputDev(io, path, boost::asio::random_access_file::read_only),
63 waitTimer(io), path(path), led(ledIn)
James Feist6714a252018-09-10 15:26:18 -070064{
James Feist251c7822018-09-12 12:54:15 -070065 sensorInterface = objectServer.add_interface(
66 "/xyz/openbmc_project/sensors/fan_tach/" + name,
67 "xyz.openbmc_project.Sensor.Value");
68
Jayashree Dhanapal56678082022-01-04 17:27:20 +053069 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070070 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053071 std::string interface = thresholds::getInterface(threshold.level);
72 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
73 objectServer.add_interface(
74 "/xyz/openbmc_project/sensors/fan_tach/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070075 }
James Feist078f2322019-03-08 11:09:05 -080076 association = objectServer.add_interface(
77 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070078 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070079
80 if (presence)
81 {
82 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070083 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070084 "xyz.openbmc_project.Inventory.Item");
85 itemIface->register_property("PrettyName",
86 std::string()); // unused property
87 itemIface->register_property("Present", true);
88 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070089 itemAssoc = objectServer.add_interface(
90 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070091 itemAssoc->register_property(
Glukhov Mikhail6e6561d2024-01-10 11:58:48 +030092 "Associations",
James Feistd8bd5622019-06-26 12:09:05 -070093 std::vector<Association>{
94 {"sensors", "inventory",
95 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
96 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070097 }
Andrei Kartashev39287412022-02-04 16:04:47 +030098 setInitialProperties(sensor_paths::unitRPMs);
James Feist6714a252018-09-10 15:26:18 -070099}
100
101TachSensor::~TachSensor()
102{
103 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700104 inputDev.close();
105 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530106 for (const auto& iface : thresholdInterfaces)
107 {
108 objServer.remove_interface(iface);
109 }
James Feist251c7822018-09-12 12:54:15 -0700110 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800111 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700112 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700113 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700114}
115
Josh Lehan5170fe62022-08-03 13:17:41 -0700116void TachSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700117{
Josh Lehan5170fe62022-08-03 13:17:41 -0700118 std::weak_ptr<TachSensor> weakRef = weak_from_this();
119 inputDev.async_read_some_at(
120 0, boost::asio::buffer(readBuf),
121 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
122 std::shared_ptr<TachSensor> self = weakRef.lock();
123 if (self)
124 {
125 self->handleResponse(ec, bytesRead);
126 }
Patrick Williams597e8422023-10-20 11:19:01 -0500127 });
Josh Lehan5170fe62022-08-03 13:17:41 -0700128}
129
130void TachSensor::restartRead(size_t pollTime)
131{
132 std::weak_ptr<TachSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800133 waitTimer.expires_after(std::chrono::milliseconds(pollTime));
Josh Lehan5170fe62022-08-03 13:17:41 -0700134 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
135 if (ec == boost::asio::error::operation_aborted)
136 {
137 return; // we're being canceled
138 }
139 std::shared_ptr<TachSensor> self = weakRef.lock();
140 if (!self)
141 {
142 return;
143 }
144 self->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700145 });
James Feist6714a252018-09-10 15:26:18 -0700146}
147
Josh Lehan5170fe62022-08-03 13:17:41 -0700148void TachSensor::handleResponse(const boost::system::error_code& err,
149 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700150{
Josh Lehan5170fe62022-08-03 13:17:41 -0700151 if ((err == boost::system::errc::bad_file_descriptor) ||
152 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700153 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700154 std::cerr << "TachSensor " << name << " removed " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700155 return; // we're being destroyed
156 }
James Feist7bc2bab2018-10-26 14:09:45 -0700157 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700158 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700159 if (presence)
160 {
161 if (!presence->getValue())
162 {
James Feist961bf092020-07-01 16:38:12 -0700163 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700164 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700165 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700166 }
James Feist60c0ec72019-03-18 15:08:43 -0700167 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700168 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700169
James Feist7bc2bab2018-10-26 14:09:45 -0700170 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700171 {
James Feist7bc2bab2018-10-26 14:09:45 -0700172 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700173 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700174 const char* bufEnd = readBuf.data() + bytesRead;
175 int nvalue = 0;
Patrick Williams779c96a2023-05-10 07:50:42 -0500176 std::from_chars_result ret = std::from_chars(readBuf.data(), bufEnd,
177 nvalue);
Josh Lehan5170fe62022-08-03 13:17:41 -0700178 if (ret.ec != std::errc())
James Feist7bc2bab2018-10-26 14:09:45 -0700179 {
James Feist961bf092020-07-01 16:38:12 -0700180 incrementError();
181 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700182 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700183 else
184 {
185 updateValue(nvalue);
186 }
James Feist6714a252018-09-10 15:26:18 -0700187 }
188 else
189 {
James Feist961bf092020-07-01 16:38:12 -0700190 incrementError();
191 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800192 }
James Feist6714a252018-09-10 15:26:18 -0700193 }
Ed Tanous99c44092022-01-14 09:59:09 -0800194
Josh Lehan5170fe62022-08-03 13:17:41 -0700195 restartRead(pollTime);
James Feist6714a252018-09-10 15:26:18 -0700196}
197
Ed Tanous201a1012024-04-03 18:07:28 -0700198void TachSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700199{
Zhikui Ren00bd56d2021-11-19 12:32:27 -0800200 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700201
Ed Tanous2049bd22022-07-09 07:20:26 -0700202 if ((redundancy != nullptr) && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700203 {
James Feist7b18b1e2019-05-14 13:42:09 -0700204 (*redundancy)
205 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700206 }
James Feist49a8ccd2020-09-16 16:09:52 -0700207
208 bool curLed = !status;
209 if (led && ledState != curLed)
210 {
211 ledState = curLed;
212 setLed(dbusConnection, *led, curLed);
213 }
James Feist6714a252018-09-10 15:26:18 -0700214}
215
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800216PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
Ed Tanous1f978632023-02-28 18:16:39 -0800217 boost::asio::io_context& io,
James Feist7b18b1e2019-05-14 13:42:09 -0700218 const std::string& name) :
Ed Tanous8a17c302021-09-02 15:07:11 -0700219 gpioLine(gpiod::find_line(gpioName)),
220 gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700221{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800222 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700223 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800224 std::cerr << "Error requesting gpio: " << gpioName << "\n";
225 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700226 return;
227 }
228
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800229 try
James Feist7bc2bab2018-10-26 14:09:45 -0700230 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800231 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
232 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
Ed Tanous2049bd22022-07-09 07:20:26 -0700233 status = (gpioLine.get_value() != 0);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800234
235 int gpioLineFd = gpioLine.event_get_fd();
236 if (gpioLineFd < 0)
237 {
238 std::cerr << "Failed to get " << gpioName << " fd\n";
239 return;
240 }
241
242 gpioFd.assign(gpioLineFd);
243 }
Patrick Williams26601e82021-10-06 12:43:25 -0500244 catch (const std::system_error&)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800245 {
246 std::cerr << "Error reading gpio: " << gpioName << "\n";
247 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700248 return;
249 }
James Feist7bc2bab2018-10-26 14:09:45 -0700250
James Feist7bc2bab2018-10-26 14:09:45 -0700251 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700252}
253
254PresenceSensor::~PresenceSensor()
255{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800256 gpioFd.close();
257 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700258}
259
Ed Tanous201a1012024-04-03 18:07:28 -0700260void PresenceSensor::monitorPresence()
James Feist7bc2bab2018-10-26 14:09:45 -0700261{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800262 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
263 [this](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700264 if (ec == boost::system::errc::bad_file_descriptor)
265 {
266 return; // we're being destroyed
267 }
268 if (ec)
269 {
270 std::cerr << "Error on presence sensor " << name << " \n";
271 ;
272 }
273 else
274 {
275 read();
276 }
277 monitorPresence();
278 });
James Feist7bc2bab2018-10-26 14:09:45 -0700279}
280
Ed Tanous201a1012024-04-03 18:07:28 -0700281void PresenceSensor::read()
James Feist7bc2bab2018-10-26 14:09:45 -0700282{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800283 gpioLine.event_read();
Ed Tanous2049bd22022-07-09 07:20:26 -0700284 status = (gpioLine.get_value() != 0);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800285 // Read is invoked when an edge event is detected by monitorPresence
286 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700287 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800288 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700289 }
290 else
291 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800292 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700293 }
294}
295
Ed Tanous201a1012024-04-03 18:07:28 -0700296bool PresenceSensor::getValue() const
James Feist7bc2bab2018-10-26 14:09:45 -0700297{
298 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700299}
300
James Feist9bb67462019-03-15 15:09:50 -0700301RedundancySensor::RedundancySensor(size_t count,
302 const std::vector<std::string>& children,
303 sdbusplus::asio::object_server& objectServer,
304 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700305 count(count),
306 iface(objectServer.add_interface(
307 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700308 "xyz.openbmc_project.Control.FanRedundancy")),
309 association(objectServer.add_interface(
310 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700311 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700312 objectServer(objectServer)
313{
James Feist9bb67462019-03-15 15:09:50 -0700314 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700315 iface->register_property("Collection", children);
316 iface->register_property("Status", std::string("Full"));
317 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
318 iface->initialize();
319}
320RedundancySensor::~RedundancySensor()
321{
James Feist9bb67462019-03-15 15:09:50 -0700322 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700323 objectServer.remove_interface(iface);
324}
James Feistd8705872019-02-08 13:26:09 -0800325void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700326{
327 statuses[name] = failed;
328 size_t failedCount = 0;
329
James Feist7b18b1e2019-05-14 13:42:09 -0700330 std::string newState = redundancy::full;
Zev Weiss0521a062022-08-12 18:21:02 -0700331 for (const auto& [name, status] : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700332 {
Zev Weiss0521a062022-08-12 18:21:02 -0700333 if (status)
James Feistdc6c55f2018-10-31 12:53:20 -0700334 {
335 failedCount++;
336 }
337 if (failedCount > count)
338 {
James Feist7b18b1e2019-05-14 13:42:09 -0700339 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700340 break;
341 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700342 if (failedCount != 0U)
James Feistdc6c55f2018-10-31 12:53:20 -0700343 {
James Feist7b18b1e2019-05-14 13:42:09 -0700344 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700345 }
346 }
James Feist7b18b1e2019-05-14 13:42:09 -0700347 if (state != newState)
348 {
349 if (state == redundancy::full)
350 {
351 logFanRedundancyLost();
352 }
353 else if (newState == redundancy::full)
354 {
355 logFanRedundancyRestored();
356 }
357 state = newState;
358 iface->set_property("Status", state);
359 }
James Feistdc6c55f2018-10-31 12:53:20 -0700360}