Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
| 3 | |
| 4 | #include "config.h" |
| 5 | |
| 6 | #include "hiomap.hpp" |
| 7 | |
| 8 | #include <endian.h> |
| 9 | #include <host-ipmid/ipmid-api.h> |
| 10 | #include <string.h> |
| 11 | #include <systemd/sd-bus.h> |
| 12 | |
| 13 | #include <fstream> |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 14 | #include <functional> |
| 15 | #include <host-ipmid/ipmid-host-cmd-utils.hpp> |
| 16 | #include <host-ipmid/ipmid-host-cmd.hpp> |
| 17 | #include <iostream> |
| 18 | #include <phosphor-logging/log.hpp> |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 19 | #include <sdbusplus/bus.hpp> |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 20 | #include <sdbusplus/bus/match.hpp> |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 21 | #include <sdbusplus/exception.hpp> |
| 22 | |
| 23 | using namespace sdbusplus; |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 24 | using namespace phosphor::host::command; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 25 | |
| 26 | static void register_openpower_hiomap_commands() __attribute__((constructor)); |
| 27 | |
| 28 | namespace openpower |
| 29 | { |
| 30 | namespace flash |
| 31 | { |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 32 | constexpr auto BMC_EVENT_DAEMON_READY = 1 << 7; |
| 33 | constexpr auto BMC_EVENT_FLASH_CTRL_LOST = 1 << 6; |
| 34 | constexpr auto BMC_EVENT_WINDOW_RESET = 1 << 1; |
| 35 | constexpr auto BMC_EVENT_PROTOCOL_RESET = 1 << 0; |
| 36 | |
| 37 | constexpr auto IPMI_CMD_HIOMAP_EVENT = 0x0f; |
| 38 | |
| 39 | constexpr auto HIOMAPD_SERVICE = "xyz.openbmc_project.Hiomapd"; |
| 40 | constexpr auto HIOMAPD_OBJECT = "/xyz/openbmc_project/Hiomapd"; |
| 41 | constexpr auto HIOMAPD_IFACE = "xyz.openbmc_project.Hiomapd.Protocol"; |
| 42 | constexpr auto HIOMAPD_IFACE_V2 = "xyz.openbmc_project.Hiomapd.Protocol.V2"; |
| 43 | |
| 44 | constexpr auto DBUS_IFACE_PROPERTIES = "org.freedesktop.DBus.Properties"; |
| 45 | |
| 46 | struct hiomap |
| 47 | { |
| 48 | bus::bus *bus; |
| 49 | |
| 50 | /* Signals */ |
| 51 | bus::match::match *properties; |
| 52 | bus::match::match *window_reset; |
| 53 | bus::match::match *bmc_reboot; |
| 54 | |
| 55 | /* Protocol state */ |
| 56 | std::map<std::string, int> event_lookup; |
| 57 | uint8_t bmc_events; |
| 58 | }; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 59 | |
| 60 | /* TODO: Replace get/put with packed structs and direct assignment */ |
| 61 | template <typename T> static inline T get(void *buf) |
| 62 | { |
| 63 | T t; |
| 64 | memcpy(&t, buf, sizeof(t)); |
| 65 | return t; |
| 66 | } |
| 67 | |
| 68 | template <typename T> static inline void put(void *buf, T &&t) |
| 69 | { |
| 70 | memcpy(buf, &t, sizeof(t)); |
| 71 | } |
| 72 | |
| 73 | typedef ipmi_ret_t (*hiomap_command)(ipmi_request_t req, ipmi_response_t resp, |
| 74 | ipmi_data_len_t data_len, |
| 75 | ipmi_context_t context); |
| 76 | |
| 77 | struct errno_cc_entry |
| 78 | { |
| 79 | int err; |
| 80 | int cc; |
| 81 | }; |
| 82 | |
| 83 | static const errno_cc_entry errno_cc_map[] = { |
| 84 | {0, IPMI_CC_OK}, |
| 85 | {EBUSY, IPMI_CC_BUSY}, |
| 86 | {ENOTSUP, IPMI_CC_INVALID}, |
| 87 | {ETIMEDOUT, 0xc3}, /* FIXME: Replace when defined in ipmid-api.h */ |
| 88 | {ENOSPC, 0xc4}, /* FIXME: Replace when defined in ipmid-api.h */ |
| 89 | {EINVAL, IPMI_CC_PARM_OUT_OF_RANGE}, |
| 90 | {ENODEV, IPMI_CC_SENSOR_INVALID}, |
| 91 | {EPERM, IPMI_CC_INSUFFICIENT_PRIVILEGE}, |
| 92 | {EACCES, IPMI_CC_INSUFFICIENT_PRIVILEGE}, |
| 93 | {-1, IPMI_CC_UNSPECIFIED_ERROR}, |
| 94 | }; |
| 95 | |
| 96 | static int hiomap_xlate_errno(int err) |
| 97 | { |
| 98 | const errno_cc_entry *entry = &errno_cc_map[0]; |
| 99 | |
| 100 | while (!(entry->err == err || entry->err == -1)) |
| 101 | { |
| 102 | entry++; |
| 103 | } |
| 104 | |
| 105 | return entry->cc; |
| 106 | } |
| 107 | |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 108 | static void ipmi_hiomap_event_response(IpmiCmdData cmd, bool status) |
| 109 | { |
| 110 | using namespace phosphor::logging; |
| 111 | |
| 112 | if (!status) |
| 113 | { |
| 114 | log<level::ERR>("Failed to deliver host command", |
| 115 | entry("SEL_COMMAND=%x:%x", cmd.first, cmd.second)); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static int hiomap_handle_property_update(struct hiomap *ctx, |
| 120 | sdbusplus::message::message &msg) |
| 121 | { |
| 122 | std::map<std::string, sdbusplus::message::variant<bool>> msgData; |
| 123 | |
| 124 | std::string iface; |
| 125 | msg.read(iface, msgData); |
| 126 | |
| 127 | for (auto const &x : msgData) |
| 128 | { |
| 129 | if (!ctx->event_lookup.count(x.first)) |
| 130 | { |
| 131 | /* Unsupported event? */ |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | uint8_t mask = ctx->event_lookup[x.first]; |
| 136 | auto value = sdbusplus::message::variant_ns::get<bool>(x.second); |
| 137 | |
| 138 | if (value) |
| 139 | { |
| 140 | ctx->bmc_events |= mask; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | ctx->bmc_events &= ~mask; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | auto cmd = std::make_pair(IPMI_CMD_HIOMAP_EVENT, ctx->bmc_events); |
| 149 | |
| 150 | ipmid_send_cmd_to_host(std::make_tuple(cmd, ipmi_hiomap_event_response)); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static bus::match::match hiomap_match_properties(struct hiomap *ctx) |
| 156 | { |
| 157 | auto properties = |
| 158 | bus::match::rules::propertiesChanged(HIOMAPD_OBJECT, HIOMAPD_IFACE_V2); |
| 159 | |
| 160 | bus::match::match match( |
| 161 | *ctx->bus, properties, |
| 162 | std::bind(hiomap_handle_property_update, ctx, std::placeholders::_1)); |
| 163 | |
| 164 | return match; |
| 165 | } |
| 166 | |
| 167 | static int hiomap_handle_signal_v2(struct hiomap *ctx, const char *name) |
| 168 | { |
| 169 | ctx->bmc_events |= ctx->event_lookup[name]; |
| 170 | |
| 171 | auto cmd = std::make_pair(IPMI_CMD_HIOMAP_EVENT, ctx->bmc_events); |
| 172 | |
| 173 | ipmid_send_cmd_to_host(std::make_tuple(cmd, ipmi_hiomap_event_response)); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static bus::match::match hiomap_match_signal_v2(struct hiomap *ctx, |
| 179 | const char *name) |
| 180 | { |
| 181 | using namespace bus::match; |
| 182 | |
| 183 | auto signals = rules::type::signal() + rules::path(HIOMAPD_OBJECT) + |
| 184 | rules::interface(HIOMAPD_IFACE_V2) + rules::member(name); |
| 185 | |
| 186 | bus::match::match match(*ctx->bus, signals, |
| 187 | std::bind(hiomap_handle_signal_v2, ctx, name)); |
| 188 | |
| 189 | return match; |
| 190 | } |
| 191 | |
| 192 | static ipmi_ret_t hiomap_reset(ipmi_request_t request, ipmi_response_t response, |
| 193 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 194 | { |
| 195 | struct hiomap *ctx = static_cast<struct hiomap *>(context); |
| 196 | |
| 197 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 198 | HIOMAPD_IFACE, "Reset"); |
| 199 | try |
| 200 | { |
| 201 | ctx->bus->call(m); |
| 202 | |
| 203 | *data_len = 0; |
| 204 | } |
| 205 | catch (const exception::SdBusError &e) |
| 206 | { |
| 207 | return hiomap_xlate_errno(e.get_errno()); |
| 208 | } |
| 209 | |
| 210 | return IPMI_CC_OK; |
| 211 | } |
| 212 | |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 213 | static ipmi_ret_t hiomap_get_info(ipmi_request_t request, |
| 214 | ipmi_response_t response, |
| 215 | ipmi_data_len_t data_len, |
| 216 | ipmi_context_t context) |
| 217 | { |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 218 | struct hiomap *ctx = static_cast<struct hiomap *>(context); |
| 219 | |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 220 | if (*data_len < 1) |
| 221 | { |
| 222 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 223 | } |
| 224 | |
| 225 | uint8_t *reqdata = (uint8_t *)request; |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 226 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 227 | HIOMAPD_IFACE, "GetInfo"); |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 228 | m.append(reqdata[0]); |
| 229 | |
| 230 | try |
| 231 | { |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 232 | auto reply = ctx->bus->call(m); |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 233 | |
| 234 | uint8_t version; |
| 235 | uint8_t blockSizeShift; |
| 236 | uint16_t timeout; |
| 237 | reply.read(version, blockSizeShift, timeout); |
| 238 | |
| 239 | uint8_t *respdata = (uint8_t *)response; |
| 240 | |
| 241 | /* FIXME: Assumes v2! */ |
| 242 | put(&respdata[0], version); |
| 243 | put(&respdata[1], blockSizeShift); |
| 244 | put(&respdata[2], htole16(timeout)); |
| 245 | |
| 246 | *data_len = 4; |
| 247 | } |
| 248 | catch (const exception::SdBusError &e) |
| 249 | { |
| 250 | return hiomap_xlate_errno(e.get_errno()); |
| 251 | } |
| 252 | |
| 253 | return IPMI_CC_OK; |
| 254 | } |
| 255 | |
| 256 | static const hiomap_command hiomap_commands[] = { |
| 257 | [0] = NULL, /* 0 is an invalid command ID */ |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 258 | [1] = hiomap_reset, |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 259 | [2] = hiomap_get_info, |
| 260 | }; |
| 261 | |
| 262 | /* FIXME: Define this in the "right" place, wherever that is */ |
| 263 | /* FIXME: Double evaluation */ |
| 264 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 265 | |
| 266 | static ipmi_ret_t hiomap_dispatch(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 267 | ipmi_request_t request, |
| 268 | ipmi_response_t response, |
| 269 | ipmi_data_len_t data_len, |
| 270 | ipmi_context_t context) |
| 271 | { |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 272 | struct hiomap *ctx = static_cast<struct hiomap *>(context); |
| 273 | |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 274 | if (*data_len < 2) |
| 275 | { |
| 276 | *data_len = 0; |
| 277 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 278 | } |
| 279 | |
| 280 | uint8_t *ipmi_req = (uint8_t *)request; |
| 281 | uint8_t *ipmi_resp = (uint8_t *)response; |
| 282 | uint8_t hiomap_cmd = ipmi_req[0]; |
| 283 | |
| 284 | if (hiomap_cmd == 0 || hiomap_cmd > ARRAY_SIZE(hiomap_commands) - 1) |
| 285 | { |
| 286 | *data_len = 0; |
| 287 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 288 | } |
| 289 | uint8_t *flash_req = ipmi_req + 2; |
| 290 | size_t flash_len = *data_len - 2; |
| 291 | uint8_t *flash_resp = ipmi_resp + 2; |
| 292 | |
| 293 | ipmi_ret_t cc = |
| 294 | hiomap_commands[hiomap_cmd](flash_req, flash_resp, &flash_len, context); |
| 295 | if (cc != IPMI_CC_OK) |
| 296 | { |
| 297 | *data_len = 0; |
| 298 | return cc; |
| 299 | } |
| 300 | |
| 301 | /* Populate the response command and sequence */ |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 302 | ipmi_resp[0] = hiomap_cmd; |
| 303 | ipmi_resp[1] = ipmi_req[1]; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 304 | |
| 305 | *data_len = flash_len + 2; |
| 306 | |
| 307 | return cc; |
| 308 | } |
| 309 | } // namespace flash |
| 310 | } // namespace openpower |
| 311 | |
| 312 | static void register_openpower_hiomap_commands() |
| 313 | { |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame^] | 314 | using namespace openpower::flash; |
| 315 | |
| 316 | /* FIXME: Clean this up? Can we unregister? */ |
| 317 | struct hiomap *ctx = new hiomap(); |
| 318 | |
| 319 | /* Initialise mapping from signal and property names to status bit */ |
| 320 | ctx->event_lookup["DaemonReady"] = BMC_EVENT_DAEMON_READY; |
| 321 | ctx->event_lookup["FlashControlLost"] = BMC_EVENT_FLASH_CTRL_LOST; |
| 322 | ctx->event_lookup["WindowReset"] = BMC_EVENT_WINDOW_RESET; |
| 323 | ctx->event_lookup["ProtocolReset"] = BMC_EVENT_PROTOCOL_RESET; |
| 324 | |
| 325 | ctx->bus = new bus::bus(ipmid_get_sd_bus_connection()); |
| 326 | |
| 327 | /* Initialise signal handling */ |
| 328 | |
| 329 | /* |
| 330 | * Can't use temporaries here because that causes SEGFAULTs due to slot |
| 331 | * destruction (!?), so enjoy the weird wrapping. |
| 332 | */ |
| 333 | ctx->properties = |
| 334 | new bus::match::match(std::move(hiomap_match_properties(ctx))); |
| 335 | ctx->bmc_reboot = new bus::match::match( |
| 336 | std::move(hiomap_match_signal_v2(ctx, "ProtocolReset"))); |
| 337 | ctx->window_reset = new bus::match::match( |
| 338 | std::move(hiomap_match_signal_v2(ctx, "WindowReset"))); |
| 339 | |
| 340 | ipmi_register_callback(NETFUN_IBM_OEM, IPMI_CMD_HIOMAP, ctx, |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 341 | openpower::flash::hiomap_dispatch, SYSTEM_INTERFACE); |
| 342 | } |