blob: cb3d2a870f5e65f1dcf87a83862bd84a01dbb3d4 [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>
James Feist8086aba2020-08-25 16:00:59 -070025#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070026#include <boost/date_time/posix_time/posix_time.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080027#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30
31#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070032#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <istream>
James Feist6714a252018-09-10 15:26:18 -070034#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <memory>
36#include <optional>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070038#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <utility>
40#include <vector>
James Feist6714a252018-09-10 15:26:18 -070041
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070042static constexpr unsigned int pwmPollMs = 500;
43static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070044
James Feistd8705872019-02-08 13:26:09 -080045TachSensor::TachSensor(const std::string& path, const std::string& objectType,
46 sdbusplus::asio::object_server& objectServer,
47 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070048 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070049 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080050 boost::asio::io_service& io, const std::string& fanName,
51 std::vector<thresholds::Threshold>&& _thresholds,
52 const std::string& sensorConfiguration,
53 const std::pair<size_t, size_t>& limits) :
James Feist930fcde2019-05-28 12:58:43 -070054 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(_thresholds),
James Feist961bf092020-07-01 16:38:12 -070055 sensorConfiguration, objectType, limits.second, limits.first,
56 PowerState::on),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050057 objServer(objectServer), redundancy(redundancy),
58 presence(std::move(presenceSensor)),
James Feist961bf092020-07-01 16:38:12 -070059 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path)
James Feist6714a252018-09-10 15:26:18 -070060{
James Feist251c7822018-09-12 12:54:15 -070061 sensorInterface = objectServer.add_interface(
62 "/xyz/openbmc_project/sensors/fan_tach/" + name,
63 "xyz.openbmc_project.Sensor.Value");
64
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070065 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070066 {
James Feist251c7822018-09-12 12:54:15 -070067 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070068 "/xyz/openbmc_project/sensors/fan_tach/" + name,
69 "xyz.openbmc_project.Sensor.Threshold.Warning");
70 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070072 {
James Feist251c7822018-09-12 12:54:15 -070073 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070074 "/xyz/openbmc_project/sensors/fan_tach/" + name,
75 "xyz.openbmc_project.Sensor.Threshold.Critical");
76 }
James Feist078f2322019-03-08 11:09:05 -080077 association = objectServer.add_interface(
78 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070079 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070080
81 if (presence)
82 {
83 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070084 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070085 "xyz.openbmc_project.Inventory.Item");
86 itemIface->register_property("PrettyName",
87 std::string()); // unused property
88 itemIface->register_property("Present", true);
89 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070090 itemAssoc = objectServer.add_interface(
91 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070092 itemAssoc->register_property(
93 "associations",
94 std::vector<Association>{
95 {"sensors", "inventory",
96 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
97 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070098 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099 setInitialProperties(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700101}
102
103TachSensor::~TachSensor()
104{
105 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700106 inputDev.close();
107 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700108 objServer.remove_interface(thresholdInterfaceWarning);
109 objServer.remove_interface(thresholdInterfaceCritical);
110 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
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700116void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700117{
118 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800120 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700121 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700122}
123
James Feistd8705872019-02-08 13:26:09 -0800124void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700125{
126 if (err == boost::system::errc::bad_file_descriptor)
127 {
128 return; // we're being destroyed
129 }
James Feist7bc2bab2018-10-26 14:09:45 -0700130 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700131 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700132 if (presence)
133 {
134 if (!presence->getValue())
135 {
James Feist961bf092020-07-01 16:38:12 -0700136 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700137 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700138 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700139 }
James Feist60c0ec72019-03-18 15:08:43 -0700140 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700141 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700142 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700143 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700144 {
James Feist7bc2bab2018-10-26 14:09:45 -0700145 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700146 {
James Feist7bc2bab2018-10-26 14:09:45 -0700147 std::string response;
148 try
James Feist6714a252018-09-10 15:26:18 -0700149 {
James Feist7bc2bab2018-10-26 14:09:45 -0700150 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800151 double nvalue = std::stod(response);
James Feist7bc2bab2018-10-26 14:09:45 -0700152 responseStream.clear();
Josh Lehan833661a2020-03-04 17:35:41 -0800153 updateValue(nvalue);
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 {
James Feist961bf092020-07-01 16:38:12 -0700157 incrementError();
158 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700159 }
James Feist6714a252018-09-10 15:26:18 -0700160 }
161 else
162 {
James Feist961bf092020-07-01 16:38:12 -0700163 incrementError();
164 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800165 }
James Feist6714a252018-09-10 15:26:18 -0700166 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167 responseStream.clear();
168 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700169 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700170 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700171 {
172 return; // we're no longer valid
173 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700174 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700175 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800176 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700177 if (ec == boost::asio::error::operation_aborted)
178 {
179 return; // we're being canceled
180 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700181 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700182 });
183}
184
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700185void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700186{
James Feistdc6c55f2018-10-31 12:53:20 -0700187 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700188
James Feist7b18b1e2019-05-14 13:42:09 -0700189 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700190 {
James Feist7b18b1e2019-05-14 13:42:09 -0700191 (*redundancy)
192 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700193 }
James Feist6714a252018-09-10 15:26:18 -0700194}
195
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800196PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700197 boost::asio::io_service& io,
198 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700199 inverted(inverted),
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800200 gpioLine(gpiod::find_line(gpioName)), gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700201{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800202 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700203 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800204 std::cerr << "Error requesting gpio: " << gpioName << "\n";
205 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700206 return;
207 }
208
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800209 try
James Feist7bc2bab2018-10-26 14:09:45 -0700210 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800211 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
212 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
213 status = gpioLine.get_value();
214
215 int gpioLineFd = gpioLine.event_get_fd();
216 if (gpioLineFd < 0)
217 {
218 std::cerr << "Failed to get " << gpioName << " fd\n";
219 return;
220 }
221
222 gpioFd.assign(gpioLineFd);
223 }
224 catch (std::system_error&)
225 {
226 std::cerr << "Error reading gpio: " << gpioName << "\n";
227 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700228 return;
229 }
James Feist7bc2bab2018-10-26 14:09:45 -0700230
James Feist7bc2bab2018-10-26 14:09:45 -0700231 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700232}
233
234PresenceSensor::~PresenceSensor()
235{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800236 gpioFd.close();
237 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700238}
239
240void PresenceSensor::monitorPresence(void)
241{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800242 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
243 [this](const boost::system::error_code& ec) {
244 if (ec == boost::system::errc::bad_file_descriptor)
245 {
246 return; // we're being destroyed
247 }
248 else if (ec)
249 {
250 std::cerr << "Error on presence sensor " << name
251 << " \n";
252 ;
253 }
254 else
255 {
256 read();
257 }
258 monitorPresence();
259 });
James Feist7bc2bab2018-10-26 14:09:45 -0700260}
261
262void PresenceSensor::read(void)
263{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800264 gpioLine.event_read();
265 status = gpioLine.get_value();
266 // Read is invoked when an edge event is detected by monitorPresence
267 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700268 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800269 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700270 }
271 else
272 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800273 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700274 }
275}
276
277bool PresenceSensor::getValue(void)
278{
279 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700280}
281
James Feist9bb67462019-03-15 15:09:50 -0700282RedundancySensor::RedundancySensor(size_t count,
283 const std::vector<std::string>& children,
284 sdbusplus::asio::object_server& objectServer,
285 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700286 count(count),
287 iface(objectServer.add_interface(
288 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700289 "xyz.openbmc_project.Control.FanRedundancy")),
290 association(objectServer.add_interface(
291 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700292 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700293 objectServer(objectServer)
294{
James Feist9bb67462019-03-15 15:09:50 -0700295 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700296 iface->register_property("Collection", children);
297 iface->register_property("Status", std::string("Full"));
298 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
299 iface->initialize();
300}
301RedundancySensor::~RedundancySensor()
302{
James Feist9bb67462019-03-15 15:09:50 -0700303 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700304 objectServer.remove_interface(iface);
305}
James Feistd8705872019-02-08 13:26:09 -0800306void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700307{
308 statuses[name] = failed;
309 size_t failedCount = 0;
310
James Feist7b18b1e2019-05-14 13:42:09 -0700311 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800312 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700313 {
314 if (status.second)
315 {
316 failedCount++;
317 }
318 if (failedCount > count)
319 {
James Feist7b18b1e2019-05-14 13:42:09 -0700320 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700321 break;
322 }
323 else if (failedCount)
324 {
James Feist7b18b1e2019-05-14 13:42:09 -0700325 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700326 }
327 }
James Feist7b18b1e2019-05-14 13:42:09 -0700328 if (state != newState)
329 {
330 if (state == redundancy::full)
331 {
332 logFanRedundancyLost();
333 }
334 else if (newState == redundancy::full)
335 {
336 logFanRedundancyRestored();
337 }
338 state = newState;
339 iface->set_property("Status", state);
340 }
James Feistdc6c55f2018-10-31 12:53:20 -0700341}