blob: 4d31ecf4649160f9d437e057a84899b94e96aa04 [file] [log] [blame]
Matt Spinler7f88fe62017-04-10 14:39:02 -05001/**
2 * Copyright © 2017 IBM 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 */
Matthew Barth8600d9a2017-06-23 14:38:05 -050016#include <chrono>
Dinesh Chinari618027a2017-06-26 23:26:50 -050017#include <phosphor-logging/log.hpp>
Matthew Barthdf3e8d62017-05-31 11:07:24 -050018#include <phosphor-logging/elog.hpp>
Dinesh Chinari618027a2017-06-26 23:26:50 -050019#include <phosphor-logging/elog-errors.hpp>
20#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler7f88fe62017-04-10 14:39:02 -050021#include "zone.hpp"
Matthew Barthdf3e8d62017-05-31 11:07:24 -050022#include "utility.hpp"
Matthew Barthd953bb22017-08-22 10:45:28 -050023#include "sdbusplus.hpp"
Matt Spinler7f88fe62017-04-10 14:39:02 -050024
25namespace phosphor
26{
27namespace fan
28{
29namespace control
30{
31
Matthew Barth8600d9a2017-06-23 14:38:05 -050032using namespace std::chrono;
Matthew Barth90149802017-08-15 10:51:37 -050033using namespace phosphor::fan;
Matthew Barthdf3e8d62017-05-31 11:07:24 -050034using namespace phosphor::logging;
Dinesh Chinari618027a2017-06-26 23:26:50 -050035using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
36 Error::InternalFailure;
Matt Spinler7f88fe62017-04-10 14:39:02 -050037
Matthew Barth14184132017-05-19 14:37:30 -050038Zone::Zone(Mode mode,
39 sdbusplus::bus::bus& bus,
Matthew Barth8600d9a2017-06-23 14:38:05 -050040 phosphor::fan::event::EventPtr& events,
Matt Spinler7f88fe62017-04-10 14:39:02 -050041 const ZoneDefinition& def) :
42 _bus(bus),
43 _fullSpeed(std::get<fullSpeedPos>(def)),
Matthew Barth1de66622017-06-12 13:13:02 -050044 _zoneNum(std::get<zoneNumPos>(def)),
Matthew Barthe0ca13e2017-06-13 16:29:09 -050045 _defFloorSpeed(std::get<floorSpeedPos>(def)),
Matthew Barth8600d9a2017-06-23 14:38:05 -050046 _defCeilingSpeed(std::get<fullSpeedPos>(def)),
Matthew Bartha9561842017-06-29 11:43:45 -050047 _incDelay(std::get<incDelayPos>(def)),
48 _decInterval(std::get<decIntervalPos>(def)),
Matthew Barth1ee48f22017-06-27 15:14:48 -050049 _incTimer(events, [this](){ this->incTimerExpired(); }),
Matthew Barth90149802017-08-15 10:51:37 -050050 _decTimer(events, [this](){ this->decTimerExpired(); }),
51 _sdEvents(events)
Matt Spinler7f88fe62017-04-10 14:39:02 -050052{
53 auto& fanDefs = std::get<fanListPos>(def);
54
55 for (auto& def : fanDefs)
56 {
57 _fans.emplace_back(std::make_unique<Fan>(bus, def));
58 }
Matthew Barth38a93a82017-05-11 14:12:27 -050059
Matthew Barth14184132017-05-19 14:37:30 -050060 // Do not enable set speed events when in init mode
61 if (mode != Mode::init)
Matthew Barth17d1fe22017-05-11 15:00:36 -050062 {
Matthew Barthccc77702017-07-28 13:43:04 -050063 // Setup signal trigger for set speed events
64 for (auto& event : std::get<setSpeedEventsPos>(def))
65 {
66 initEvent(event);
67 }
Matthew Barth8600d9a2017-06-23 14:38:05 -050068 // Start timer for fan speed decreases
Matthew Bartha9561842017-06-29 11:43:45 -050069 if (!_decTimer.running() && _decInterval != seconds::zero())
Matthew Barth8600d9a2017-06-23 14:38:05 -050070 {
Matthew Bartha9561842017-06-29 11:43:45 -050071 _decTimer.start(_decInterval,
Matthew Barthd953bb22017-08-22 10:45:28 -050072 util::Timer::TimerType::repeating);
Matthew Barth8600d9a2017-06-23 14:38:05 -050073 }
Matthew Barth38a93a82017-05-11 14:12:27 -050074 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050075}
76
Matt Spinler7f88fe62017-04-10 14:39:02 -050077void Zone::setSpeed(uint64_t speed)
78{
Matthew Barth60b00762017-08-15 13:39:06 -050079 if (_isActive)
Matt Spinler7f88fe62017-04-10 14:39:02 -050080 {
Matthew Barth60b00762017-08-15 13:39:06 -050081 _targetSpeed = speed;
82 for (auto& fan : _fans)
83 {
84 fan->setSpeed(_targetSpeed);
85 }
86 }
87}
88
89void Zone::setFullSpeed()
90{
91 if (_fullSpeed != 0)
92 {
93 _targetSpeed = _fullSpeed;
94 for (auto& fan : _fans)
95 {
96 fan->setSpeed(_targetSpeed);
97 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050098 }
99}
100
Matthew Barth861d77c2017-05-22 14:18:25 -0500101void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
102{
Matthew Barth60b00762017-08-15 13:39:06 -0500103 _active[*(group)] = isActiveAllow;
Matthew Barth861d77c2017-05-22 14:18:25 -0500104 if (!isActiveAllow)
105 {
106 _isActive = false;
107 }
108 else
109 {
110 // Check all entries are set to allow control active
111 auto actPred = [](auto const& entry) {return entry.second;};
112 _isActive = std::all_of(_active.begin(),
113 _active.end(),
114 actPred);
115 }
116}
117
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500118void Zone::setFloor(uint64_t speed)
119{
120 _floorSpeed = speed;
121 // Floor speed above target, update target to floor speed
122 if (_targetSpeed < _floorSpeed)
123 {
124 requestSpeedIncrease(_floorSpeed - _targetSpeed);
125 }
126}
127
Matthew Barth240397b2017-06-22 11:23:30 -0500128void Zone::requestSpeedIncrease(uint64_t targetDelta)
129{
130 // Only increase speed when delta is higher than
131 // the current increase delta for the zone and currently under ceiling
132 if (targetDelta > _incSpeedDelta &&
133 _targetSpeed < _ceilingSpeed)
134 {
Matthew Barth60b00762017-08-15 13:39:06 -0500135 auto requestTarget = _targetSpeed;
136 requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
Matthew Barth240397b2017-06-22 11:23:30 -0500137 _incSpeedDelta = targetDelta;
Matthew Barth240397b2017-06-22 11:23:30 -0500138 // Target speed can not go above a defined ceiling speed
Matthew Barth60b00762017-08-15 13:39:06 -0500139 if (requestTarget > _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500140 {
Matthew Barth60b00762017-08-15 13:39:06 -0500141 requestTarget = _ceilingSpeed;
Matthew Barth240397b2017-06-22 11:23:30 -0500142 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500143 // Cancel current timer countdown
144 if (_incTimer.running())
145 {
146 _incTimer.stop();
147 }
Matthew Barth60b00762017-08-15 13:39:06 -0500148 setSpeed(requestTarget);
Matthew Barth1ee48f22017-06-27 15:14:48 -0500149 // Start timer countdown for fan speed increase
Matthew Bartha9561842017-06-29 11:43:45 -0500150 _incTimer.start(_incDelay,
Matthew Barthd953bb22017-08-22 10:45:28 -0500151 util::Timer::TimerType::oneshot);
Matthew Barth240397b2017-06-22 11:23:30 -0500152 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500153}
154
155void Zone::incTimerExpired()
156{
157 // Clear increase delta when timer expires allowing additional speed
158 // increase requests or speed decreases to occur
Matthew Barth240397b2017-06-22 11:23:30 -0500159 _incSpeedDelta = 0;
160}
161
Matthew Barth0ce99d82017-06-22 15:07:29 -0500162void Zone::requestSpeedDecrease(uint64_t targetDelta)
163{
164 // Only decrease the lowest target delta requested
165 if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
166 {
167 _decSpeedDelta = targetDelta;
168 }
Matthew Barth8600d9a2017-06-23 14:38:05 -0500169}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500170
Matthew Barth8600d9a2017-06-23 14:38:05 -0500171void Zone::decTimerExpired()
172{
Matthew Barth1ee48f22017-06-27 15:14:48 -0500173 // Only decrease speeds when no requested increases exist and
174 // the increase timer is not running (i.e. not in the middle of increasing)
175 if (_incSpeedDelta == 0 && !_incTimer.running())
Matthew Barth0ce99d82017-06-22 15:07:29 -0500176 {
Matthew Barth60b00762017-08-15 13:39:06 -0500177 auto requestTarget = _targetSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500178 // Target speed can not go below the defined floor speed
Matthew Barth60b00762017-08-15 13:39:06 -0500179 if ((requestTarget < _decSpeedDelta) ||
180 (requestTarget - _decSpeedDelta < _floorSpeed))
Matthew Barth0ce99d82017-06-22 15:07:29 -0500181 {
Matthew Barth60b00762017-08-15 13:39:06 -0500182 requestTarget = _floorSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500183 }
184 else
185 {
Matthew Barth60b00762017-08-15 13:39:06 -0500186 requestTarget = requestTarget - _decSpeedDelta;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500187 }
Matthew Barth60b00762017-08-15 13:39:06 -0500188 setSpeed(requestTarget);
Matthew Barth0ce99d82017-06-22 15:07:29 -0500189 }
190 // Clear decrease delta when timer expires
191 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500192 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500193}
194
Matthew Barthccc77702017-07-28 13:43:04 -0500195void Zone::initEvent(const SetSpeedEvent& event)
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500196{
Matthew Barthccc77702017-07-28 13:43:04 -0500197 // Get the current value for each property
Matthew Barth604329e2017-08-04 11:18:28 -0500198 for (auto& group : std::get<groupPos>(event))
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500199 {
Matthew Barth604329e2017-08-04 11:18:28 -0500200 try
201 {
202 refreshProperty(_bus,
203 group.first,
204 std::get<intfPos>(group.second),
205 std::get<propPos>(group.second));
206 }
207 catch (const InternalFailure& ife)
208 {
Matthew Barthd953bb22017-08-22 10:45:28 -0500209 log<level::INFO>(
210 "Unable to find property",
Matthew Barth604329e2017-08-04 11:18:28 -0500211 entry("PATH=%s", group.first.c_str()),
212 entry("INTERFACE=%s", std::get<intfPos>(group.second).c_str()),
213 entry("PROPERTY=%s", std::get<propPos>(group.second).c_str()));
214 }
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500215 }
Matthew Barthccc77702017-07-28 13:43:04 -0500216 // Setup signal matches for property change events
217 for (auto& prop : std::get<propChangeListPos>(event))
218 {
Matthew Barthf6b76d82017-08-04 12:58:02 -0500219 std::unique_ptr<EventData> eventData =
220 std::make_unique<EventData>(
221 EventData
222 {
223 std::get<groupPos>(event),
224 std::get<handlerObjPos>(prop),
225 std::get<actionPos>(event)
226 }
227 );
228 std::unique_ptr<sdbusplus::server::match::match> match =
229 std::make_unique<sdbusplus::server::match::match>(
Matthew Barthccc77702017-07-28 13:43:04 -0500230 _bus,
231 std::get<signaturePos>(prop).c_str(),
232 std::bind(std::mem_fn(&Zone::handleEvent),
233 this,
234 std::placeholders::_1,
Matthew Barthf6b76d82017-08-04 12:58:02 -0500235 eventData.get())
236 );
237 _signalEvents.emplace_back(std::move(eventData), std::move(match));
Matthew Barthccc77702017-07-28 13:43:04 -0500238 }
Matthew Barth90149802017-08-15 10:51:37 -0500239 // Attach a timer to run the action of an event
240 auto eventTimer = std::get<timerPos>(event);
241 if (std::get<intervalPos>(eventTimer) != seconds(0))
242 {
243 std::unique_ptr<util::Timer> timer =
244 std::make_unique<util::Timer>(
245 _sdEvents,
246 [this,
247 action = &(std::get<actionPos>(event)),
248 group = &(std::get<groupPos>(event))]()
249 {
250 this->timerExpired(*group, *action);
251 });
252 if (!timer->running())
253 {
254 timer->start(std::get<intervalPos>(eventTimer),
255 util::Timer::TimerType::repeating);
256 }
257 _timerEvents.emplace_back(std::move(timer));
258 }
Matthew Barthccc77702017-07-28 13:43:04 -0500259 // Run action function for initial event state
260 std::get<actionPos>(event)(*this,
261 std::get<groupPos>(event));
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500262}
263
Matthew Barthf6b76d82017-08-04 12:58:02 -0500264void Zone::removeEvent(const SetSpeedEvent& event)
265{
266 // Find the signal event to be removed
267 auto it = std::find_if(
268 _signalEvents.begin(),
269 _signalEvents.end(),
270 [&event](auto const& se)
271 {
272 auto seEventData = *std::get<signalEventDataPos>(se);
273 // TODO Use the action function target for comparison
274 return
275 (
276 std::get<eventGroupPos>(seEventData) ==
277 std::get<groupPos>(event) &&
278 std::get<eventActionPos>(seEventData).target_type().name() ==
279 std::get<actionPos>(event).target_type().name()
280 );
281 });
282 if (it != std::end(_signalEvents))
283 {
284 std::get<signalEventDataPos>(*it).reset();
285 std::get<signalMatchPos>(*it).reset();
286 _signalEvents.erase(it);
287 }
288}
289
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500290void Zone::refreshProperty(sdbusplus::bus::bus& bus,
291 const std::string& path,
292 const std::string& iface,
293 const std::string& prop)
294{
295 PropertyVariantType property;
296 getProperty(_bus, path, iface, prop, property);
297 setPropertyValue(path.c_str(), iface.c_str(), prop.c_str(), property);
298}
299
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500300void Zone::getProperty(sdbusplus::bus::bus& bus,
301 const std::string& path,
302 const std::string& iface,
303 const std::string& prop,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500304 PropertyVariantType& value)
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500305{
Matthew Barthd953bb22017-08-22 10:45:28 -0500306 auto serv = util::SDBusPlus::getService(bus, path, iface);
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500307 auto hostCall = bus.new_method_call(serv.c_str(),
308 path.c_str(),
309 "org.freedesktop.DBus.Properties",
310 "Get");
311 hostCall.append(iface);
312 hostCall.append(prop);
313 auto hostResponseMsg = bus.call(hostCall);
314 if (hostResponseMsg.is_method_error())
315 {
Matthew Barthd953bb22017-08-22 10:45:28 -0500316 log<level::INFO>("Host call response error for retrieving property");
Dinesh Chinari618027a2017-06-26 23:26:50 -0500317 elog<InternalFailure>();
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500318 }
Matthew Barth9e741ed2017-06-02 16:29:09 -0500319 hostResponseMsg.read(value);
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500320}
321
Matthew Barth90149802017-08-15 10:51:37 -0500322void Zone::timerExpired(Group eventGroup, Action eventAction)
323{
324 // Perform the action
325 eventAction(*this, eventGroup);
326}
327
Matthew Barth38a93a82017-05-11 14:12:27 -0500328void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth34f1bda2017-05-31 13:45:36 -0500329 const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500330{
331 // Handle the callback
Matthew Barth34f1bda2017-05-31 13:45:36 -0500332 std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
Matthew Barth17d1fe22017-05-11 15:00:36 -0500333 // Perform the action
Matthew Barth34f1bda2017-05-31 13:45:36 -0500334 std::get<eventActionPos>(*eventData)(*this,
335 std::get<eventGroupPos>(*eventData));
Matthew Barth38a93a82017-05-11 14:12:27 -0500336}
337
Matt Spinler7f88fe62017-04-10 14:39:02 -0500338}
339}
340}