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 | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 100 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | void Zone::setSpeed(uint64_t speed) |
| 105 | { |
| 106 | for (auto& fan : _fans) |
| 107 | { |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 108 | //TODO openbmc/openbmc#1626 Move to control algorithm function |
| 109 | if (speed < _floorSpeed) |
| 110 | { |
| 111 | speed = _floorSpeed; |
| 112 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 113 | fan->setSpeed(speed); |
| 114 | } |
| 115 | } |
| 116 | |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 117 | void Zone::setActiveAllow(const Group* group, bool isActiveAllow) |
| 118 | { |
| 119 | _active[group] = isActiveAllow; |
| 120 | if (!isActiveAllow) |
| 121 | { |
| 122 | _isActive = false; |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | // Check all entries are set to allow control active |
| 127 | auto actPred = [](auto const& entry) {return entry.second;}; |
| 128 | _isActive = std::all_of(_active.begin(), |
| 129 | _active.end(), |
| 130 | actPred); |
| 131 | } |
| 132 | } |
| 133 | |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame^] | 134 | void Zone::requestSpeedIncrease(uint64_t targetDelta) |
| 135 | { |
| 136 | // Only increase speed when delta is higher than |
| 137 | // the current increase delta for the zone and currently under ceiling |
| 138 | if (targetDelta > _incSpeedDelta && |
| 139 | _targetSpeed < _ceilingSpeed) |
| 140 | { |
| 141 | _targetSpeed = (targetDelta - _incSpeedDelta) + _targetSpeed; |
| 142 | _incSpeedDelta = targetDelta; |
| 143 | //TODO openbmc/openbmc#1625 Cancel current timer countdown |
| 144 | //TODO Floor speed above target, update target to floor speed |
| 145 | if (_targetSpeed < _floorSpeed) |
| 146 | { |
| 147 | _targetSpeed = _floorSpeed; |
| 148 | } |
| 149 | // Target speed can not go above a defined ceiling speed |
| 150 | if (_targetSpeed > _ceilingSpeed) |
| 151 | { |
| 152 | _targetSpeed = _ceilingSpeed; |
| 153 | } |
| 154 | |
| 155 | setSpeed(_targetSpeed); |
| 156 | //TODO openbmc/openbmc#1625 Start timer countdown for fan speed increase |
| 157 | } |
| 158 | //TODO openbmc/openbmc#1625 Clear increase delta when timer expires |
| 159 | _incSpeedDelta = 0; |
| 160 | } |
| 161 | |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 162 | void Zone::getProperty(sdbusplus::bus::bus& bus, |
| 163 | const std::string& path, |
| 164 | const std::string& iface, |
| 165 | const std::string& prop, |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 166 | PropertyVariantType& value) |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 167 | { |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 168 | auto serv = phosphor::fan::util::getService(path, iface, bus); |
| 169 | auto hostCall = bus.new_method_call(serv.c_str(), |
| 170 | path.c_str(), |
| 171 | "org.freedesktop.DBus.Properties", |
| 172 | "Get"); |
| 173 | hostCall.append(iface); |
| 174 | hostCall.append(prop); |
| 175 | auto hostResponseMsg = bus.call(hostCall); |
| 176 | if (hostResponseMsg.is_method_error()) |
| 177 | { |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 178 | log<level::ERR>("Error in host call response for retrieving property"); |
| 179 | elog<InternalFailure>(); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 180 | } |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 181 | hostResponseMsg.read(value); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 184 | void Zone::handleEvent(sdbusplus::message::message& msg, |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 185 | const EventData* eventData) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 186 | { |
| 187 | // Handle the callback |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 188 | std::get<eventHandlerPos>(*eventData)(_bus, msg, *this); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 189 | // Perform the action |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 190 | std::get<eventActionPos>(*eventData)(*this, |
| 191 | std::get<eventGroupPos>(*eventData)); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |