Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 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 | */ |
| 16 | |
Patrick Venture | 07c3a80 | 2018-09-19 10:06:30 -0700 | [diff] [blame] | 17 | #include <host-ipmid/ipmid-api.h> |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 18 | |
Patrick Venture | 07c3a80 | 2018-09-19 10:06:30 -0700 | [diff] [blame] | 19 | #include <host-ipmid/iana.hpp> |
| 20 | #include <host-ipmid/oemopenbmc.hpp> |
| 21 | #include <host-ipmid/oemrouter.hpp> |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 22 | #include <map> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 23 | #include <sdbusplus/bus.hpp> |
| 24 | #include <sdbusplus/message.hpp> |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <tuple> |
| 27 | |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 28 | enum ManualSubCmd |
| 29 | { |
| 30 | GET_CONTROL_STATE = 0, |
| 31 | SET_CONTROL_STATE = 1, |
| 32 | GET_FAILSAFE_STATE = 2, |
| 33 | }; |
| 34 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 35 | struct FanCtrlRequest |
| 36 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 37 | uint8_t command; |
| 38 | uint8_t zone; |
| 39 | } __attribute__((packed)); |
| 40 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 41 | struct FanCtrlRequestSet |
| 42 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 43 | uint8_t command; |
| 44 | uint8_t zone; |
| 45 | uint8_t value; |
| 46 | } __attribute__((packed)); |
| 47 | |
| 48 | static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone"; |
| 49 | static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl"; |
Patrick Venture | dc3b790 | 2018-03-24 10:41:19 -0700 | [diff] [blame] | 50 | static constexpr auto intf = "xyz.openbmc_project.Control.Mode"; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 51 | static constexpr auto manualProperty = "Manual"; |
| 52 | static constexpr auto failsafeProperty = "FailSafe"; |
| 53 | static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties"; |
| 54 | |
| 55 | using Property = std::string; |
| 56 | using Value = sdbusplus::message::variant<bool>; |
| 57 | using PropertyMap = std::map<Property, Value>; |
| 58 | |
| 59 | /* The following was copied directly from my manual thread handler. */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 60 | static std::string getControlPath(int8_t zone) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 61 | { |
| 62 | return std::string(objectPath) + std::to_string(zone); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * busctl call xyz.openbmc_project.State.FanCtrl \ |
| 67 | * /xyz/openbmc_project/settings/fanctrl/zone1 \ |
| 68 | * org.freedesktop.DBus.Properties \ |
| 69 | * GetAll \ |
| 70 | * s \ |
Patrick Venture | dc3b790 | 2018-03-24 10:41:19 -0700 | [diff] [blame] | 71 | * xyz.openbmc_project.Control.Mode |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 72 | * a{sv} 2 "Manual" b false "FailSafe" b false |
| 73 | */ |
| 74 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 75 | static ipmi_ret_t getFanCtrlProperty(uint8_t zoneId, bool* value, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 76 | const std::string& property) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 77 | { |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 78 | std::string path = getControlPath(zoneId); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 79 | |
| 80 | auto propertyReadBus = sdbusplus::bus::new_default(); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 81 | auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(), |
| 82 | propertiesintf, "GetAll"); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 83 | pimMsg.append(intf); |
| 84 | |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 85 | try |
| 86 | { |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 87 | PropertyMap propMap; |
Patrick Venture | 8f17914 | 2018-09-10 09:24:12 -0700 | [diff] [blame] | 88 | |
| 89 | /* a method could error but the call not error. */ |
| 90 | auto valueResponseMsg = propertyReadBus.call(pimMsg); |
Patrick Venture | 8f17914 | 2018-09-10 09:24:12 -0700 | [diff] [blame] | 91 | |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 92 | valueResponseMsg.read(propMap); |
| 93 | |
| 94 | *value = sdbusplus::message::variant_ns::get<bool>(propMap[property]); |
| 95 | } |
| 96 | catch (const sdbusplus::exception::SdBusError& ex) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 97 | { |
| 98 | return IPMI_CC_INVALID; |
| 99 | } |
| 100 | |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 101 | return IPMI_CC_OK; |
| 102 | } |
| 103 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 104 | static ipmi_ret_t getFailsafeModeState(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 105 | size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 106 | { |
| 107 | ipmi_ret_t rc = IPMI_CC_OK; |
| 108 | bool current; |
| 109 | |
| 110 | if (*dataLen < sizeof(struct FanCtrlRequest)) |
| 111 | { |
| 112 | return IPMI_CC_INVALID; |
| 113 | } |
| 114 | |
| 115 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 116 | reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 117 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 118 | rc = getFanCtrlProperty(request->zone, ¤t, failsafeProperty); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 119 | if (rc) |
| 120 | { |
| 121 | return rc; |
| 122 | } |
| 123 | |
| 124 | *replyBuf = (uint8_t)current; |
| 125 | *dataLen = sizeof(uint8_t); |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * <method name="GetAll"> |
| 131 | * <arg name="interface" direction="in" type="s"/> |
| 132 | * <arg name="properties" direction="out" type="a{sv}"/> |
| 133 | * </method> |
| 134 | */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 135 | static ipmi_ret_t getManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 136 | size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 137 | { |
| 138 | ipmi_ret_t rc = IPMI_CC_OK; |
| 139 | bool current; |
| 140 | |
| 141 | if (*dataLen < sizeof(struct FanCtrlRequest)) |
| 142 | { |
| 143 | return IPMI_CC_INVALID; |
| 144 | } |
| 145 | |
| 146 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 147 | reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 148 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 149 | rc = getFanCtrlProperty(request->zone, ¤t, manualProperty); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 150 | if (rc) |
| 151 | { |
| 152 | return rc; |
| 153 | } |
| 154 | |
| 155 | *replyBuf = (uint8_t)current; |
| 156 | *dataLen = sizeof(uint8_t); |
| 157 | return rc; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * <method name="Set"> |
| 162 | * <arg name="interface" direction="in" type="s"/> |
| 163 | * <arg name="property" direction="in" type="s"/> |
| 164 | * <arg name="value" direction="in" type="v"/> |
| 165 | * </method> |
| 166 | */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 167 | static ipmi_ret_t setManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 168 | size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 169 | { |
| 170 | ipmi_ret_t rc = IPMI_CC_OK; |
| 171 | if (*dataLen < sizeof(struct FanCtrlRequestSet)) |
| 172 | { |
| 173 | return IPMI_CC_INVALID; |
| 174 | } |
| 175 | |
| 176 | using Value = sdbusplus::message::variant<bool>; |
| 177 | |
| 178 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 179 | reinterpret_cast<const struct FanCtrlRequestSet*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 180 | |
| 181 | /* 0 is false, 1 is true */ |
| 182 | bool setValue = static_cast<bool>(request->value); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 183 | Value v{setValue}; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 184 | |
| 185 | auto PropertyWriteBus = sdbusplus::bus::new_default(); |
| 186 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 187 | std::string path = getControlPath(request->zone); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 188 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 189 | auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(), |
| 190 | propertiesintf, "Set"); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 191 | pimMsg.append(intf); |
| 192 | pimMsg.append(manualProperty); |
| 193 | pimMsg.append(v); |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 194 | |
| 195 | try |
| 196 | { |
| 197 | PropertyWriteBus.call_noreply(pimMsg); |
| 198 | } |
| 199 | catch (const sdbusplus::exception::SdBusError& ex) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 200 | { |
| 201 | rc = IPMI_CC_INVALID; |
| 202 | } |
| 203 | /* TODO(venture): Should sanity check the result. */ |
| 204 | |
| 205 | return rc; |
| 206 | } |
| 207 | |
| 208 | /* Three command packages: get, set true, set false */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 209 | static ipmi_ret_t manualModeControl(ipmi_cmd_t cmd, const uint8_t* reqBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 210 | uint8_t* replyCmdBuf, size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 211 | { |
| 212 | ipmi_ret_t rc = IPMI_CC_OK; |
| 213 | // FanCtrlRequest is the smaller of the requests, so it's at a minimum. |
| 214 | if (*dataLen < sizeof(struct FanCtrlRequest)) |
| 215 | { |
| 216 | return IPMI_CC_INVALID; |
| 217 | } |
| 218 | |
| 219 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 220 | reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 221 | |
| 222 | switch (request->command) |
| 223 | { |
| 224 | case GET_CONTROL_STATE: |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 225 | return getManualModeState(reqBuf, replyCmdBuf, dataLen); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 226 | case SET_CONTROL_STATE: |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 227 | return setManualModeState(reqBuf, replyCmdBuf, dataLen); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 228 | case GET_FAILSAFE_STATE: |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 229 | return getFailsafeModeState(reqBuf, replyCmdBuf, dataLen); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 230 | default: |
| 231 | rc = IPMI_CC_INVALID; |
| 232 | } |
| 233 | |
| 234 | return rc; |
| 235 | } |
| 236 | |
| 237 | void setupGlobalOemFanControl() __attribute__((constructor)); |
| 238 | |
| 239 | void setupGlobalOemFanControl() |
| 240 | { |
Patrick Venture | ba00343 | 2018-07-27 06:59:05 -0700 | [diff] [blame] | 241 | oem::Router* router = oem::mutableRouter(); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 242 | |
| 243 | fprintf(stderr, |
| 244 | "Registering OEM:[%#08X], Cmd:[%#04X] for Manual Zone Control\n", |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 245 | oem::obmcOemNumber, oem::Cmd::fanManualCmd); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 246 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 247 | router->registerHandler(oem::obmcOemNumber, oem::Cmd::fanManualCmd, |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 248 | manualModeControl); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 249 | } |