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> |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 10 | |
Patrick Venture | ee70196 | 2018-10-31 14:51:37 -0700 | [diff] [blame^] | 11 | #include <cstring> |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 12 | #include <fstream> |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 13 | #include <functional> |
| 14 | #include <host-ipmid/ipmid-host-cmd-utils.hpp> |
| 15 | #include <host-ipmid/ipmid-host-cmd.hpp> |
| 16 | #include <iostream> |
Patrick Venture | ee70196 | 2018-10-31 14:51:37 -0700 | [diff] [blame^] | 17 | #include <map> |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 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> |
Patrick Venture | ee70196 | 2018-10-31 14:51:37 -0700 | [diff] [blame^] | 22 | #include <string> |
| 23 | #include <tuple> |
| 24 | #include <utility> |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 25 | |
| 26 | using namespace sdbusplus; |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 27 | using namespace phosphor::host::command; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 28 | |
| 29 | static void register_openpower_hiomap_commands() __attribute__((constructor)); |
| 30 | |
| 31 | namespace openpower |
| 32 | { |
| 33 | namespace flash |
| 34 | { |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 35 | constexpr auto BMC_EVENT_DAEMON_READY = 1 << 7; |
| 36 | constexpr auto BMC_EVENT_FLASH_CTRL_LOST = 1 << 6; |
| 37 | constexpr auto BMC_EVENT_WINDOW_RESET = 1 << 1; |
| 38 | constexpr auto BMC_EVENT_PROTOCOL_RESET = 1 << 0; |
| 39 | |
| 40 | constexpr auto IPMI_CMD_HIOMAP_EVENT = 0x0f; |
| 41 | |
| 42 | constexpr auto HIOMAPD_SERVICE = "xyz.openbmc_project.Hiomapd"; |
| 43 | constexpr auto HIOMAPD_OBJECT = "/xyz/openbmc_project/Hiomapd"; |
| 44 | constexpr auto HIOMAPD_IFACE = "xyz.openbmc_project.Hiomapd.Protocol"; |
| 45 | constexpr auto HIOMAPD_IFACE_V2 = "xyz.openbmc_project.Hiomapd.Protocol.V2"; |
| 46 | |
| 47 | constexpr auto DBUS_IFACE_PROPERTIES = "org.freedesktop.DBus.Properties"; |
| 48 | |
| 49 | struct hiomap |
| 50 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 51 | bus::bus* bus; |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 52 | |
| 53 | /* Signals */ |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 54 | bus::match::match* properties; |
| 55 | bus::match::match* window_reset; |
| 56 | bus::match::match* bmc_reboot; |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 57 | |
| 58 | /* Protocol state */ |
| 59 | std::map<std::string, int> event_lookup; |
| 60 | uint8_t bmc_events; |
Andrew Jeffery | 04d7513 | 2018-09-26 00:58:52 +0930 | [diff] [blame] | 61 | uint8_t seq; |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 62 | }; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 63 | |
| 64 | /* TODO: Replace get/put with packed structs and direct assignment */ |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 65 | template <typename T> |
| 66 | static inline T get(void* buf) |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 67 | { |
| 68 | T t; |
Patrick Venture | ee70196 | 2018-10-31 14:51:37 -0700 | [diff] [blame^] | 69 | std::memcpy(&t, buf, sizeof(t)); |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 70 | return t; |
| 71 | } |
| 72 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 73 | template <typename T> |
| 74 | static inline void put(void* buf, T&& t) |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 75 | { |
Patrick Venture | ee70196 | 2018-10-31 14:51:37 -0700 | [diff] [blame^] | 76 | std::memcpy(buf, &t, sizeof(t)); |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | typedef ipmi_ret_t (*hiomap_command)(ipmi_request_t req, ipmi_response_t resp, |
| 80 | ipmi_data_len_t data_len, |
| 81 | ipmi_context_t context); |
| 82 | |
| 83 | struct errno_cc_entry |
| 84 | { |
| 85 | int err; |
| 86 | int cc; |
| 87 | }; |
| 88 | |
| 89 | static const errno_cc_entry errno_cc_map[] = { |
| 90 | {0, IPMI_CC_OK}, |
| 91 | {EBUSY, IPMI_CC_BUSY}, |
| 92 | {ENOTSUP, IPMI_CC_INVALID}, |
| 93 | {ETIMEDOUT, 0xc3}, /* FIXME: Replace when defined in ipmid-api.h */ |
| 94 | {ENOSPC, 0xc4}, /* FIXME: Replace when defined in ipmid-api.h */ |
| 95 | {EINVAL, IPMI_CC_PARM_OUT_OF_RANGE}, |
| 96 | {ENODEV, IPMI_CC_SENSOR_INVALID}, |
| 97 | {EPERM, IPMI_CC_INSUFFICIENT_PRIVILEGE}, |
| 98 | {EACCES, IPMI_CC_INSUFFICIENT_PRIVILEGE}, |
| 99 | {-1, IPMI_CC_UNSPECIFIED_ERROR}, |
| 100 | }; |
| 101 | |
| 102 | static int hiomap_xlate_errno(int err) |
| 103 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 104 | const errno_cc_entry* entry = &errno_cc_map[0]; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 105 | |
| 106 | while (!(entry->err == err || entry->err == -1)) |
| 107 | { |
| 108 | entry++; |
| 109 | } |
| 110 | |
| 111 | return entry->cc; |
| 112 | } |
| 113 | |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 114 | static void ipmi_hiomap_event_response(IpmiCmdData cmd, bool status) |
| 115 | { |
| 116 | using namespace phosphor::logging; |
| 117 | |
| 118 | if (!status) |
| 119 | { |
| 120 | log<level::ERR>("Failed to deliver host command", |
| 121 | entry("SEL_COMMAND=%x:%x", cmd.first, cmd.second)); |
| 122 | } |
| 123 | } |
| 124 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 125 | static int hiomap_handle_property_update(struct hiomap* ctx, |
| 126 | sdbusplus::message::message& msg) |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 127 | { |
| 128 | std::map<std::string, sdbusplus::message::variant<bool>> msgData; |
| 129 | |
| 130 | std::string iface; |
| 131 | msg.read(iface, msgData); |
| 132 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 133 | for (auto const& x : msgData) |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 134 | { |
| 135 | if (!ctx->event_lookup.count(x.first)) |
| 136 | { |
| 137 | /* Unsupported event? */ |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | uint8_t mask = ctx->event_lookup[x.first]; |
| 142 | auto value = sdbusplus::message::variant_ns::get<bool>(x.second); |
| 143 | |
| 144 | if (value) |
| 145 | { |
| 146 | ctx->bmc_events |= mask; |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | ctx->bmc_events &= ~mask; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | auto cmd = std::make_pair(IPMI_CMD_HIOMAP_EVENT, ctx->bmc_events); |
| 155 | |
| 156 | ipmid_send_cmd_to_host(std::make_tuple(cmd, ipmi_hiomap_event_response)); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 161 | static bus::match::match hiomap_match_properties(struct hiomap* ctx) |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 162 | { |
| 163 | auto properties = |
| 164 | bus::match::rules::propertiesChanged(HIOMAPD_OBJECT, HIOMAPD_IFACE_V2); |
| 165 | |
| 166 | bus::match::match match( |
| 167 | *ctx->bus, properties, |
| 168 | std::bind(hiomap_handle_property_update, ctx, std::placeholders::_1)); |
| 169 | |
| 170 | return match; |
| 171 | } |
| 172 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 173 | static int hiomap_handle_signal_v2(struct hiomap* ctx, const char* name) |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 174 | { |
| 175 | ctx->bmc_events |= ctx->event_lookup[name]; |
| 176 | |
| 177 | auto cmd = std::make_pair(IPMI_CMD_HIOMAP_EVENT, ctx->bmc_events); |
| 178 | |
| 179 | ipmid_send_cmd_to_host(std::make_tuple(cmd, ipmi_hiomap_event_response)); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 184 | static bus::match::match hiomap_match_signal_v2(struct hiomap* ctx, |
| 185 | const char* name) |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 186 | { |
| 187 | using namespace bus::match; |
| 188 | |
| 189 | auto signals = rules::type::signal() + rules::path(HIOMAPD_OBJECT) + |
| 190 | rules::interface(HIOMAPD_IFACE_V2) + rules::member(name); |
| 191 | |
| 192 | bus::match::match match(*ctx->bus, signals, |
| 193 | std::bind(hiomap_handle_signal_v2, ctx, name)); |
| 194 | |
| 195 | return match; |
| 196 | } |
| 197 | |
| 198 | static ipmi_ret_t hiomap_reset(ipmi_request_t request, ipmi_response_t response, |
| 199 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 200 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 201 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 202 | |
| 203 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 204 | HIOMAPD_IFACE, "Reset"); |
| 205 | try |
| 206 | { |
| 207 | ctx->bus->call(m); |
| 208 | |
| 209 | *data_len = 0; |
| 210 | } |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 211 | catch (const exception::SdBusError& e) |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 212 | { |
| 213 | return hiomap_xlate_errno(e.get_errno()); |
| 214 | } |
| 215 | |
| 216 | return IPMI_CC_OK; |
| 217 | } |
| 218 | |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 219 | static ipmi_ret_t hiomap_get_info(ipmi_request_t request, |
| 220 | ipmi_response_t response, |
| 221 | ipmi_data_len_t data_len, |
| 222 | ipmi_context_t context) |
| 223 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 224 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 225 | |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 226 | if (*data_len < 1) |
| 227 | { |
| 228 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 229 | } |
| 230 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 231 | uint8_t* reqdata = (uint8_t*)request; |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 232 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 233 | HIOMAPD_IFACE, "GetInfo"); |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 234 | m.append(reqdata[0]); |
| 235 | |
| 236 | try |
| 237 | { |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 238 | auto reply = ctx->bus->call(m); |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 239 | |
| 240 | uint8_t version; |
| 241 | uint8_t blockSizeShift; |
| 242 | uint16_t timeout; |
| 243 | reply.read(version, blockSizeShift, timeout); |
| 244 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 245 | uint8_t* respdata = (uint8_t*)response; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 246 | |
| 247 | /* FIXME: Assumes v2! */ |
| 248 | put(&respdata[0], version); |
| 249 | put(&respdata[1], blockSizeShift); |
| 250 | put(&respdata[2], htole16(timeout)); |
| 251 | |
| 252 | *data_len = 4; |
| 253 | } |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 254 | catch (const exception::SdBusError& e) |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 255 | { |
| 256 | return hiomap_xlate_errno(e.get_errno()); |
| 257 | } |
| 258 | |
| 259 | return IPMI_CC_OK; |
| 260 | } |
| 261 | |
Andrew Jeffery | db688e9 | 2018-08-23 21:21:30 +0930 | [diff] [blame] | 262 | static ipmi_ret_t hiomap_get_flash_info(ipmi_request_t request, |
| 263 | ipmi_response_t response, |
| 264 | ipmi_data_len_t data_len, |
| 265 | ipmi_context_t context) |
| 266 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 267 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | db688e9 | 2018-08-23 21:21:30 +0930 | [diff] [blame] | 268 | |
| 269 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 270 | HIOMAPD_IFACE_V2, "GetFlashInfo"); |
| 271 | try |
| 272 | { |
| 273 | auto reply = ctx->bus->call(m); |
| 274 | |
| 275 | uint16_t flashSize, eraseSize; |
| 276 | reply.read(flashSize, eraseSize); |
| 277 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 278 | uint8_t* respdata = (uint8_t*)response; |
Andrew Jeffery | db688e9 | 2018-08-23 21:21:30 +0930 | [diff] [blame] | 279 | put(&respdata[0], htole16(flashSize)); |
| 280 | put(&respdata[2], htole16(eraseSize)); |
| 281 | |
| 282 | *data_len = 4; |
| 283 | } |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 284 | catch (const exception::SdBusError& e) |
Andrew Jeffery | db688e9 | 2018-08-23 21:21:30 +0930 | [diff] [blame] | 285 | { |
| 286 | return hiomap_xlate_errno(e.get_errno()); |
| 287 | } |
| 288 | |
| 289 | return IPMI_CC_OK; |
| 290 | } |
| 291 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 292 | static ipmi_ret_t hiomap_create_window(struct hiomap* ctx, bool ro, |
Andrew Jeffery | a00f59b | 2018-08-23 22:25:49 +0930 | [diff] [blame] | 293 | ipmi_request_t request, |
| 294 | ipmi_response_t response, |
| 295 | ipmi_data_len_t data_len) |
| 296 | { |
| 297 | if (*data_len < 4) |
| 298 | { |
| 299 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 300 | } |
| 301 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 302 | uint8_t* reqdata = (uint8_t*)request; |
Andrew Jeffery | a00f59b | 2018-08-23 22:25:49 +0930 | [diff] [blame] | 303 | auto windowType = ro ? "CreateReadWindow" : "CreateWriteWindow"; |
| 304 | |
| 305 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 306 | HIOMAPD_IFACE_V2, windowType); |
| 307 | m.append(le16toh(get<uint16_t>(&reqdata[0]))); |
| 308 | m.append(le16toh(get<uint16_t>(&reqdata[2]))); |
| 309 | |
| 310 | try |
| 311 | { |
| 312 | auto reply = ctx->bus->call(m); |
| 313 | |
| 314 | uint16_t lpcAddress, size, offset; |
| 315 | reply.read(lpcAddress, size, offset); |
| 316 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 317 | uint8_t* respdata = (uint8_t*)response; |
Andrew Jeffery | a00f59b | 2018-08-23 22:25:49 +0930 | [diff] [blame] | 318 | |
| 319 | /* FIXME: Assumes v2! */ |
| 320 | put(&respdata[0], htole16(lpcAddress)); |
| 321 | put(&respdata[2], htole16(size)); |
| 322 | put(&respdata[4], htole16(offset)); |
| 323 | |
| 324 | *data_len = 6; |
| 325 | } |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 326 | catch (const exception::SdBusError& e) |
Andrew Jeffery | a00f59b | 2018-08-23 22:25:49 +0930 | [diff] [blame] | 327 | { |
| 328 | return hiomap_xlate_errno(e.get_errno()); |
| 329 | } |
| 330 | |
| 331 | return IPMI_CC_OK; |
| 332 | } |
| 333 | |
| 334 | static ipmi_ret_t hiomap_create_read_window(ipmi_request_t request, |
| 335 | ipmi_response_t response, |
| 336 | ipmi_data_len_t data_len, |
| 337 | ipmi_context_t context) |
| 338 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 339 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | a00f59b | 2018-08-23 22:25:49 +0930 | [diff] [blame] | 340 | |
| 341 | return hiomap_create_window(ctx, true, request, response, data_len); |
| 342 | } |
| 343 | |
| 344 | static ipmi_ret_t hiomap_create_write_window(ipmi_request_t request, |
| 345 | ipmi_response_t response, |
| 346 | ipmi_data_len_t data_len, |
| 347 | ipmi_context_t context) |
| 348 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 349 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | a00f59b | 2018-08-23 22:25:49 +0930 | [diff] [blame] | 350 | |
| 351 | return hiomap_create_window(ctx, false, request, response, data_len); |
| 352 | } |
| 353 | |
Andrew Jeffery | b52822c | 2018-08-23 23:01:39 +0930 | [diff] [blame] | 354 | static ipmi_ret_t hiomap_close_window(ipmi_request_t request, |
| 355 | ipmi_response_t response, |
| 356 | ipmi_data_len_t data_len, |
| 357 | ipmi_context_t context) |
| 358 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 359 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | b52822c | 2018-08-23 23:01:39 +0930 | [diff] [blame] | 360 | |
| 361 | if (*data_len < 1) |
| 362 | { |
| 363 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 364 | } |
| 365 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 366 | uint8_t* reqdata = (uint8_t*)request; |
Andrew Jeffery | b52822c | 2018-08-23 23:01:39 +0930 | [diff] [blame] | 367 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 368 | HIOMAPD_IFACE_V2, "CloseWindow"); |
| 369 | m.append(reqdata[0]); |
| 370 | |
| 371 | try |
| 372 | { |
| 373 | auto reply = ctx->bus->call(m); |
| 374 | |
| 375 | *data_len = 0; |
| 376 | } |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 377 | catch (const exception::SdBusError& e) |
Andrew Jeffery | b52822c | 2018-08-23 23:01:39 +0930 | [diff] [blame] | 378 | { |
| 379 | return hiomap_xlate_errno(e.get_errno()); |
| 380 | } |
| 381 | |
| 382 | return IPMI_CC_OK; |
| 383 | } |
| 384 | |
Andrew Jeffery | 9847f1c | 2018-08-24 09:13:40 +0930 | [diff] [blame] | 385 | static ipmi_ret_t hiomap_mark_dirty(ipmi_request_t request, |
| 386 | ipmi_response_t response, |
| 387 | ipmi_data_len_t data_len, |
| 388 | ipmi_context_t context) |
| 389 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 390 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | 9847f1c | 2018-08-24 09:13:40 +0930 | [diff] [blame] | 391 | |
| 392 | if (*data_len < 4) |
| 393 | { |
| 394 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 395 | } |
| 396 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 397 | uint8_t* reqdata = (uint8_t*)request; |
Andrew Jeffery | 9847f1c | 2018-08-24 09:13:40 +0930 | [diff] [blame] | 398 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 399 | HIOMAPD_IFACE_V2, "MarkDirty"); |
| 400 | /* FIXME: Assumes v2 */ |
| 401 | m.append(le16toh(get<uint16_t>(&reqdata[0]))); /* offset */ |
| 402 | m.append(le16toh(get<uint16_t>(&reqdata[2]))); /* size */ |
| 403 | |
| 404 | try |
| 405 | { |
| 406 | auto reply = ctx->bus->call(m); |
| 407 | |
| 408 | *data_len = 0; |
| 409 | } |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 410 | catch (const exception::SdBusError& e) |
Andrew Jeffery | 9847f1c | 2018-08-24 09:13:40 +0930 | [diff] [blame] | 411 | { |
| 412 | return hiomap_xlate_errno(e.get_errno()); |
| 413 | } |
| 414 | |
| 415 | return IPMI_CC_OK; |
| 416 | } |
| 417 | |
Andrew Jeffery | 7b225fb | 2018-08-24 09:19:21 +0930 | [diff] [blame] | 418 | static ipmi_ret_t hiomap_flush(ipmi_request_t request, ipmi_response_t response, |
| 419 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 420 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 421 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | 7b225fb | 2018-08-24 09:19:21 +0930 | [diff] [blame] | 422 | |
| 423 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 424 | HIOMAPD_IFACE_V2, "Flush"); |
| 425 | |
| 426 | try |
| 427 | { |
| 428 | /* FIXME: No argument call assumes v2 */ |
| 429 | auto reply = ctx->bus->call(m); |
| 430 | |
| 431 | *data_len = 0; |
| 432 | } |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 433 | catch (const exception::SdBusError& e) |
Andrew Jeffery | 7b225fb | 2018-08-24 09:19:21 +0930 | [diff] [blame] | 434 | { |
| 435 | return hiomap_xlate_errno(e.get_errno()); |
| 436 | } |
| 437 | |
| 438 | return IPMI_CC_OK; |
| 439 | } |
| 440 | |
Andrew Jeffery | 99f277a | 2018-08-24 09:24:04 +0930 | [diff] [blame] | 441 | static ipmi_ret_t hiomap_ack(ipmi_request_t request, ipmi_response_t response, |
| 442 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 443 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 444 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | 99f277a | 2018-08-24 09:24:04 +0930 | [diff] [blame] | 445 | |
| 446 | if (*data_len < 1) |
| 447 | { |
| 448 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 449 | } |
| 450 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 451 | uint8_t* reqdata = (uint8_t*)request; |
Andrew Jeffery | 99f277a | 2018-08-24 09:24:04 +0930 | [diff] [blame] | 452 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 453 | HIOMAPD_IFACE_V2, "Ack"); |
| 454 | auto acked = reqdata[0]; |
| 455 | m.append(acked); |
| 456 | |
| 457 | try |
| 458 | { |
| 459 | auto reply = ctx->bus->call(m); |
| 460 | |
| 461 | /* Update our cache: Necessary because the signals do not carry a value |
| 462 | */ |
| 463 | ctx->bmc_events &= ~acked; |
| 464 | |
| 465 | *data_len = 0; |
| 466 | } |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 467 | catch (const exception::SdBusError& e) |
Andrew Jeffery | 99f277a | 2018-08-24 09:24:04 +0930 | [diff] [blame] | 468 | { |
| 469 | return hiomap_xlate_errno(e.get_errno()); |
| 470 | } |
| 471 | |
| 472 | return IPMI_CC_OK; |
| 473 | } |
| 474 | |
Andrew Jeffery | a1e35b8 | 2018-08-24 09:39:10 +0930 | [diff] [blame] | 475 | static ipmi_ret_t hiomap_erase(ipmi_request_t request, ipmi_response_t response, |
| 476 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 477 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 478 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | a1e35b8 | 2018-08-24 09:39:10 +0930 | [diff] [blame] | 479 | |
| 480 | if (*data_len < 4) |
| 481 | { |
| 482 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 483 | } |
| 484 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 485 | uint8_t* reqdata = (uint8_t*)request; |
Andrew Jeffery | a1e35b8 | 2018-08-24 09:39:10 +0930 | [diff] [blame] | 486 | auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT, |
| 487 | HIOMAPD_IFACE_V2, "Erase"); |
| 488 | /* FIXME: Assumes v2 */ |
| 489 | m.append(le16toh(get<uint16_t>(&reqdata[0]))); /* offset */ |
| 490 | m.append(le16toh(get<uint16_t>(&reqdata[2]))); /* size */ |
| 491 | |
| 492 | try |
| 493 | { |
| 494 | auto reply = ctx->bus->call(m); |
| 495 | |
| 496 | *data_len = 0; |
| 497 | } |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 498 | catch (const exception::SdBusError& e) |
Andrew Jeffery | a1e35b8 | 2018-08-24 09:39:10 +0930 | [diff] [blame] | 499 | { |
| 500 | return hiomap_xlate_errno(e.get_errno()); |
| 501 | } |
| 502 | |
| 503 | return IPMI_CC_OK; |
| 504 | } |
| 505 | |
Andrew Jeffery | 04d7513 | 2018-09-26 00:58:52 +0930 | [diff] [blame] | 506 | #define HIOMAP_C_RESET 1 |
| 507 | #define HIOMAP_C_GET_INFO 2 |
| 508 | #define HIOMAP_C_GET_FLASH_INFO 3 |
| 509 | #define HIOMAP_C_CREATE_READ_WINDOW 4 |
| 510 | #define HIOMAP_C_CLOSE_WINDOW 5 |
| 511 | #define HIOMAP_C_CREATE_WRITE_WINDOW 6 |
| 512 | #define HIOMAP_C_MARK_DIRTY 7 |
| 513 | #define HIOMAP_C_FLUSH 8 |
| 514 | #define HIOMAP_C_ACK 9 |
| 515 | #define HIOMAP_C_ERASE 10 |
| 516 | |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 517 | static const hiomap_command hiomap_commands[] = { |
Andrew Jeffery | 04d7513 | 2018-09-26 00:58:52 +0930 | [diff] [blame] | 518 | [0] = NULL, /* Invalid command ID */ |
| 519 | [HIOMAP_C_RESET] = hiomap_reset, |
| 520 | [HIOMAP_C_GET_INFO] = hiomap_get_info, |
| 521 | [HIOMAP_C_GET_FLASH_INFO] = hiomap_get_flash_info, |
| 522 | [HIOMAP_C_CREATE_READ_WINDOW] = hiomap_create_read_window, |
| 523 | [HIOMAP_C_CLOSE_WINDOW] = hiomap_close_window, |
| 524 | [HIOMAP_C_CREATE_WRITE_WINDOW] = hiomap_create_write_window, |
| 525 | [HIOMAP_C_MARK_DIRTY] = hiomap_mark_dirty, |
| 526 | [HIOMAP_C_FLUSH] = hiomap_flush, |
| 527 | [HIOMAP_C_ACK] = hiomap_ack, |
| 528 | [HIOMAP_C_ERASE] = hiomap_erase, |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 529 | }; |
| 530 | |
| 531 | /* FIXME: Define this in the "right" place, wherever that is */ |
| 532 | /* FIXME: Double evaluation */ |
| 533 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 534 | |
| 535 | static ipmi_ret_t hiomap_dispatch(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 536 | ipmi_request_t request, |
| 537 | ipmi_response_t response, |
| 538 | ipmi_data_len_t data_len, |
| 539 | ipmi_context_t context) |
| 540 | { |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 541 | struct hiomap* ctx = static_cast<struct hiomap*>(context); |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 542 | |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 543 | if (*data_len < 2) |
| 544 | { |
| 545 | *data_len = 0; |
| 546 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 547 | } |
| 548 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 549 | uint8_t* ipmi_req = (uint8_t*)request; |
| 550 | uint8_t* ipmi_resp = (uint8_t*)response; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 551 | uint8_t hiomap_cmd = ipmi_req[0]; |
| 552 | |
| 553 | if (hiomap_cmd == 0 || hiomap_cmd > ARRAY_SIZE(hiomap_commands) - 1) |
| 554 | { |
| 555 | *data_len = 0; |
| 556 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 557 | } |
Andrew Jeffery | 04d7513 | 2018-09-26 00:58:52 +0930 | [diff] [blame] | 558 | |
| 559 | bool is_unversioned = |
| 560 | (hiomap_cmd == HIOMAP_C_RESET || hiomap_cmd == HIOMAP_C_GET_INFO || |
| 561 | hiomap_cmd == HIOMAP_C_ACK); |
| 562 | if (!is_unversioned && ctx->seq == ipmi_req[1]) |
| 563 | { |
| 564 | *data_len = 0; |
| 565 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 566 | } |
| 567 | |
| 568 | ctx->seq = ipmi_req[1]; |
| 569 | |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 570 | uint8_t* flash_req = ipmi_req + 2; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 571 | size_t flash_len = *data_len - 2; |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 572 | uint8_t* flash_resp = ipmi_resp + 2; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 573 | |
| 574 | ipmi_ret_t cc = |
| 575 | hiomap_commands[hiomap_cmd](flash_req, flash_resp, &flash_len, context); |
| 576 | if (cc != IPMI_CC_OK) |
| 577 | { |
| 578 | *data_len = 0; |
| 579 | return cc; |
| 580 | } |
| 581 | |
| 582 | /* Populate the response command and sequence */ |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 583 | ipmi_resp[0] = hiomap_cmd; |
Andrew Jeffery | 04d7513 | 2018-09-26 00:58:52 +0930 | [diff] [blame] | 584 | ipmi_resp[1] = ctx->seq; |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 585 | |
| 586 | *data_len = flash_len + 2; |
| 587 | |
| 588 | return cc; |
| 589 | } |
| 590 | } // namespace flash |
| 591 | } // namespace openpower |
| 592 | |
| 593 | static void register_openpower_hiomap_commands() |
| 594 | { |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 595 | using namespace openpower::flash; |
| 596 | |
| 597 | /* FIXME: Clean this up? Can we unregister? */ |
Patrick Venture | 5b35506 | 2018-10-31 14:42:42 -0700 | [diff] [blame] | 598 | struct hiomap* ctx = new hiomap(); |
Andrew Jeffery | 0a3358e | 2018-08-21 10:42:09 +0930 | [diff] [blame] | 599 | |
| 600 | /* Initialise mapping from signal and property names to status bit */ |
| 601 | ctx->event_lookup["DaemonReady"] = BMC_EVENT_DAEMON_READY; |
| 602 | ctx->event_lookup["FlashControlLost"] = BMC_EVENT_FLASH_CTRL_LOST; |
| 603 | ctx->event_lookup["WindowReset"] = BMC_EVENT_WINDOW_RESET; |
| 604 | ctx->event_lookup["ProtocolReset"] = BMC_EVENT_PROTOCOL_RESET; |
| 605 | |
| 606 | ctx->bus = new bus::bus(ipmid_get_sd_bus_connection()); |
| 607 | |
| 608 | /* Initialise signal handling */ |
| 609 | |
| 610 | /* |
| 611 | * Can't use temporaries here because that causes SEGFAULTs due to slot |
| 612 | * destruction (!?), so enjoy the weird wrapping. |
| 613 | */ |
| 614 | ctx->properties = |
| 615 | new bus::match::match(std::move(hiomap_match_properties(ctx))); |
| 616 | ctx->bmc_reboot = new bus::match::match( |
| 617 | std::move(hiomap_match_signal_v2(ctx, "ProtocolReset"))); |
| 618 | ctx->window_reset = new bus::match::match( |
| 619 | std::move(hiomap_match_signal_v2(ctx, "WindowReset"))); |
| 620 | |
| 621 | ipmi_register_callback(NETFUN_IBM_OEM, IPMI_CMD_HIOMAP, ctx, |
Andrew Jeffery | 2c07f6f | 2018-08-10 16:24:32 +0930 | [diff] [blame] | 622 | openpower::flash::hiomap_dispatch, SYSTEM_INTERFACE); |
| 623 | } |