blob: ffde46a04428ba1acba4b3de11e97cba91c36b9c [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 Barth2b3db612017-10-25 10:56:51 -050063 // Update target speed to current zone target speed
64 if (!_fans.empty())
65 {
66 _targetSpeed = _fans.front()->getTargetSpeed();
67 }
Matthew Barthccc77702017-07-28 13:43:04 -050068 // Setup signal trigger for set speed events
69 for (auto& event : std::get<setSpeedEventsPos>(def))
70 {
71 initEvent(event);
72 }
Matthew Barth8600d9a2017-06-23 14:38:05 -050073 // Start timer for fan speed decreases
Matthew Bartha9561842017-06-29 11:43:45 -050074 if (!_decTimer.running() && _decInterval != seconds::zero())
Matthew Barth8600d9a2017-06-23 14:38:05 -050075 {
Matthew Bartha9561842017-06-29 11:43:45 -050076 _decTimer.start(_decInterval,
Matthew Barthd953bb22017-08-22 10:45:28 -050077 util::Timer::TimerType::repeating);
Matthew Barth8600d9a2017-06-23 14:38:05 -050078 }
Matthew Barth38a93a82017-05-11 14:12:27 -050079 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050080}
81
Matt Spinler7f88fe62017-04-10 14:39:02 -050082void Zone::setSpeed(uint64_t speed)
83{
Matthew Barth60b00762017-08-15 13:39:06 -050084 if (_isActive)
Matt Spinler7f88fe62017-04-10 14:39:02 -050085 {
Matthew Barth60b00762017-08-15 13:39:06 -050086 _targetSpeed = speed;
87 for (auto& fan : _fans)
88 {
89 fan->setSpeed(_targetSpeed);
90 }
91 }
92}
93
94void Zone::setFullSpeed()
95{
96 if (_fullSpeed != 0)
97 {
98 _targetSpeed = _fullSpeed;
99 for (auto& fan : _fans)
100 {
101 fan->setSpeed(_targetSpeed);
102 }
Matt Spinler7f88fe62017-04-10 14:39:02 -0500103 }
104}
105
Matthew Barth861d77c2017-05-22 14:18:25 -0500106void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
107{
Matthew Barth60b00762017-08-15 13:39:06 -0500108 _active[*(group)] = isActiveAllow;
Matthew Barth861d77c2017-05-22 14:18:25 -0500109 if (!isActiveAllow)
110 {
111 _isActive = false;
112 }
113 else
114 {
115 // Check all entries are set to allow control active
116 auto actPred = [](auto const& entry) {return entry.second;};
117 _isActive = std::all_of(_active.begin(),
118 _active.end(),
119 actPred);
120 }
121}
122
Matthew Barth55dea642017-11-06 13:34:32 -0600123void Zone::removeService(const Group* group,
124 const std::string& name)
125{
126 try
127 {
128 auto& sNames = _services.at(*group);
129 auto it = std::find_if(
130 sNames.begin(),
131 sNames.end(),
132 [&name](auto const& entry)
133 {
134 return name == std::get<namePos>(entry);
135 }
136 );
137 if (it != std::end(sNames))
138 {
139 // Remove service name from group
140 sNames.erase(it);
141 }
142 }
143 catch (const std::out_of_range& oore)
144 {
145 // No services for group found
146 }
147}
148
Matthew Barthe59fdf72017-09-27 09:33:42 -0500149void Zone::setServiceOwner(const Group* group,
150 const std::string& name,
151 const bool hasOwner)
152{
153 try
154 {
155 auto& sNames = _services.at(*group);
156 auto it = std::find_if(
157 sNames.begin(),
158 sNames.end(),
159 [&name](auto const& entry)
160 {
161 return name == std::get<namePos>(entry);
162 }
163 );
164 if (it != std::end(sNames))
165 {
166 std::get<hasOwnerPos>(*it) = hasOwner;
167 }
168 else
169 {
170 _services[*group].emplace_back(name, hasOwner);
171 }
172 }
173 catch (const std::out_of_range& oore)
174 {
175 _services[*group].emplace_back(name, hasOwner);
176 }
177}
178
Matthew Barth480787c2017-11-06 11:00:00 -0600179void Zone::setServices(const Group* group)
180{
Matthew Barth55dea642017-11-06 13:34:32 -0600181 // Remove the empty service name if exists
182 removeService(group, "");
Matthew Barth480787c2017-11-06 11:00:00 -0600183 for (auto it = group->begin(); it != group->end(); ++it)
184 {
185 std::string name;
186 bool hasOwner = false;
187 try
188 {
189 name = util::SDBusPlus::getService(
190 _bus,
191 it->first,
192 std::get<intfPos>(it->second));
193 hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
194 _bus,
195 "org.freedesktop.DBus",
196 "/org/freedesktop/DBus",
197 "org.freedesktop.DBus",
198 "NameHasOwner",
199 name);
200 }
201 catch (const InternalFailure& ife)
202 {
203 // Failed to get service name owner state
204 hasOwner = false;
205 }
206 setServiceOwner(group, name, hasOwner);
207 }
208}
209
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500210void Zone::setFloor(uint64_t speed)
211{
Matthew Barth98726c42017-10-17 10:35:20 -0500212 // Check all entries are set to allow floor to be set
213 auto pred = [](auto const& entry) {return entry.second;};
214 auto setFloor = std::all_of(_floorChange.begin(),
215 _floorChange.end(),
216 pred);
217 if (setFloor)
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500218 {
Matthew Barth98726c42017-10-17 10:35:20 -0500219 _floorSpeed = speed;
220 // Floor speed above target, update target to floor speed
221 if (_targetSpeed < _floorSpeed)
222 {
223 requestSpeedIncrease(_floorSpeed - _targetSpeed);
224 }
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500225 }
226}
227
Matthew Barth240397b2017-06-22 11:23:30 -0500228void Zone::requestSpeedIncrease(uint64_t targetDelta)
229{
230 // Only increase speed when delta is higher than
231 // the current increase delta for the zone and currently under ceiling
232 if (targetDelta > _incSpeedDelta &&
233 _targetSpeed < _ceilingSpeed)
234 {
Matthew Barth4e728542017-09-14 16:47:55 -0500235 auto requestTarget = getRequestSpeedBase();
Matthew Barth60b00762017-08-15 13:39:06 -0500236 requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
Matthew Barth240397b2017-06-22 11:23:30 -0500237 _incSpeedDelta = targetDelta;
Matthew Barth240397b2017-06-22 11:23:30 -0500238 // Target speed can not go above a defined ceiling speed
Matthew Barth60b00762017-08-15 13:39:06 -0500239 if (requestTarget > _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500240 {
Matthew Barth60b00762017-08-15 13:39:06 -0500241 requestTarget = _ceilingSpeed;
Matthew Barth240397b2017-06-22 11:23:30 -0500242 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500243 // Cancel current timer countdown
244 if (_incTimer.running())
245 {
246 _incTimer.stop();
247 }
Matthew Barth60b00762017-08-15 13:39:06 -0500248 setSpeed(requestTarget);
Matthew Barth1ee48f22017-06-27 15:14:48 -0500249 // Start timer countdown for fan speed increase
Matthew Bartha9561842017-06-29 11:43:45 -0500250 _incTimer.start(_incDelay,
Matthew Barthd953bb22017-08-22 10:45:28 -0500251 util::Timer::TimerType::oneshot);
Matthew Barth240397b2017-06-22 11:23:30 -0500252 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500253}
254
255void Zone::incTimerExpired()
256{
257 // Clear increase delta when timer expires allowing additional speed
258 // increase requests or speed decreases to occur
Matthew Barth240397b2017-06-22 11:23:30 -0500259 _incSpeedDelta = 0;
260}
261
Matthew Barth0ce99d82017-06-22 15:07:29 -0500262void Zone::requestSpeedDecrease(uint64_t targetDelta)
263{
264 // Only decrease the lowest target delta requested
265 if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
266 {
267 _decSpeedDelta = targetDelta;
268 }
Matthew Barth8600d9a2017-06-23 14:38:05 -0500269}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500270
Matthew Barth8600d9a2017-06-23 14:38:05 -0500271void Zone::decTimerExpired()
272{
Matthew Barth1ee48f22017-06-27 15:14:48 -0500273 // Only decrease speeds when no requested increases exist and
274 // the increase timer is not running (i.e. not in the middle of increasing)
275 if (_incSpeedDelta == 0 && !_incTimer.running())
Matthew Barth0ce99d82017-06-22 15:07:29 -0500276 {
Matthew Barth4e728542017-09-14 16:47:55 -0500277 auto requestTarget = getRequestSpeedBase();
Matthew Barthc63973a2017-12-08 15:31:35 -0600278 // Request target speed should not start above ceiling
279 if (requestTarget > _ceilingSpeed)
280 {
281 requestTarget = _ceilingSpeed;
282 }
Matthew Barth0ce99d82017-06-22 15:07:29 -0500283 // Target speed can not go below the defined floor speed
Matthew Barth60b00762017-08-15 13:39:06 -0500284 if ((requestTarget < _decSpeedDelta) ||
285 (requestTarget - _decSpeedDelta < _floorSpeed))
Matthew Barth0ce99d82017-06-22 15:07:29 -0500286 {
Matthew Barth60b00762017-08-15 13:39:06 -0500287 requestTarget = _floorSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500288 }
289 else
290 {
Matthew Barth60b00762017-08-15 13:39:06 -0500291 requestTarget = requestTarget - _decSpeedDelta;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500292 }
Matthew Barth60b00762017-08-15 13:39:06 -0500293 setSpeed(requestTarget);
Matthew Barth0ce99d82017-06-22 15:07:29 -0500294 }
295 // Clear decrease delta when timer expires
296 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500297 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500298}
299
Matthew Barthccc77702017-07-28 13:43:04 -0500300void Zone::initEvent(const SetSpeedEvent& event)
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500301{
Matthew Barth336f18a2017-09-26 09:15:56 -0500302 sdbusplus::message::message nullMsg{nullptr};
303
Matthew Barth67967f92017-09-22 12:43:57 -0500304 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthccc77702017-07-28 13:43:04 -0500305 {
Matthew Barth336f18a2017-09-26 09:15:56 -0500306 // Initialize the event signal using handler
307 std::get<sigHandlerPos>(sig)(_bus, nullMsg, *this);
308 // Setup signal matches of the property for event
Matthew Barthf6b76d82017-08-04 12:58:02 -0500309 std::unique_ptr<EventData> eventData =
310 std::make_unique<EventData>(
Matthew Barthf6b76d82017-08-04 12:58:02 -0500311 std::get<groupPos>(event),
Matthew Barth336f18a2017-09-26 09:15:56 -0500312 std::get<sigMatchPos>(sig),
313 std::get<sigHandlerPos>(sig),
Matthew Barthf9201ab2017-09-11 16:07:58 -0500314 std::get<actionsPos>(event)
Matthew Barthf6b76d82017-08-04 12:58:02 -0500315 );
Matthew Barth336f18a2017-09-26 09:15:56 -0500316 std::unique_ptr<sdbusplus::server::match::match> match = nullptr;
317 if (!std::get<sigMatchPos>(sig).empty())
318 {
319 match = std::make_unique<sdbusplus::server::match::match>(
320 _bus,
321 std::get<sigMatchPos>(sig).c_str(),
322 std::bind(std::mem_fn(&Zone::handleEvent),
323 this,
324 std::placeholders::_1,
325 eventData.get())
326 );
327 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500328 _signalEvents.emplace_back(std::move(eventData), std::move(match));
Matthew Barthccc77702017-07-28 13:43:04 -0500329 }
Matthew Barth90149802017-08-15 10:51:37 -0500330 // Attach a timer to run the action of an event
331 auto eventTimer = std::get<timerPos>(event);
332 if (std::get<intervalPos>(eventTimer) != seconds(0))
333 {
Matthew Barthbfb1a562017-10-05 17:03:40 -0500334 // Associate event data with timer
335 std::unique_ptr<EventData> eventData =
336 std::make_unique<EventData>(
Matthew Barth34a389e2017-11-07 14:01:22 -0600337 std::get<groupPos>(event),
338 "",
339 nullptr,
340 std::get<actionsPos>(event)
Matthew Barthbfb1a562017-10-05 17:03:40 -0500341 );
Matthew Barth90149802017-08-15 10:51:37 -0500342 std::unique_ptr<util::Timer> timer =
343 std::make_unique<util::Timer>(
344 _sdEvents,
345 [this,
Matthew Barthf9201ab2017-09-11 16:07:58 -0500346 action = &(std::get<actionsPos>(event)),
Matthew Barth90149802017-08-15 10:51:37 -0500347 group = &(std::get<groupPos>(event))]()
348 {
349 this->timerExpired(*group, *action);
350 });
351 if (!timer->running())
352 {
353 timer->start(std::get<intervalPos>(eventTimer),
Matthew Barth7b7ceb82017-10-04 12:59:50 -0500354 std::get<typePos>(eventTimer));
Matthew Barth90149802017-08-15 10:51:37 -0500355 }
Matthew Barthbfb1a562017-10-05 17:03:40 -0500356 addTimer(std::move(eventData), std::move(timer));
Matthew Barth90149802017-08-15 10:51:37 -0500357 }
Matthew Barthf9201ab2017-09-11 16:07:58 -0500358 // Run action functions for initial event state
359 std::for_each(
360 std::get<actionsPos>(event).begin(),
361 std::get<actionsPos>(event).end(),
362 [this, &event](auto const& action)
363 {
364 action(*this,
365 std::get<groupPos>(event));
366 });
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500367}
368
Matthew Barthf6b76d82017-08-04 12:58:02 -0500369void Zone::removeEvent(const SetSpeedEvent& event)
370{
371 // Find the signal event to be removed
372 auto it = std::find_if(
373 _signalEvents.begin(),
374 _signalEvents.end(),
375 [&event](auto const& se)
376 {
377 auto seEventData = *std::get<signalEventDataPos>(se);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500378 if (std::get<eventActionsPos>(seEventData).size() !=
379 std::get<actionsPos>(event).size())
380 {
381 return false;
382 }
383 else
384 {
385 // TODO openbmc/openbmc#2328 - Use the action function target
386 // for comparison
387 auto actsEqual = [](auto const& a1,
388 auto const& a2)
389 {
390 return a1.target_type().name() ==
391 a2.target_type().name();
392 };
393 return
394 (
395 std::get<eventGroupPos>(seEventData) ==
396 std::get<groupPos>(event) &&
397 std::equal(std::get<actionsPos>(event).begin(),
398 std::get<actionsPos>(event).end(),
399 std::get<eventActionsPos>(seEventData).begin(),
400 actsEqual)
401 );
402 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500403 });
404 if (it != std::end(_signalEvents))
405 {
406 std::get<signalEventDataPos>(*it).reset();
Matthew Barth336f18a2017-09-26 09:15:56 -0500407 if (std::get<signalMatchPos>(*it) != nullptr)
408 {
409 std::get<signalMatchPos>(*it).reset();
410 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500411 _signalEvents.erase(it);
412 }
413}
414
Matthew Barthbfb1a562017-10-05 17:03:40 -0500415std::vector<TimerEvent>::iterator Zone::findTimer(
416 const Group& eventGroup,
417 const std::vector<Action>& eventActions)
418{
419 for (auto it = _timerEvents.begin(); it != _timerEvents.end(); ++it)
420 {
421 auto teEventData = *std::get<timerEventDataPos>(*it);
422 if (std::get<eventActionsPos>(teEventData).size() ==
423 eventActions.size())
424 {
425 // TODO openbmc/openbmc#2328 - Use the action function target
426 // for comparison
427 auto actsEqual = [](auto const& a1,
428 auto const& a2)
429 {
430 return a1.target_type().name() ==
431 a2.target_type().name();
432 };
433 if (std::get<eventGroupPos>(teEventData) == eventGroup &&
434 std::equal(eventActions.begin(),
435 eventActions.end(),
436 std::get<eventActionsPos>(teEventData).begin(),
437 actsEqual))
438 {
439 return it;
440 }
441 }
442 }
443
444 return _timerEvents.end();
445}
446
Matthew Barthf9201ab2017-09-11 16:07:58 -0500447void Zone::timerExpired(Group eventGroup, std::vector<Action> eventActions)
Matthew Barth90149802017-08-15 10:51:37 -0500448{
Matthew Barthf9201ab2017-09-11 16:07:58 -0500449 // Perform the actions
450 std::for_each(eventActions.begin(),
451 eventActions.end(),
452 [this, &eventGroup](auto const& action)
453 {
454 action(*this, eventGroup);
455 });
Matthew Barth90149802017-08-15 10:51:37 -0500456}
457
Matthew Barth38a93a82017-05-11 14:12:27 -0500458void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth34f1bda2017-05-31 13:45:36 -0500459 const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500460{
461 // Handle the callback
Matthew Barth34f1bda2017-05-31 13:45:36 -0500462 std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500463 // Perform the actions
464 std::for_each(
465 std::get<eventActionsPos>(*eventData).begin(),
466 std::get<eventActionsPos>(*eventData).end(),
467 [this, &eventData](auto const& action)
468 {
469 action(*this,
470 std::get<eventGroupPos>(*eventData));
471 });
Matthew Barth38a93a82017-05-11 14:12:27 -0500472}
473
Matt Spinler7f88fe62017-04-10 14:39:02 -0500474}
475}
476}