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