blob: 418b3ba84dc13c1231286b51482bb754fa24f413 [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 Barthe59fdf72017-09-27 09:33:42 -0500118void Zone::setServiceOwner(const Group* group,
119 const std::string& name,
120 const bool hasOwner)
121{
122 try
123 {
124 auto& sNames = _services.at(*group);
125 auto it = std::find_if(
126 sNames.begin(),
127 sNames.end(),
128 [&name](auto const& entry)
129 {
130 return name == std::get<namePos>(entry);
131 }
132 );
133 if (it != std::end(sNames))
134 {
135 std::get<hasOwnerPos>(*it) = hasOwner;
136 }
137 else
138 {
139 _services[*group].emplace_back(name, hasOwner);
140 }
141 }
142 catch (const std::out_of_range& oore)
143 {
144 _services[*group].emplace_back(name, hasOwner);
145 }
146}
147
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500148void Zone::setFloor(uint64_t speed)
149{
Matthew Barth98726c42017-10-17 10:35:20 -0500150 // Check all entries are set to allow floor to be set
151 auto pred = [](auto const& entry) {return entry.second;};
152 auto setFloor = std::all_of(_floorChange.begin(),
153 _floorChange.end(),
154 pred);
155 if (setFloor)
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500156 {
Matthew Barth98726c42017-10-17 10:35:20 -0500157 _floorSpeed = speed;
158 // Floor speed above target, update target to floor speed
159 if (_targetSpeed < _floorSpeed)
160 {
161 requestSpeedIncrease(_floorSpeed - _targetSpeed);
162 }
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500163 }
164}
165
Matthew Barth240397b2017-06-22 11:23:30 -0500166void Zone::requestSpeedIncrease(uint64_t targetDelta)
167{
168 // Only increase speed when delta is higher than
169 // the current increase delta for the zone and currently under ceiling
170 if (targetDelta > _incSpeedDelta &&
171 _targetSpeed < _ceilingSpeed)
172 {
Matthew Barth4e728542017-09-14 16:47:55 -0500173 auto requestTarget = getRequestSpeedBase();
Matthew Barth60b00762017-08-15 13:39:06 -0500174 requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
Matthew Barth240397b2017-06-22 11:23:30 -0500175 _incSpeedDelta = targetDelta;
Matthew Barth240397b2017-06-22 11:23:30 -0500176 // Target speed can not go above a defined ceiling speed
Matthew Barth60b00762017-08-15 13:39:06 -0500177 if (requestTarget > _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500178 {
Matthew Barth60b00762017-08-15 13:39:06 -0500179 requestTarget = _ceilingSpeed;
Matthew Barth240397b2017-06-22 11:23:30 -0500180 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500181 // Cancel current timer countdown
182 if (_incTimer.running())
183 {
184 _incTimer.stop();
185 }
Matthew Barth60b00762017-08-15 13:39:06 -0500186 setSpeed(requestTarget);
Matthew Barth1ee48f22017-06-27 15:14:48 -0500187 // Start timer countdown for fan speed increase
Matthew Bartha9561842017-06-29 11:43:45 -0500188 _incTimer.start(_incDelay,
Matthew Barthd953bb22017-08-22 10:45:28 -0500189 util::Timer::TimerType::oneshot);
Matthew Barth240397b2017-06-22 11:23:30 -0500190 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500191}
192
193void Zone::incTimerExpired()
194{
195 // Clear increase delta when timer expires allowing additional speed
196 // increase requests or speed decreases to occur
Matthew Barth240397b2017-06-22 11:23:30 -0500197 _incSpeedDelta = 0;
198}
199
Matthew Barth0ce99d82017-06-22 15:07:29 -0500200void Zone::requestSpeedDecrease(uint64_t targetDelta)
201{
202 // Only decrease the lowest target delta requested
203 if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
204 {
205 _decSpeedDelta = targetDelta;
206 }
Matthew Barth8600d9a2017-06-23 14:38:05 -0500207}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500208
Matthew Barth8600d9a2017-06-23 14:38:05 -0500209void Zone::decTimerExpired()
210{
Matthew Barth1ee48f22017-06-27 15:14:48 -0500211 // Only decrease speeds when no requested increases exist and
212 // the increase timer is not running (i.e. not in the middle of increasing)
213 if (_incSpeedDelta == 0 && !_incTimer.running())
Matthew Barth0ce99d82017-06-22 15:07:29 -0500214 {
Matthew Barth4e728542017-09-14 16:47:55 -0500215 auto requestTarget = getRequestSpeedBase();
Matthew Barth0ce99d82017-06-22 15:07:29 -0500216 // Target speed can not go below the defined floor speed
Matthew Barth60b00762017-08-15 13:39:06 -0500217 if ((requestTarget < _decSpeedDelta) ||
218 (requestTarget - _decSpeedDelta < _floorSpeed))
Matthew Barth0ce99d82017-06-22 15:07:29 -0500219 {
Matthew Barth60b00762017-08-15 13:39:06 -0500220 requestTarget = _floorSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500221 }
222 else
223 {
Matthew Barth60b00762017-08-15 13:39:06 -0500224 requestTarget = requestTarget - _decSpeedDelta;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500225 }
Matthew Barth60b00762017-08-15 13:39:06 -0500226 setSpeed(requestTarget);
Matthew Barth0ce99d82017-06-22 15:07:29 -0500227 }
228 // Clear decrease delta when timer expires
229 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500230 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500231}
232
Matthew Barthccc77702017-07-28 13:43:04 -0500233void Zone::initEvent(const SetSpeedEvent& event)
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500234{
Matthew Barth336f18a2017-09-26 09:15:56 -0500235 sdbusplus::message::message nullMsg{nullptr};
236
Matthew Barth67967f92017-09-22 12:43:57 -0500237 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthccc77702017-07-28 13:43:04 -0500238 {
Matthew Barth336f18a2017-09-26 09:15:56 -0500239 // Initialize the event signal using handler
240 std::get<sigHandlerPos>(sig)(_bus, nullMsg, *this);
241 // Setup signal matches of the property for event
Matthew Barthf6b76d82017-08-04 12:58:02 -0500242 std::unique_ptr<EventData> eventData =
243 std::make_unique<EventData>(
244 EventData
245 {
246 std::get<groupPos>(event),
Matthew Barth336f18a2017-09-26 09:15:56 -0500247 std::get<sigMatchPos>(sig),
248 std::get<sigHandlerPos>(sig),
Matthew Barthf9201ab2017-09-11 16:07:58 -0500249 std::get<actionsPos>(event)
Matthew Barthf6b76d82017-08-04 12:58:02 -0500250 }
251 );
Matthew Barth336f18a2017-09-26 09:15:56 -0500252 std::unique_ptr<sdbusplus::server::match::match> match = nullptr;
253 if (!std::get<sigMatchPos>(sig).empty())
254 {
255 match = std::make_unique<sdbusplus::server::match::match>(
256 _bus,
257 std::get<sigMatchPos>(sig).c_str(),
258 std::bind(std::mem_fn(&Zone::handleEvent),
259 this,
260 std::placeholders::_1,
261 eventData.get())
262 );
263 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500264 _signalEvents.emplace_back(std::move(eventData), std::move(match));
Matthew Barthccc77702017-07-28 13:43:04 -0500265 }
Matthew Barth90149802017-08-15 10:51:37 -0500266 // Attach a timer to run the action of an event
267 auto eventTimer = std::get<timerPos>(event);
268 if (std::get<intervalPos>(eventTimer) != seconds(0))
269 {
Matthew Barthbfb1a562017-10-05 17:03:40 -0500270 // Associate event data with timer
271 std::unique_ptr<EventData> eventData =
272 std::make_unique<EventData>(
273 EventData
274 {
275 std::get<groupPos>(event),
276 "",
277 nullptr,
278 std::get<actionsPos>(event)
279 }
280 );
Matthew Barth90149802017-08-15 10:51:37 -0500281 std::unique_ptr<util::Timer> timer =
282 std::make_unique<util::Timer>(
283 _sdEvents,
284 [this,
Matthew Barthf9201ab2017-09-11 16:07:58 -0500285 action = &(std::get<actionsPos>(event)),
Matthew Barth90149802017-08-15 10:51:37 -0500286 group = &(std::get<groupPos>(event))]()
287 {
288 this->timerExpired(*group, *action);
289 });
290 if (!timer->running())
291 {
292 timer->start(std::get<intervalPos>(eventTimer),
Matthew Barth7b7ceb82017-10-04 12:59:50 -0500293 std::get<typePos>(eventTimer));
Matthew Barth90149802017-08-15 10:51:37 -0500294 }
Matthew Barthbfb1a562017-10-05 17:03:40 -0500295 addTimer(std::move(eventData), std::move(timer));
Matthew Barth90149802017-08-15 10:51:37 -0500296 }
Matthew Barthf9201ab2017-09-11 16:07:58 -0500297 // Run action functions for initial event state
298 std::for_each(
299 std::get<actionsPos>(event).begin(),
300 std::get<actionsPos>(event).end(),
301 [this, &event](auto const& action)
302 {
303 action(*this,
304 std::get<groupPos>(event));
305 });
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500306}
307
Matthew Barthf6b76d82017-08-04 12:58:02 -0500308void Zone::removeEvent(const SetSpeedEvent& event)
309{
310 // Find the signal event to be removed
311 auto it = std::find_if(
312 _signalEvents.begin(),
313 _signalEvents.end(),
314 [&event](auto const& se)
315 {
316 auto seEventData = *std::get<signalEventDataPos>(se);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500317 if (std::get<eventActionsPos>(seEventData).size() !=
318 std::get<actionsPos>(event).size())
319 {
320 return false;
321 }
322 else
323 {
324 // TODO openbmc/openbmc#2328 - Use the action function target
325 // for comparison
326 auto actsEqual = [](auto const& a1,
327 auto const& a2)
328 {
329 return a1.target_type().name() ==
330 a2.target_type().name();
331 };
332 return
333 (
334 std::get<eventGroupPos>(seEventData) ==
335 std::get<groupPos>(event) &&
336 std::equal(std::get<actionsPos>(event).begin(),
337 std::get<actionsPos>(event).end(),
338 std::get<eventActionsPos>(seEventData).begin(),
339 actsEqual)
340 );
341 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500342 });
343 if (it != std::end(_signalEvents))
344 {
345 std::get<signalEventDataPos>(*it).reset();
Matthew Barth336f18a2017-09-26 09:15:56 -0500346 if (std::get<signalMatchPos>(*it) != nullptr)
347 {
348 std::get<signalMatchPos>(*it).reset();
349 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500350 _signalEvents.erase(it);
351 }
352}
353
Matthew Barthbfb1a562017-10-05 17:03:40 -0500354std::vector<TimerEvent>::iterator Zone::findTimer(
355 const Group& eventGroup,
356 const std::vector<Action>& eventActions)
357{
358 for (auto it = _timerEvents.begin(); it != _timerEvents.end(); ++it)
359 {
360 auto teEventData = *std::get<timerEventDataPos>(*it);
361 if (std::get<eventActionsPos>(teEventData).size() ==
362 eventActions.size())
363 {
364 // TODO openbmc/openbmc#2328 - Use the action function target
365 // for comparison
366 auto actsEqual = [](auto const& a1,
367 auto const& a2)
368 {
369 return a1.target_type().name() ==
370 a2.target_type().name();
371 };
372 if (std::get<eventGroupPos>(teEventData) == eventGroup &&
373 std::equal(eventActions.begin(),
374 eventActions.end(),
375 std::get<eventActionsPos>(teEventData).begin(),
376 actsEqual))
377 {
378 return it;
379 }
380 }
381 }
382
383 return _timerEvents.end();
384}
385
Matthew Barthf9201ab2017-09-11 16:07:58 -0500386void Zone::timerExpired(Group eventGroup, std::vector<Action> eventActions)
Matthew Barth90149802017-08-15 10:51:37 -0500387{
Matthew Barthf9201ab2017-09-11 16:07:58 -0500388 // Perform the actions
389 std::for_each(eventActions.begin(),
390 eventActions.end(),
391 [this, &eventGroup](auto const& action)
392 {
393 action(*this, eventGroup);
394 });
Matthew Barth90149802017-08-15 10:51:37 -0500395}
396
Matthew Barth38a93a82017-05-11 14:12:27 -0500397void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth34f1bda2017-05-31 13:45:36 -0500398 const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500399{
400 // Handle the callback
Matthew Barth34f1bda2017-05-31 13:45:36 -0500401 std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500402 // Perform the actions
403 std::for_each(
404 std::get<eventActionsPos>(*eventData).begin(),
405 std::get<eventActionsPos>(*eventData).end(),
406 [this, &eventData](auto const& action)
407 {
408 action(*this,
409 std::get<eventGroupPos>(*eventData));
410 });
Matthew Barth38a93a82017-05-11 14:12:27 -0500411}
412
Matt Spinler7f88fe62017-04-10 14:39:02 -0500413}
414}
415}