blob: bea79d862fdc491f2f3a8d2cc7d54abb6ff3a86f [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
James Feist58295ad2019-05-30 15:01:41 -070017#include "TachSensor.hpp"
18
19#include "Utils.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <unistd.h>
22
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/date_time/posix_time/posix_time.hpp>
James Feist7bc2bab2018-10-26 14:09:45 -070026#include <fstream>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080027#include <gpiod.hpp>
James Feist6714a252018-09-10 15:26:18 -070028#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <istream>
James Feist6714a252018-09-10 15:26:18 -070030#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <memory>
32#include <optional>
James Feist6714a252018-09-10 15:26:18 -070033#include <sdbusplus/asio/connection.hpp>
34#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070036#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <utility>
38#include <vector>
James Feist6714a252018-09-10 15:26:18 -070039
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070040static constexpr unsigned int pwmPollMs = 500;
41static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070042
James Feistd8705872019-02-08 13:26:09 -080043TachSensor::TachSensor(const std::string& path, const std::string& objectType,
44 sdbusplus::asio::object_server& objectServer,
45 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070046 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070047 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080048 boost::asio::io_service& io, const std::string& fanName,
49 std::vector<thresholds::Threshold>&& _thresholds,
50 const std::string& sensorConfiguration,
51 const std::pair<size_t, size_t>& limits) :
James Feist930fcde2019-05-28 12:58:43 -070052 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(_thresholds),
53 sensorConfiguration, objectType, limits.second, limits.first),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050054 objServer(objectServer), redundancy(redundancy),
55 presence(std::move(presenceSensor)),
56 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
57 errCount(0)
James Feist6714a252018-09-10 15:26:18 -070058{
James Feist251c7822018-09-12 12:54:15 -070059 sensorInterface = objectServer.add_interface(
60 "/xyz/openbmc_project/sensors/fan_tach/" + name,
61 "xyz.openbmc_project.Sensor.Value");
62
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070063 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070064 {
James Feist251c7822018-09-12 12:54:15 -070065 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070066 "/xyz/openbmc_project/sensors/fan_tach/" + name,
67 "xyz.openbmc_project.Sensor.Threshold.Warning");
68 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070069 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070070 {
James Feist251c7822018-09-12 12:54:15 -070071 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070072 "/xyz/openbmc_project/sensors/fan_tach/" + name,
73 "xyz.openbmc_project.Sensor.Threshold.Critical");
74 }
James Feist078f2322019-03-08 11:09:05 -080075 association = objectServer.add_interface(
76 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070077 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070078
79 if (presence)
80 {
81 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070082 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070083 "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 Feist2adc95c2019-09-30 14:55:28 -070088 itemAssoc = objectServer.add_interface(
89 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070090 itemAssoc->register_property(
91 "associations",
92 std::vector<Association>{
93 {"sensors", "inventory",
94 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
95 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070096 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080098 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700100}
101
102TachSensor::~TachSensor()
103{
104 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700105 inputDev.close();
106 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700107 objServer.remove_interface(thresholdInterfaceWarning);
108 objServer.remove_interface(thresholdInterfaceCritical);
109 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800110 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700111 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700112 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700113}
114
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700116{
117 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700118 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800119 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700120 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700121}
122
James Feistd8705872019-02-08 13:26:09 -0800123void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700124{
125 if (err == boost::system::errc::bad_file_descriptor)
126 {
127 return; // we're being destroyed
128 }
James Feist7bc2bab2018-10-26 14:09:45 -0700129 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700130 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700131 if (presence)
132 {
133 if (!presence->getValue())
134 {
James Feist1169eb42018-10-31 10:08:47 -0700135 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700136 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700137 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700138 }
James Feist60c0ec72019-03-18 15:08:43 -0700139 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700140 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700141 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700142 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700143 {
James Feist7bc2bab2018-10-26 14:09:45 -0700144 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700145 {
James Feist7bc2bab2018-10-26 14:09:45 -0700146 std::string response;
147 try
James Feist6714a252018-09-10 15:26:18 -0700148 {
James Feist7bc2bab2018-10-26 14:09:45 -0700149 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800150 double nvalue = std::stod(response);
James Feist7bc2bab2018-10-26 14:09:45 -0700151 responseStream.clear();
Josh Lehan833661a2020-03-04 17:35:41 -0800152 updateValue(nvalue);
James Feist7bc2bab2018-10-26 14:09:45 -0700153 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700154 }
James Feistd8705872019-02-08 13:26:09 -0800155 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700156 {
157 errCount++;
158 }
James Feist6714a252018-09-10 15:26:18 -0700159 }
160 else
161 {
James Feist71d31b22019-01-02 16:57:54 -0800162 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700163 {
James Feist71d31b22019-01-02 16:57:54 -0800164 errCount = 0;
165 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700166 }
167 else
168 {
James Feist71d31b22019-01-02 16:57:54 -0800169 pollTime = sensorFailedPollTimeMs;
170 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700171 }
James Feist6714a252018-09-10 15:26:18 -0700172 }
James Feist71d31b22019-01-02 16:57:54 -0800173 if (errCount >= warnAfterErrorCount)
174 {
175 // only print once
176 if (errCount == warnAfterErrorCount)
177 {
178 std::cerr << "Failure to read sensor " << name << " at " << path
179 << " ec:" << err << "\n";
180 }
181 updateValue(0);
182 }
James Feist6714a252018-09-10 15:26:18 -0700183 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700184 responseStream.clear();
185 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700186 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700187 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700188 {
189 return; // we're no longer valid
190 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700191 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700192 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800193 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700194 if (ec == boost::asio::error::operation_aborted)
195 {
196 return; // we're being canceled
197 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700198 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700199 });
200}
201
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700202void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700203{
James Feist95e54902019-09-04 15:13:36 -0700204 if (!isPowerOn())
205 {
206 return;
207 }
208
James Feistdc6c55f2018-10-31 12:53:20 -0700209 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700210
James Feist7b18b1e2019-05-14 13:42:09 -0700211 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700212 {
James Feist7b18b1e2019-05-14 13:42:09 -0700213 (*redundancy)
214 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700215 }
James Feist6714a252018-09-10 15:26:18 -0700216}
217
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800218PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700219 boost::asio::io_service& io,
220 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700221 inverted(inverted),
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800222 gpioLine(gpiod::find_line(gpioName)), gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700223{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800224 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700225 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800226 std::cerr << "Error requesting gpio: " << gpioName << "\n";
227 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700228 return;
229 }
230
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800231 try
James Feist7bc2bab2018-10-26 14:09:45 -0700232 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800233 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
234 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
235 status = gpioLine.get_value();
236
237 int gpioLineFd = gpioLine.event_get_fd();
238 if (gpioLineFd < 0)
239 {
240 std::cerr << "Failed to get " << gpioName << " fd\n";
241 return;
242 }
243
244 gpioFd.assign(gpioLineFd);
245 }
246 catch (std::system_error&)
247 {
248 std::cerr << "Error reading gpio: " << gpioName << "\n";
249 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700250 return;
251 }
James Feist7bc2bab2018-10-26 14:09:45 -0700252
James Feist7bc2bab2018-10-26 14:09:45 -0700253 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700254}
255
256PresenceSensor::~PresenceSensor()
257{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800258 gpioFd.close();
259 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700260}
261
262void PresenceSensor::monitorPresence(void)
263{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800264 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
265 [this](const boost::system::error_code& ec) {
266 if (ec == boost::system::errc::bad_file_descriptor)
267 {
268 return; // we're being destroyed
269 }
270 else if (ec)
271 {
272 std::cerr << "Error on presence sensor " << name
273 << " \n";
274 ;
275 }
276 else
277 {
278 read();
279 }
280 monitorPresence();
281 });
James Feist7bc2bab2018-10-26 14:09:45 -0700282}
283
284void PresenceSensor::read(void)
285{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800286 gpioLine.event_read();
287 status = gpioLine.get_value();
288 // Read is invoked when an edge event is detected by monitorPresence
289 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700290 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800291 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700292 }
293 else
294 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800295 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700296 }
297}
298
299bool PresenceSensor::getValue(void)
300{
301 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700302}
303
James Feist9bb67462019-03-15 15:09:50 -0700304RedundancySensor::RedundancySensor(size_t count,
305 const std::vector<std::string>& children,
306 sdbusplus::asio::object_server& objectServer,
307 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700308 count(count),
309 iface(objectServer.add_interface(
310 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700311 "xyz.openbmc_project.Control.FanRedundancy")),
312 association(objectServer.add_interface(
313 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700314 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700315 objectServer(objectServer)
316{
James Feist9bb67462019-03-15 15:09:50 -0700317 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700318 iface->register_property("Collection", children);
319 iface->register_property("Status", std::string("Full"));
320 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
321 iface->initialize();
322}
323RedundancySensor::~RedundancySensor()
324{
James Feist9bb67462019-03-15 15:09:50 -0700325 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700326 objectServer.remove_interface(iface);
327}
James Feistd8705872019-02-08 13:26:09 -0800328void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700329{
330 statuses[name] = failed;
331 size_t failedCount = 0;
332
James Feist7b18b1e2019-05-14 13:42:09 -0700333 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800334 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700335 {
336 if (status.second)
337 {
338 failedCount++;
339 }
340 if (failedCount > count)
341 {
James Feist7b18b1e2019-05-14 13:42:09 -0700342 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700343 break;
344 }
345 else if (failedCount)
346 {
James Feist7b18b1e2019-05-14 13:42:09 -0700347 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700348 }
349 }
James Feist7b18b1e2019-05-14 13:42:09 -0700350 if (state != newState)
351 {
352 if (state == redundancy::full)
353 {
354 logFanRedundancyLost();
355 }
356 else if (newState == redundancy::full)
357 {
358 logFanRedundancyRestored();
359 }
360 state = newState;
361 iface->set_property("Status", state);
362 }
James Feistdc6c55f2018-10-31 12:53:20 -0700363}