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)), |
| 45 | _decTimer(events, [this](){ this->decTimerExpired(); }) |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 46 | { |
| 47 | auto& fanDefs = std::get<fanListPos>(def); |
| 48 | |
| 49 | for (auto& def : fanDefs) |
| 50 | { |
| 51 | _fans.emplace_back(std::make_unique<Fan>(bus, def)); |
| 52 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 53 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 54 | // Do not enable set speed events when in init mode |
| 55 | if (mode != Mode::init) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 56 | { |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame^] | 57 | initEvents(def); |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 58 | // Start timer for fan speed decreases |
| 59 | if (!_decTimer.running()) |
| 60 | { |
| 61 | //TODO Update time value to what's given in zones yaml |
| 62 | _decTimer.start(seconds(30), |
| 63 | phosphor::fan::util::Timer::TimerType::repeating); |
| 64 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 65 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
| 69 | void Zone::setSpeed(uint64_t speed) |
| 70 | { |
| 71 | for (auto& fan : _fans) |
| 72 | { |
| 73 | fan->setSpeed(speed); |
| 74 | } |
| 75 | } |
| 76 | |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 77 | void Zone::setActiveAllow(const Group* group, bool isActiveAllow) |
| 78 | { |
| 79 | _active[group] = isActiveAllow; |
| 80 | if (!isActiveAllow) |
| 81 | { |
| 82 | _isActive = false; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | // Check all entries are set to allow control active |
| 87 | auto actPred = [](auto const& entry) {return entry.second;}; |
| 88 | _isActive = std::all_of(_active.begin(), |
| 89 | _active.end(), |
| 90 | actPred); |
| 91 | } |
| 92 | } |
| 93 | |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 94 | void Zone::requestSpeedIncrease(uint64_t targetDelta) |
| 95 | { |
| 96 | // Only increase speed when delta is higher than |
| 97 | // the current increase delta for the zone and currently under ceiling |
| 98 | if (targetDelta > _incSpeedDelta && |
| 99 | _targetSpeed < _ceilingSpeed) |
| 100 | { |
| 101 | _targetSpeed = (targetDelta - _incSpeedDelta) + _targetSpeed; |
| 102 | _incSpeedDelta = targetDelta; |
| 103 | //TODO openbmc/openbmc#1625 Cancel current timer countdown |
| 104 | //TODO Floor speed above target, update target to floor speed |
| 105 | if (_targetSpeed < _floorSpeed) |
| 106 | { |
| 107 | _targetSpeed = _floorSpeed; |
| 108 | } |
| 109 | // Target speed can not go above a defined ceiling speed |
| 110 | if (_targetSpeed > _ceilingSpeed) |
| 111 | { |
| 112 | _targetSpeed = _ceilingSpeed; |
| 113 | } |
| 114 | |
| 115 | setSpeed(_targetSpeed); |
| 116 | //TODO openbmc/openbmc#1625 Start timer countdown for fan speed increase |
| 117 | } |
| 118 | //TODO openbmc/openbmc#1625 Clear increase delta when timer expires |
| 119 | _incSpeedDelta = 0; |
| 120 | } |
| 121 | |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 122 | void Zone::requestSpeedDecrease(uint64_t targetDelta) |
| 123 | { |
| 124 | // Only decrease the lowest target delta requested |
| 125 | if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta) |
| 126 | { |
| 127 | _decSpeedDelta = targetDelta; |
| 128 | } |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 129 | } |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 130 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 131 | void Zone::decTimerExpired() |
| 132 | { |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 133 | // Only decrease speeds when no requested increases exist |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 134 | //TODO Add increase timer not running (i.e. not in the middle of increasing) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 135 | if (_incSpeedDelta == 0) |
| 136 | { |
| 137 | // Target speed can not go below the defined floor speed |
| 138 | if ((_targetSpeed < _decSpeedDelta) || |
| 139 | (_targetSpeed - _decSpeedDelta < _floorSpeed)) |
| 140 | { |
| 141 | _targetSpeed = _floorSpeed; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | _targetSpeed = _targetSpeed - _decSpeedDelta; |
| 146 | } |
| 147 | setSpeed(_targetSpeed); |
| 148 | } |
| 149 | // Clear decrease delta when timer expires |
| 150 | _decSpeedDelta = 0; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 151 | // Decrease timer is restarted since its repeating |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame^] | 154 | void Zone::initEvents(const ZoneDefinition& def) |
| 155 | { |
| 156 | // Setup signal trigger for set speed events |
| 157 | for (auto& event : std::get<setSpeedEventsPos>(def)) |
| 158 | { |
| 159 | // Get the current value for each property |
| 160 | for (auto& entry : std::get<groupPos>(event)) |
| 161 | { |
| 162 | refreshProperty(_bus, |
| 163 | entry.first, |
| 164 | std::get<intfPos>(entry.second), |
| 165 | std::get<propPos>(entry.second)); |
| 166 | } |
| 167 | // Setup signal matches for property change events |
| 168 | for (auto& prop : std::get<propChangeListPos>(event)) |
| 169 | { |
| 170 | _signalEvents.emplace_back( |
| 171 | std::make_unique<EventData>( |
| 172 | EventData |
| 173 | { |
| 174 | std::get<groupPos>(event), |
| 175 | std::get<handlerObjPos>(prop), |
| 176 | std::get<actionPos>(event) |
| 177 | })); |
| 178 | _matches.emplace_back( |
| 179 | _bus, |
| 180 | std::get<signaturePos>(prop).c_str(), |
| 181 | std::bind(std::mem_fn(&Zone::handleEvent), |
| 182 | this, |
| 183 | std::placeholders::_1, |
| 184 | _signalEvents.back().get())); |
| 185 | } |
| 186 | // Run action function for initial event state |
| 187 | std::get<actionPos>(event)(*this, |
| 188 | std::get<groupPos>(event)); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void Zone::refreshProperty(sdbusplus::bus::bus& bus, |
| 193 | const std::string& path, |
| 194 | const std::string& iface, |
| 195 | const std::string& prop) |
| 196 | { |
| 197 | PropertyVariantType property; |
| 198 | getProperty(_bus, path, iface, prop, property); |
| 199 | setPropertyValue(path.c_str(), iface.c_str(), prop.c_str(), property); |
| 200 | } |
| 201 | |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 202 | void Zone::getProperty(sdbusplus::bus::bus& bus, |
| 203 | const std::string& path, |
| 204 | const std::string& iface, |
| 205 | const std::string& prop, |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 206 | PropertyVariantType& value) |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 207 | { |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 208 | auto serv = phosphor::fan::util::getService(path, iface, bus); |
| 209 | auto hostCall = bus.new_method_call(serv.c_str(), |
| 210 | path.c_str(), |
| 211 | "org.freedesktop.DBus.Properties", |
| 212 | "Get"); |
| 213 | hostCall.append(iface); |
| 214 | hostCall.append(prop); |
| 215 | auto hostResponseMsg = bus.call(hostCall); |
| 216 | if (hostResponseMsg.is_method_error()) |
| 217 | { |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 218 | log<level::ERR>("Error in host call response for retrieving property"); |
| 219 | elog<InternalFailure>(); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 220 | } |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 221 | hostResponseMsg.read(value); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 224 | void Zone::handleEvent(sdbusplus::message::message& msg, |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 225 | const EventData* eventData) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 226 | { |
| 227 | // Handle the callback |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 228 | std::get<eventHandlerPos>(*eventData)(_bus, msg, *this); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 229 | // Perform the action |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 230 | std::get<eventActionPos>(*eventData)(*this, |
| 231 | std::get<eventGroupPos>(*eventData)); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 232 | } |
| 233 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |