Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 1 | /** |
| 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 Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 16 | #include <chrono> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 17 | #include <phosphor-logging/log.hpp> |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 18 | #include <phosphor-logging/elog.hpp> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 19 | #include <phosphor-logging/elog-errors.hpp> |
| 20 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 21 | #include "zone.hpp" |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 22 | #include "utility.hpp" |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 23 | |
| 24 | namespace phosphor |
| 25 | { |
| 26 | namespace fan |
| 27 | { |
| 28 | namespace control |
| 29 | { |
| 30 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 31 | using namespace std::chrono; |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 32 | using namespace phosphor::logging; |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 33 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 34 | Error::InternalFailure; |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 35 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 36 | Zone::Zone(Mode mode, |
| 37 | sdbusplus::bus::bus& bus, |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 38 | phosphor::fan::event::EventPtr& events, |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 39 | const ZoneDefinition& def) : |
| 40 | _bus(bus), |
| 41 | _fullSpeed(std::get<fullSpeedPos>(def)), |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 42 | _zoneNum(std::get<zoneNumPos>(def)), |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 43 | _defFloorSpeed(std::get<floorSpeedPos>(def)), |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 44 | _defCeilingSpeed(std::get<fullSpeedPos>(def)), |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 45 | _incDelay(std::get<incDelayPos>(def)), |
| 46 | _decInterval(std::get<decIntervalPos>(def)), |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 47 | _incTimer(events, [this](){ this->incTimerExpired(); }), |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 48 | _decTimer(events, [this](){ this->decTimerExpired(); }) |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 49 | { |
| 50 | auto& fanDefs = std::get<fanListPos>(def); |
| 51 | |
| 52 | for (auto& def : fanDefs) |
| 53 | { |
| 54 | _fans.emplace_back(std::make_unique<Fan>(bus, def)); |
| 55 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 56 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 57 | // Do not enable set speed events when in init mode |
| 58 | if (mode != Mode::init) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 59 | { |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 60 | // Setup signal trigger for set speed events |
| 61 | for (auto& event : std::get<setSpeedEventsPos>(def)) |
| 62 | { |
| 63 | initEvent(event); |
| 64 | } |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 65 | // Start timer for fan speed decreases |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 66 | if (!_decTimer.running() && _decInterval != seconds::zero()) |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 67 | { |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 68 | _decTimer.start(_decInterval, |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 69 | phosphor::fan::util::Timer::TimerType::repeating); |
| 70 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 71 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
| 75 | void Zone::setSpeed(uint64_t speed) |
| 76 | { |
| 77 | for (auto& fan : _fans) |
| 78 | { |
| 79 | fan->setSpeed(speed); |
| 80 | } |
| 81 | } |
| 82 | |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 83 | void Zone::setActiveAllow(const Group* group, bool isActiveAllow) |
| 84 | { |
| 85 | _active[group] = isActiveAllow; |
| 86 | if (!isActiveAllow) |
| 87 | { |
| 88 | _isActive = false; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | // Check all entries are set to allow control active |
| 93 | auto actPred = [](auto const& entry) {return entry.second;}; |
| 94 | _isActive = std::all_of(_active.begin(), |
| 95 | _active.end(), |
| 96 | actPred); |
| 97 | } |
| 98 | } |
| 99 | |
Matthew Barth | b4a7cb9 | 2017-06-28 15:29:50 -0500 | [diff] [blame] | 100 | void Zone::setFloor(uint64_t speed) |
| 101 | { |
| 102 | _floorSpeed = speed; |
| 103 | // Floor speed above target, update target to floor speed |
| 104 | if (_targetSpeed < _floorSpeed) |
| 105 | { |
| 106 | requestSpeedIncrease(_floorSpeed - _targetSpeed); |
| 107 | } |
| 108 | } |
| 109 | |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 110 | void Zone::requestSpeedIncrease(uint64_t targetDelta) |
| 111 | { |
| 112 | // Only increase speed when delta is higher than |
| 113 | // the current increase delta for the zone and currently under ceiling |
| 114 | if (targetDelta > _incSpeedDelta && |
| 115 | _targetSpeed < _ceilingSpeed) |
| 116 | { |
| 117 | _targetSpeed = (targetDelta - _incSpeedDelta) + _targetSpeed; |
| 118 | _incSpeedDelta = targetDelta; |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 119 | // Target speed can not go above a defined ceiling speed |
| 120 | if (_targetSpeed > _ceilingSpeed) |
| 121 | { |
| 122 | _targetSpeed = _ceilingSpeed; |
| 123 | } |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 124 | // Cancel current timer countdown |
| 125 | if (_incTimer.running()) |
| 126 | { |
| 127 | _incTimer.stop(); |
| 128 | } |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 129 | setSpeed(_targetSpeed); |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 130 | // Start timer countdown for fan speed increase |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 131 | _incTimer.start(_incDelay, |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 132 | phosphor::fan::util::Timer::TimerType::oneshot); |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 133 | } |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void Zone::incTimerExpired() |
| 137 | { |
| 138 | // Clear increase delta when timer expires allowing additional speed |
| 139 | // increase requests or speed decreases to occur |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 140 | _incSpeedDelta = 0; |
| 141 | } |
| 142 | |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 143 | void Zone::requestSpeedDecrease(uint64_t targetDelta) |
| 144 | { |
| 145 | // Only decrease the lowest target delta requested |
| 146 | if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta) |
| 147 | { |
| 148 | _decSpeedDelta = targetDelta; |
| 149 | } |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 150 | } |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 151 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 152 | void Zone::decTimerExpired() |
| 153 | { |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 154 | // Only decrease speeds when no requested increases exist and |
| 155 | // the increase timer is not running (i.e. not in the middle of increasing) |
| 156 | if (_incSpeedDelta == 0 && !_incTimer.running()) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 157 | { |
| 158 | // Target speed can not go below the defined floor speed |
| 159 | if ((_targetSpeed < _decSpeedDelta) || |
| 160 | (_targetSpeed - _decSpeedDelta < _floorSpeed)) |
| 161 | { |
| 162 | _targetSpeed = _floorSpeed; |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | _targetSpeed = _targetSpeed - _decSpeedDelta; |
| 167 | } |
| 168 | setSpeed(_targetSpeed); |
| 169 | } |
| 170 | // Clear decrease delta when timer expires |
| 171 | _decSpeedDelta = 0; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 172 | // Decrease timer is restarted since its repeating |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 175 | void Zone::initEvent(const SetSpeedEvent& event) |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 176 | { |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 177 | // Get the current value for each property |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 178 | for (auto& group : std::get<groupPos>(event)) |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 179 | { |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 180 | try |
| 181 | { |
| 182 | refreshProperty(_bus, |
| 183 | group.first, |
| 184 | std::get<intfPos>(group.second), |
| 185 | std::get<propPos>(group.second)); |
| 186 | } |
| 187 | catch (const InternalFailure& ife) |
| 188 | { |
| 189 | log<level::ERR>( |
| 190 | "Unable to find property: ", |
| 191 | entry("PATH=%s", group.first.c_str()), |
| 192 | entry("INTERFACE=%s", std::get<intfPos>(group.second).c_str()), |
| 193 | entry("PROPERTY=%s", std::get<propPos>(group.second).c_str())); |
| 194 | } |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 195 | } |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 196 | // Setup signal matches for property change events |
| 197 | for (auto& prop : std::get<propChangeListPos>(event)) |
| 198 | { |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 199 | std::unique_ptr<EventData> eventData = |
| 200 | std::make_unique<EventData>( |
| 201 | EventData |
| 202 | { |
| 203 | std::get<groupPos>(event), |
| 204 | std::get<handlerObjPos>(prop), |
| 205 | std::get<actionPos>(event) |
| 206 | } |
| 207 | ); |
| 208 | std::unique_ptr<sdbusplus::server::match::match> match = |
| 209 | std::make_unique<sdbusplus::server::match::match>( |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 210 | _bus, |
| 211 | std::get<signaturePos>(prop).c_str(), |
| 212 | std::bind(std::mem_fn(&Zone::handleEvent), |
| 213 | this, |
| 214 | std::placeholders::_1, |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 215 | eventData.get()) |
| 216 | ); |
| 217 | _signalEvents.emplace_back(std::move(eventData), std::move(match)); |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 218 | } |
| 219 | // Run action function for initial event state |
| 220 | std::get<actionPos>(event)(*this, |
| 221 | std::get<groupPos>(event)); |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 224 | void Zone::removeEvent(const SetSpeedEvent& event) |
| 225 | { |
| 226 | // Find the signal event to be removed |
| 227 | auto it = std::find_if( |
| 228 | _signalEvents.begin(), |
| 229 | _signalEvents.end(), |
| 230 | [&event](auto const& se) |
| 231 | { |
| 232 | auto seEventData = *std::get<signalEventDataPos>(se); |
| 233 | // TODO Use the action function target for comparison |
| 234 | return |
| 235 | ( |
| 236 | std::get<eventGroupPos>(seEventData) == |
| 237 | std::get<groupPos>(event) && |
| 238 | std::get<eventActionPos>(seEventData).target_type().name() == |
| 239 | std::get<actionPos>(event).target_type().name() |
| 240 | ); |
| 241 | }); |
| 242 | if (it != std::end(_signalEvents)) |
| 243 | { |
| 244 | std::get<signalEventDataPos>(*it).reset(); |
| 245 | std::get<signalMatchPos>(*it).reset(); |
| 246 | _signalEvents.erase(it); |
| 247 | } |
| 248 | } |
| 249 | |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 250 | void Zone::refreshProperty(sdbusplus::bus::bus& bus, |
| 251 | const std::string& path, |
| 252 | const std::string& iface, |
| 253 | const std::string& prop) |
| 254 | { |
| 255 | PropertyVariantType property; |
| 256 | getProperty(_bus, path, iface, prop, property); |
| 257 | setPropertyValue(path.c_str(), iface.c_str(), prop.c_str(), property); |
| 258 | } |
| 259 | |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 260 | void Zone::getProperty(sdbusplus::bus::bus& bus, |
| 261 | const std::string& path, |
| 262 | const std::string& iface, |
| 263 | const std::string& prop, |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 264 | PropertyVariantType& value) |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 265 | { |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 266 | auto serv = phosphor::fan::util::getService(path, iface, bus); |
| 267 | auto hostCall = bus.new_method_call(serv.c_str(), |
| 268 | path.c_str(), |
| 269 | "org.freedesktop.DBus.Properties", |
| 270 | "Get"); |
| 271 | hostCall.append(iface); |
| 272 | hostCall.append(prop); |
| 273 | auto hostResponseMsg = bus.call(hostCall); |
| 274 | if (hostResponseMsg.is_method_error()) |
| 275 | { |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 276 | log<level::ERR>("Error in host call response for retrieving property"); |
| 277 | elog<InternalFailure>(); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 278 | } |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 279 | hostResponseMsg.read(value); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 280 | } |
| 281 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 282 | void Zone::handleEvent(sdbusplus::message::message& msg, |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 283 | const EventData* eventData) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 284 | { |
| 285 | // Handle the callback |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 286 | std::get<eventHandlerPos>(*eventData)(_bus, msg, *this); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 287 | // Perform the action |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 288 | std::get<eventActionPos>(*eventData)(*this, |
| 289 | std::get<eventGroupPos>(*eventData)); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 290 | } |
| 291 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |