blob: 7e616aead41eb20ae279f8d88f48cae0929e6618 [file] [log] [blame]
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +09301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
3
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +09304#include "hiomap.hpp"
5
6#include <endian.h>
Willy Tu89707262022-06-15 13:27:37 -07007#include <ipmid/api.h>
Andrew Jeffery619207d2018-11-22 13:09:17 +11008#include <signal.h>
9#include <string.h>
10#include <systemd/sd-bus.h>
11#include <systemd/sd-event.h>
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +093012
Patrick Williams0d5b0762023-05-10 07:50:28 -050013#include <ipmid-host/cmd-utils.hpp>
14#include <ipmid-host/cmd.hpp>
15#include <ipmid/api.hpp>
16#include <phosphor-logging/log.hpp>
17#include <sdbusplus/bus.hpp>
18#include <sdbusplus/bus/match.hpp>
19#include <sdbusplus/exception.hpp>
20
Andrew Jeffery619207d2018-11-22 13:09:17 +110021#include <cassert>
Patrick Ventureee701962018-10-31 14:51:37 -070022#include <cstring>
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +093023#include <fstream>
Andrew Jeffery0a3358e2018-08-21 10:42:09 +093024#include <functional>
Andrew Jeffery0a3358e2018-08-21 10:42:09 +093025#include <iostream>
Patrick Ventureee701962018-10-31 14:51:37 -070026#include <map>
Patrick Ventureee701962018-10-31 14:51:37 -070027#include <string>
28#include <tuple>
Willy Tu89707262022-06-15 13:27:37 -070029#include <unordered_map>
Patrick Ventureee701962018-10-31 14:51:37 -070030#include <utility>
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +093031
Andrew Jeffery619207d2018-11-22 13:09:17 +110032/*
33
34Design and integration notes
35============================
36
37The primary motivation of the Host I/O Mapping protocol (HIOMAP) is to mediate
38host access to a BMC-controlled flash chip housing the host's boot firmware.
39
40openpower-host-ipmi-flash facilitates the system design of transporting the
41HIOMAP protocol[1] over IPMI. This is somewhat abusive of IPMI, basically
42treating the BT interface as a mailbox with an interrupt each way between the
43BMC and the host.
44
45[1] https://github.com/openbmc/mboxbridge/blob/master/Documentation/protocol.md
46
47Using IPMI in this way has a number of challenges, a lot of them on the host
48side where we need to bring up the LPC and BT interfaces to enable IPMI before
49accessing the flash, and before any interrupts are enabled. There are also
50challenges on the BMC side with the design of the current implementation. We
51will cover those here.
52
53BMC-side System Design and Integration Issues
54---------------------------------------------
55
56The current design is that we have the HIOMAP daemon, mboxd (to be renamed),
57exposing a set of DBus interfaces. Whilst the spec defines the IPMI transport
58message packing, mboxd knows nothing of IPMI itself, instead relying on the
59DBus interface to receive messages from ipmid. ipmid in-turn knows nothing of
60the interfaces communicating with it, also relying on DBus to receive messages
61from interface-specific daemons, e.g. btbridged[2].
62
63[2] https://github.com/openbmc/btbridge
64
65For this design to function correctly we must ensure that the daemons are
66started and shut down in a reasonable order, however defining that order is
67somewhat tricky:
68
691. systemd uses Wants=/Before=/After= relationships in units to define both
70 start-up *and* shutdown order, in stack push / pop order respectively.
712. Clearly ipmid depends on btbridged to receive messages sent by signals and
72 replied to by method calls, so it needs a Wants=/After= relationship on
73 btbridged
743. mboxd depends on ipmid to receive messages sent by method call, and issues a
75 PropertiesChanged signal to notify of state changes.
76
77Point 3. suggests mboxd should have a Wants=/Before= relationship with ipmid to
78ensure ipmid can call into mboxd as messages arrive. However, this causes some
79grief with shutdown of the BMC, as mboxd needs to issue a state-change
Manojkiran Eda27031412024-06-17 11:47:39 +053080notification when it is shut down to inform the host that will not respond to
Andrew Jeffery619207d2018-11-22 13:09:17 +110081future requests and that the protocol state has been reset. If mboxd has a
82Wants=/Before= relationship with ipmid this message will never propagate to the
83host, as ipmid will be shut by systemd before mboxd.
84
85The above leads to mboxd having a Wants=/After= relationship with ipmid. This
86ensures that if mboxd is restarted on its own the correct state changes will be
87propagated to the host. The case where ipmid attempts to call into mboxd's DBus
88interface before mboxd is ready is mitigated by the ready bit in the protocol's
89BMC status, which will not yet be set, preventing a conforming host from
90attempting to contact mboxd.
91
92While this ordering prevents mboxd from being terminated before ipmid, there is
93no control over the *scheduling* of processes to ensure the PropertiesChanged
94signal emitted by mboxd before mboxd is terminated is seen by ipmid before
95*ipmid* is also terminated. This leads to our first implementation wart:
96
97 On the basis that mboxd has a Wants=/After= relationship with ipmid,
98 openpower-host-ipmi-flash will emit an HIOMAP BMC status event to the host
99 with the value BMC_EVENT_PROTOCOL_RESET upon receiving SIGTERM iff the BMC
100 state is not already set to BMC_EVENT_PROTOCOL_RESET.
101
102If ipmid has received SIGTERM the assumption is that it is systemd that is
103sending it, and that the Wants=/After= relationship requires that mboxd has
104been terminated before ipmid receives SIGTERM. By ensuring
105openpower-host-ipmi-flash emits the BMC event state we close the race where the
106host is not informed of the termination of mboxd due to scheduling ipmid (to
107deliver SIGTERM) prior to scheduling dbus-daemon, where the PropertiesChanged
108event would be delivered from mboxd to ipmid.
109
110Observations on the IPMI Specification and Design Details of ipmid
111------------------------------------------------------------------
112
113In addition to the system-level design problems with delivering
114PropertiesChanged signals during shutdown, IPMI specification and ipmid design
115issues exist that make it tedious to ensure that events will be correctly
116delivered to the host.
117
118The first necessary observation is that the mechanism for delivering BMC state
119change events from mboxd to the host over IPMI uses the SMS ATN bit to indicate
120a message is ready for delivery from the BMC to the host system. Retrieving the
121BMC state data involves the host recognising that the SMS ATN bit is set,
122performing Get Message Flags transaction with the BMC followed by a subsequent
123Get Message transaction. Thus, delivery of the HIOMAP protocol's BMC status is
124not an atomic event.
125
126The second necessary observation is that the kernel delivers signals
127asynchronously. This couples badly with IPMI's event delivery not being atomic:
128ipmid can win the race against SIGTERM to receive the PropertiesChanged event
129from mboxd, but lose the race to complete delivery to the host.
130
131 On this basis, we need to block the delivery of SIGTERM to ipmid until ipmid
132 has completed the set of `SMS ATN`/`Get Message Flags`/`Get Message`
133 transactions with the host
134
135One approach to this would be to configure a custom SIGTERM handler that sets
136some global application state to indicate that SIGTERM has been delivered. A
137better approach that avoids the need for global application state is to simply
138block the signal until we are ready to handle it, which we can do via
139sigprocmask(2).
140
141The existing design of ipmid makes it feasible to block and unblock
142asynchronous SIGTERM as we require. ipmid_send_cmd_to_host() takes a CallBack
143function as an argument, which is invoked by
144phosphor::host::command::Manager::getNextCommand(). The documentation for
145phosphor::host::command::Manager::getNextCommand() says:
146
147 @brief Extracts the next entry in the queue and returns
148 Command and data part of it.
149
150 @detail Also calls into the registered handlers so that they can now
151 send the CommandComplete signal since the interface contract
152 is that we emit this signal once the message has been
153 passed to the host (which is required when calling this)
154
155 Also, if the queue has more commands, then it will alert the
156 host
157
158However, its description is not entirely accurate. The callback function is
159invoked when ipmid *dequeues* the data to send to the host: Delivery of the
160data to the host occurs at some *after* the callback has been invoked.
161
162Invoking the callback before completion of delivery of the data to the host
163nullifies the approach of unblocking asynchronous SIGTERM in the callback
164associated with sending the HIOMAP BMC state event to the host, as the BMC
165kernel can asynchronously terminate the process between the callback being
166invoked and the host receiving the BMC state event data.
167
168Overcoming this issue hinges on a significant implementation detail of ipmid:
169
170 ipmid uses an sd_event loop in the main function to pump DBus events.
171
172This leads to a third necessary observation:
173
174 sd_event can be used to process UNIX signals as well as other events by way
175 of Linux's signalfd(2) interface.
176
177The fact that sd_event is used to pump DBus events means that ipmid can remain
178a single-threaded process. By remaining single-threaded we know that events
Manojkiran Eda27031412024-06-17 11:47:39 +0530179processing is sequential and no two events can be processed simultaneously. A
Andrew Jeffery619207d2018-11-22 13:09:17 +1100180corollary of this is that DBus events and UNIX signals are serialised with
Manojkiran Eda27031412024-06-17 11:47:39 +0530181respect to each other.
Andrew Jeffery619207d2018-11-22 13:09:17 +1100182
183The fourth necessary observation is that we do not need to pump sd_event in
184order to complete DBus method calls; sd_bus will handle the pumping independent
185of the main loop in order to complete the method invocation.
186
187Implementing Reliable HIOMAP BMC Status Event Delivery
188------------------------------------------------------
189
190We achieve reliable delivery of HIOMAP BMC status events in the following way:
191
1921. During plugin initialisation, mask SIGTERM using sigprocmask(2)
1932. Subsequent to masking SIGTERM, register
194 openpower::flash::hiomap_protocol_reset() as the SIGTERM handler using
195 sd_event_add_signal() to hook a signalfd(2) into sd_event
1963. openpower::flash::hiomap_protocol_reset() implements the logic to send the
197 BMC_EVENT_PROTOCOL_RESET state to the host if necessary, otherwise terminate
198 the sd_event loop.
1994. If it is necessary to send BMC_EVENT_PROTOCOL_RESET to the host in 3, assign
200 a callback handler that terminates the sd_event loop, which is only
201 processed after the current iteration is complete.
202
203This process and its use of signalfd integration in the sd_event loop
204eliminates the following three races:
205
2061. The scheduler race between mboxd, dbus-daemon and ipmid, by having
207 openpower-host-ipmi-flash conditionally deliver the protocol reset event if
208 no such message has been received from mboxd
2092. The race between delivering the BMC status event to the host and ipmid
210 receiving asynchronous SIGTERM after receiving the PropertiesChanged event
211 from mboxd
2123. The race to deliver the BMC status data to the host after unblocking
213 asynchronous SIGTERM in the host command callback and before receiving
214 asynchronous SIGTERM.
215
216Ultimately, ipmid could benefit from a redesign that fires the callback *after*
217delivering the associated data to the host, but brief inspection determined
218that this involved a non-trivial amount of effort.
219
220*/
221
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930222using namespace sdbusplus;
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930223using namespace phosphor::host::command;
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930224
225static void register_openpower_hiomap_commands() __attribute__((constructor));
226
227namespace openpower
228{
229namespace flash
230{
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930231constexpr auto BMC_EVENT_DAEMON_READY = 1 << 7;
232constexpr auto BMC_EVENT_FLASH_CTRL_LOST = 1 << 6;
233constexpr auto BMC_EVENT_WINDOW_RESET = 1 << 1;
234constexpr auto BMC_EVENT_PROTOCOL_RESET = 1 << 0;
235
236constexpr auto IPMI_CMD_HIOMAP_EVENT = 0x0f;
237
238constexpr auto HIOMAPD_SERVICE = "xyz.openbmc_project.Hiomapd";
239constexpr auto HIOMAPD_OBJECT = "/xyz/openbmc_project/Hiomapd";
240constexpr auto HIOMAPD_IFACE = "xyz.openbmc_project.Hiomapd.Protocol";
241constexpr auto HIOMAPD_IFACE_V2 = "xyz.openbmc_project.Hiomapd.Protocol.V2";
242
243constexpr auto DBUS_IFACE_PROPERTIES = "org.freedesktop.DBus.Properties";
244
Andrew Jeffery619207d2018-11-22 13:09:17 +1100245/* XXX: ipmid is currently single-threaded, pumping dbus events in sequence
246 * via the main event loop. Thus the code is not forced to be re-entrant. We
247 * also know that the callback and DBus event handling will not be running
248 * concurrently.
249 *
250 * ipmid_send_cmd_to_host() takes a callback that doesn't define a context
251 * pointer, so instead use a global. active_event_updates gates manipulation of
252 * process state, so its definition as a global at least aligns with its use.
253 */
254static int active_event_updates;
Andrew Jeffery619207d2018-11-22 13:09:17 +1100255
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930256struct hiomap
257{
Patrick Williams6aa92362022-07-22 19:26:53 -0500258 bus_t* bus;
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930259
260 /* Signals */
Patrick Williams6aa92362022-07-22 19:26:53 -0500261 bus::match_t* properties;
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930262
263 /* Protocol state */
264 std::map<std::string, int> event_lookup;
265 uint8_t bmc_events;
Andrew Jeffery04d75132018-09-26 00:58:52 +0930266 uint8_t seq;
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930267};
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930268
Vernon Maueryee3064b2019-03-18 10:19:58 -0700269SignalResponse sigtermResponse = SignalResponse::continueExecution;
270
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930271/* TODO: Replace get/put with packed structs and direct assignment */
Patrick Venture5b355062018-10-31 14:42:42 -0700272template <typename T>
273static inline T get(void* buf)
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930274{
275 T t;
Patrick Ventureee701962018-10-31 14:51:37 -0700276 std::memcpy(&t, buf, sizeof(t));
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930277 return t;
278}
279
Patrick Venture5b355062018-10-31 14:42:42 -0700280template <typename T>
281static inline void put(void* buf, T&& t)
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930282{
Patrick Ventureee701962018-10-31 14:51:37 -0700283 std::memcpy(buf, &t, sizeof(t));
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930284}
285
Willy Tu89707262022-06-15 13:27:37 -0700286using hiomap_command =
287 std::function<ipmi_ret_t(ipmi_request_t req, ipmi_response_t resp,
288 ipmi_data_len_t data_len, ipmi_context_t context)>;
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930289struct errno_cc_entry
290{
291 int err;
292 int cc;
293};
294
295static const errno_cc_entry errno_cc_map[] = {
296 {0, IPMI_CC_OK},
297 {EBUSY, IPMI_CC_BUSY},
298 {ENOTSUP, IPMI_CC_INVALID},
299 {ETIMEDOUT, 0xc3}, /* FIXME: Replace when defined in ipmid-api.h */
300 {ENOSPC, 0xc4}, /* FIXME: Replace when defined in ipmid-api.h */
301 {EINVAL, IPMI_CC_PARM_OUT_OF_RANGE},
302 {ENODEV, IPMI_CC_SENSOR_INVALID},
303 {EPERM, IPMI_CC_INSUFFICIENT_PRIVILEGE},
304 {EACCES, IPMI_CC_INSUFFICIENT_PRIVILEGE},
305 {-1, IPMI_CC_UNSPECIFIED_ERROR},
306};
307
308static int hiomap_xlate_errno(int err)
309{
Patrick Venture5b355062018-10-31 14:42:42 -0700310 const errno_cc_entry* entry = &errno_cc_map[0];
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930311
312 while (!(entry->err == err || entry->err == -1))
313 {
314 entry++;
315 }
316
317 return entry->cc;
318}
319
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930320static void ipmi_hiomap_event_response(IpmiCmdData cmd, bool status)
321{
322 using namespace phosphor::logging;
323
324 if (!status)
325 {
326 log<level::ERR>("Failed to deliver host command",
327 entry("SEL_COMMAND=%x:%x", cmd.first, cmd.second));
328 }
Andrew Jeffery619207d2018-11-22 13:09:17 +1100329
330 assert(active_event_updates);
331 active_event_updates--;
332 if (!active_event_updates)
333 {
Vernon Maueryee3064b2019-03-18 10:19:58 -0700334 sigtermResponse = SignalResponse::continueExecution;
335 log<level::DEBUG>("Unblocked SIGTERM");
Andrew Jeffery619207d2018-11-22 13:09:17 +1100336 }
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930337}
338
Patrick Venture5b355062018-10-31 14:42:42 -0700339static int hiomap_handle_property_update(struct hiomap* ctx,
Patrick Williams6aa92362022-07-22 19:26:53 -0500340 sdbusplus::message_t& msg)
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930341{
Andrew Jeffery619207d2018-11-22 13:09:17 +1100342 using namespace phosphor::logging;
343
Patrick Williams80d5bca2020-05-13 17:49:26 -0500344 std::map<std::string, std::variant<bool>> msgData;
Andrew Jeffery619207d2018-11-22 13:09:17 +1100345
Vernon Maueryee3064b2019-03-18 10:19:58 -0700346 sigtermResponse = SignalResponse::breakExecution;
Andrew Jeffery619207d2018-11-22 13:09:17 +1100347 if (!active_event_updates)
348 {
Vernon Maueryee3064b2019-03-18 10:19:58 -0700349 sigtermResponse = SignalResponse::breakExecution;
350 log<level::DEBUG>("Blocked SIGTERM");
Andrew Jeffery619207d2018-11-22 13:09:17 +1100351 }
352 active_event_updates++;
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930353
354 std::string iface;
355 msg.read(iface, msgData);
356
Patrick Williams0d5b0762023-05-10 07:50:28 -0500357 for (const auto& x : msgData)
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930358 {
359 if (!ctx->event_lookup.count(x.first))
360 {
361 /* Unsupported event? */
362 continue;
363 }
364
365 uint8_t mask = ctx->event_lookup[x.first];
Patrick Williams3d420922020-05-13 12:26:25 -0500366 auto value = std::get<bool>(x.second);
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930367
368 if (value)
369 {
370 ctx->bmc_events |= mask;
371 }
372 else
373 {
374 ctx->bmc_events &= ~mask;
375 }
376 }
377
378 auto cmd = std::make_pair(IPMI_CMD_HIOMAP_EVENT, ctx->bmc_events);
379
380 ipmid_send_cmd_to_host(std::make_tuple(cmd, ipmi_hiomap_event_response));
381
382 return 0;
383}
384
Willy Tu89707262022-06-15 13:27:37 -0700385static int hiomap_protocol_reset_response([[maybe_unused]] IpmiCmdData cmd,
386 [[maybe_unused]] bool status)
Andrew Jeffery619207d2018-11-22 13:09:17 +1100387{
Vernon Maueryee3064b2019-03-18 10:19:58 -0700388 // If this is running in signal context, ipmid will shutdown
389 // the event queue as the last signal handler
Adriana Kobylakd4b7f5e2019-04-16 15:40:18 -0500390 sigtermResponse = SignalResponse::continueExecution;
Vernon Maueryee3064b2019-03-18 10:19:58 -0700391 return 0;
Andrew Jeffery619207d2018-11-22 13:09:17 +1100392}
393
Vernon Maueryee3064b2019-03-18 10:19:58 -0700394static int hiomap_protocol_reset(struct hiomap* ctx)
Andrew Jeffery619207d2018-11-22 13:09:17 +1100395{
Andrew Jeffery619207d2018-11-22 13:09:17 +1100396 if (ctx->bmc_events == BMC_EVENT_PROTOCOL_RESET)
397 {
Vernon Maueryee3064b2019-03-18 10:19:58 -0700398 // If this is running in signal context, ipmid will shutdown
399 // the event queue as the last signal handler
Adriana Kobylakd4b7f5e2019-04-16 15:40:18 -0500400 sigtermResponse = SignalResponse::continueExecution;
Vernon Maueryee3064b2019-03-18 10:19:58 -0700401 return 0;
Andrew Jeffery619207d2018-11-22 13:09:17 +1100402 }
403
404 /*
405 * Send an attention indicating the hiomapd has died
406 * (BMC_EVENT_DAEMON_READY cleared) and that the protocol has been reset
407 * (BMC_EVENT_PROTOCOL_RESET set) to indicate to the host that it needs to
408 * wait for the BMC to come back and renegotiate the protocol.
409 *
410 * We know this to be the case in systems that integrate
411 * openpower-host-ipmi-flash, as hiomapd's unit depends on
412 * phosphor-ipmi-host, and thus hiomapd has been terminated before ipmid
413 * receives SIGTERM.
414 */
415 auto cmd = std::make_pair(IPMI_CMD_HIOMAP_EVENT, BMC_EVENT_PROTOCOL_RESET);
416
417 auto cmdHandler = std::make_tuple(cmd, hiomap_protocol_reset_response);
418 ipmid_send_cmd_to_host(cmdHandler);
419
420 return 0;
421}
422
Patrick Williams6aa92362022-07-22 19:26:53 -0500423static bus::match_t hiomap_match_properties(struct hiomap* ctx)
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930424{
Patrick Williams40a2afb2024-08-16 15:20:18 -0400425 auto properties =
426 bus::match::rules::propertiesChanged(HIOMAPD_OBJECT, HIOMAPD_IFACE_V2);
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930427
Patrick Williams6aa92362022-07-22 19:26:53 -0500428 bus::match_t match(
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930429 *ctx->bus, properties,
430 std::bind(hiomap_handle_property_update, ctx, std::placeholders::_1));
431
432 return match;
433}
434
Willy Tu89707262022-06-15 13:27:37 -0700435static ipmi_ret_t hiomap_reset([[maybe_unused]] ipmi_request_t request,
436 [[maybe_unused]] ipmi_response_t response,
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930437 ipmi_data_len_t data_len, ipmi_context_t context)
438{
Patrick Venture5b355062018-10-31 14:42:42 -0700439 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930440
441 auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT,
442 HIOMAPD_IFACE, "Reset");
443 try
444 {
445 ctx->bus->call(m);
446
447 *data_len = 0;
448 }
Patrick Williams6aa92362022-07-22 19:26:53 -0500449 catch (const exception_t& e)
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930450 {
451 return hiomap_xlate_errno(e.get_errno());
452 }
453
454 return IPMI_CC_OK;
455}
456
Patrick Williams40a2afb2024-08-16 15:20:18 -0400457static ipmi_ret_t
458 hiomap_get_info(ipmi_request_t request, ipmi_response_t response,
459 ipmi_data_len_t data_len, ipmi_context_t context)
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930460{
Patrick Venture5b355062018-10-31 14:42:42 -0700461 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930462
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930463 if (*data_len < 1)
464 {
465 return IPMI_CC_REQ_DATA_LEN_INVALID;
466 }
467
Patrick Venture5b355062018-10-31 14:42:42 -0700468 uint8_t* reqdata = (uint8_t*)request;
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930469 auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT,
470 HIOMAPD_IFACE, "GetInfo");
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930471 m.append(reqdata[0]);
472
473 try
474 {
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930475 auto reply = ctx->bus->call(m);
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930476
477 uint8_t version;
478 uint8_t blockSizeShift;
479 uint16_t timeout;
480 reply.read(version, blockSizeShift, timeout);
481
Patrick Venture5b355062018-10-31 14:42:42 -0700482 uint8_t* respdata = (uint8_t*)response;
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930483
484 /* FIXME: Assumes v2! */
485 put(&respdata[0], version);
486 put(&respdata[1], blockSizeShift);
487 put(&respdata[2], htole16(timeout));
488
489 *data_len = 4;
490 }
Patrick Williams6aa92362022-07-22 19:26:53 -0500491 catch (const exception_t& e)
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930492 {
493 return hiomap_xlate_errno(e.get_errno());
494 }
495
496 return IPMI_CC_OK;
497}
498
Patrick Williams40a2afb2024-08-16 15:20:18 -0400499static ipmi_ret_t hiomap_get_flash_info(
500 [[maybe_unused]] ipmi_request_t request, ipmi_response_t response,
501 ipmi_data_len_t data_len, ipmi_context_t context)
Andrew Jefferydb688e92018-08-23 21:21:30 +0930502{
Patrick Venture5b355062018-10-31 14:42:42 -0700503 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jefferydb688e92018-08-23 21:21:30 +0930504
505 auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT,
506 HIOMAPD_IFACE_V2, "GetFlashInfo");
507 try
508 {
509 auto reply = ctx->bus->call(m);
510
511 uint16_t flashSize, eraseSize;
512 reply.read(flashSize, eraseSize);
513
Patrick Venture5b355062018-10-31 14:42:42 -0700514 uint8_t* respdata = (uint8_t*)response;
Andrew Jefferydb688e92018-08-23 21:21:30 +0930515 put(&respdata[0], htole16(flashSize));
516 put(&respdata[2], htole16(eraseSize));
517
518 *data_len = 4;
519 }
Patrick Williams6aa92362022-07-22 19:26:53 -0500520 catch (const exception_t& e)
Andrew Jefferydb688e92018-08-23 21:21:30 +0930521 {
522 return hiomap_xlate_errno(e.get_errno());
523 }
524
525 return IPMI_CC_OK;
526}
527
Patrick Williams40a2afb2024-08-16 15:20:18 -0400528static ipmi_ret_t
529 hiomap_create_window(struct hiomap* ctx, bool ro, ipmi_request_t request,
530 ipmi_response_t response, ipmi_data_len_t data_len)
Andrew Jefferya00f59b2018-08-23 22:25:49 +0930531{
532 if (*data_len < 4)
533 {
534 return IPMI_CC_REQ_DATA_LEN_INVALID;
535 }
536
Patrick Venture5b355062018-10-31 14:42:42 -0700537 uint8_t* reqdata = (uint8_t*)request;
Andrew Jefferya00f59b2018-08-23 22:25:49 +0930538 auto windowType = ro ? "CreateReadWindow" : "CreateWriteWindow";
539
540 auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT,
541 HIOMAPD_IFACE_V2, windowType);
542 m.append(le16toh(get<uint16_t>(&reqdata[0])));
543 m.append(le16toh(get<uint16_t>(&reqdata[2])));
544
545 try
546 {
547 auto reply = ctx->bus->call(m);
548
549 uint16_t lpcAddress, size, offset;
550 reply.read(lpcAddress, size, offset);
551
Patrick Venture5b355062018-10-31 14:42:42 -0700552 uint8_t* respdata = (uint8_t*)response;
Andrew Jefferya00f59b2018-08-23 22:25:49 +0930553
554 /* FIXME: Assumes v2! */
555 put(&respdata[0], htole16(lpcAddress));
556 put(&respdata[2], htole16(size));
557 put(&respdata[4], htole16(offset));
558
559 *data_len = 6;
560 }
Patrick Williams6aa92362022-07-22 19:26:53 -0500561 catch (const exception_t& e)
Andrew Jefferya00f59b2018-08-23 22:25:49 +0930562 {
563 return hiomap_xlate_errno(e.get_errno());
564 }
565
566 return IPMI_CC_OK;
567}
568
Patrick Williams40a2afb2024-08-16 15:20:18 -0400569static ipmi_ret_t
570 hiomap_create_read_window(ipmi_request_t request, ipmi_response_t response,
571 ipmi_data_len_t data_len, ipmi_context_t context)
Andrew Jefferya00f59b2018-08-23 22:25:49 +0930572{
Patrick Venture5b355062018-10-31 14:42:42 -0700573 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jefferya00f59b2018-08-23 22:25:49 +0930574
575 return hiomap_create_window(ctx, true, request, response, data_len);
576}
577
Patrick Williams40a2afb2024-08-16 15:20:18 -0400578static ipmi_ret_t
579 hiomap_create_write_window(ipmi_request_t request, ipmi_response_t response,
580 ipmi_data_len_t data_len, ipmi_context_t context)
Andrew Jefferya00f59b2018-08-23 22:25:49 +0930581{
Patrick Venture5b355062018-10-31 14:42:42 -0700582 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jefferya00f59b2018-08-23 22:25:49 +0930583
584 return hiomap_create_window(ctx, false, request, response, data_len);
585}
586
Patrick Williams40a2afb2024-08-16 15:20:18 -0400587static ipmi_ret_t hiomap_close_window(
588 ipmi_request_t request, [[maybe_unused]] ipmi_response_t response,
589 ipmi_data_len_t data_len, ipmi_context_t context)
Andrew Jefferyb52822c2018-08-23 23:01:39 +0930590{
Patrick Venture5b355062018-10-31 14:42:42 -0700591 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jefferyb52822c2018-08-23 23:01:39 +0930592
593 if (*data_len < 1)
594 {
595 return IPMI_CC_REQ_DATA_LEN_INVALID;
596 }
597
Patrick Venture5b355062018-10-31 14:42:42 -0700598 uint8_t* reqdata = (uint8_t*)request;
Andrew Jefferyb52822c2018-08-23 23:01:39 +0930599 auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT,
600 HIOMAPD_IFACE_V2, "CloseWindow");
601 m.append(reqdata[0]);
602
603 try
604 {
605 auto reply = ctx->bus->call(m);
606
607 *data_len = 0;
608 }
Patrick Williams6aa92362022-07-22 19:26:53 -0500609 catch (const exception_t& e)
Andrew Jefferyb52822c2018-08-23 23:01:39 +0930610 {
611 return hiomap_xlate_errno(e.get_errno());
612 }
613
614 return IPMI_CC_OK;
615}
616
Patrick Williams40a2afb2024-08-16 15:20:18 -0400617static ipmi_ret_t hiomap_mark_dirty(
618 ipmi_request_t request, [[maybe_unused]] ipmi_response_t response,
619 ipmi_data_len_t data_len, ipmi_context_t context)
Andrew Jeffery9847f1c2018-08-24 09:13:40 +0930620{
Patrick Venture5b355062018-10-31 14:42:42 -0700621 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jeffery9847f1c2018-08-24 09:13:40 +0930622
623 if (*data_len < 4)
624 {
625 return IPMI_CC_REQ_DATA_LEN_INVALID;
626 }
627
Patrick Venture5b355062018-10-31 14:42:42 -0700628 uint8_t* reqdata = (uint8_t*)request;
Andrew Jeffery9847f1c2018-08-24 09:13:40 +0930629 auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT,
630 HIOMAPD_IFACE_V2, "MarkDirty");
631 /* FIXME: Assumes v2 */
632 m.append(le16toh(get<uint16_t>(&reqdata[0]))); /* offset */
633 m.append(le16toh(get<uint16_t>(&reqdata[2]))); /* size */
634
635 try
636 {
637 auto reply = ctx->bus->call(m);
638
639 *data_len = 0;
640 }
Patrick Williams6aa92362022-07-22 19:26:53 -0500641 catch (const exception_t& e)
Andrew Jeffery9847f1c2018-08-24 09:13:40 +0930642 {
643 return hiomap_xlate_errno(e.get_errno());
644 }
645
646 return IPMI_CC_OK;
647}
648
Willy Tu89707262022-06-15 13:27:37 -0700649static ipmi_ret_t hiomap_flush([[maybe_unused]] ipmi_request_t request,
650 [[maybe_unused]] ipmi_response_t response,
Andrew Jeffery7b225fb2018-08-24 09:19:21 +0930651 ipmi_data_len_t data_len, ipmi_context_t context)
652{
Patrick Venture5b355062018-10-31 14:42:42 -0700653 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jeffery7b225fb2018-08-24 09:19:21 +0930654
655 auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT,
656 HIOMAPD_IFACE_V2, "Flush");
657
658 try
659 {
660 /* FIXME: No argument call assumes v2 */
661 auto reply = ctx->bus->call(m);
662
663 *data_len = 0;
664 }
Patrick Williams6aa92362022-07-22 19:26:53 -0500665 catch (const exception_t& e)
Andrew Jeffery7b225fb2018-08-24 09:19:21 +0930666 {
667 return hiomap_xlate_errno(e.get_errno());
668 }
669
670 return IPMI_CC_OK;
671}
672
Willy Tu89707262022-06-15 13:27:37 -0700673static ipmi_ret_t hiomap_ack(ipmi_request_t request,
674 [[maybe_unused]] ipmi_response_t response,
Andrew Jeffery99f277a2018-08-24 09:24:04 +0930675 ipmi_data_len_t data_len, ipmi_context_t context)
676{
Patrick Venture5b355062018-10-31 14:42:42 -0700677 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jeffery99f277a2018-08-24 09:24:04 +0930678
679 if (*data_len < 1)
680 {
681 return IPMI_CC_REQ_DATA_LEN_INVALID;
682 }
683
Patrick Venture5b355062018-10-31 14:42:42 -0700684 uint8_t* reqdata = (uint8_t*)request;
Andrew Jeffery99f277a2018-08-24 09:24:04 +0930685 auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT,
686 HIOMAPD_IFACE_V2, "Ack");
687 auto acked = reqdata[0];
688 m.append(acked);
689
690 try
691 {
692 auto reply = ctx->bus->call(m);
693
Andrew Jeffery99f277a2018-08-24 09:24:04 +0930694 *data_len = 0;
695 }
Patrick Williams6aa92362022-07-22 19:26:53 -0500696 catch (const exception_t& e)
Andrew Jeffery99f277a2018-08-24 09:24:04 +0930697 {
698 return hiomap_xlate_errno(e.get_errno());
699 }
700
701 return IPMI_CC_OK;
702}
703
Willy Tu89707262022-06-15 13:27:37 -0700704static ipmi_ret_t hiomap_erase(ipmi_request_t request,
705 [[maybe_unused]] ipmi_response_t response,
Andrew Jefferya1e35b82018-08-24 09:39:10 +0930706 ipmi_data_len_t data_len, ipmi_context_t context)
707{
Patrick Venture5b355062018-10-31 14:42:42 -0700708 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jefferya1e35b82018-08-24 09:39:10 +0930709
710 if (*data_len < 4)
711 {
712 return IPMI_CC_REQ_DATA_LEN_INVALID;
713 }
714
Patrick Venture5b355062018-10-31 14:42:42 -0700715 uint8_t* reqdata = (uint8_t*)request;
Andrew Jefferya1e35b82018-08-24 09:39:10 +0930716 auto m = ctx->bus->new_method_call(HIOMAPD_SERVICE, HIOMAPD_OBJECT,
717 HIOMAPD_IFACE_V2, "Erase");
718 /* FIXME: Assumes v2 */
719 m.append(le16toh(get<uint16_t>(&reqdata[0]))); /* offset */
720 m.append(le16toh(get<uint16_t>(&reqdata[2]))); /* size */
721
722 try
723 {
724 auto reply = ctx->bus->call(m);
725
726 *data_len = 0;
727 }
Patrick Williams6aa92362022-07-22 19:26:53 -0500728 catch (const exception_t& e)
Andrew Jefferya1e35b82018-08-24 09:39:10 +0930729 {
730 return hiomap_xlate_errno(e.get_errno());
731 }
732
733 return IPMI_CC_OK;
734}
735
Andrew Jeffery04d75132018-09-26 00:58:52 +0930736#define HIOMAP_C_RESET 1
737#define HIOMAP_C_GET_INFO 2
738#define HIOMAP_C_GET_FLASH_INFO 3
739#define HIOMAP_C_CREATE_READ_WINDOW 4
740#define HIOMAP_C_CLOSE_WINDOW 5
741#define HIOMAP_C_CREATE_WRITE_WINDOW 6
742#define HIOMAP_C_MARK_DIRTY 7
743#define HIOMAP_C_FLUSH 8
744#define HIOMAP_C_ACK 9
745#define HIOMAP_C_ERASE 10
746
Willy Tu89707262022-06-15 13:27:37 -0700747static const std::unordered_map<uint8_t, hiomap_command> hiomap_commands = {
748 {0, nullptr}, /* Invalid command ID */
749 {HIOMAP_C_RESET, hiomap_reset},
750 {HIOMAP_C_GET_INFO, hiomap_get_info},
751 {HIOMAP_C_GET_FLASH_INFO, hiomap_get_flash_info},
752 {HIOMAP_C_CREATE_READ_WINDOW, hiomap_create_read_window},
753 {HIOMAP_C_CLOSE_WINDOW, hiomap_close_window},
754 {HIOMAP_C_CREATE_WRITE_WINDOW, hiomap_create_write_window},
755 {HIOMAP_C_MARK_DIRTY, hiomap_mark_dirty},
756 {HIOMAP_C_FLUSH, hiomap_flush},
757 {HIOMAP_C_ACK, hiomap_ack},
758 {HIOMAP_C_ERASE, hiomap_erase},
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930759};
760
761/* FIXME: Define this in the "right" place, wherever that is */
762/* FIXME: Double evaluation */
763#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
764
Patrick Williams40a2afb2024-08-16 15:20:18 -0400765static ipmi_ret_t hiomap_dispatch(
766 [[maybe_unused]] ipmi_netfn_t netfn, [[maybe_unused]] ipmi_cmd_t cmd,
767 ipmi_request_t request, ipmi_response_t response, ipmi_data_len_t data_len,
768 ipmi_context_t context)
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930769{
Patrick Venture5b355062018-10-31 14:42:42 -0700770 struct hiomap* ctx = static_cast<struct hiomap*>(context);
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930771
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930772 if (*data_len < 2)
773 {
774 *data_len = 0;
775 return IPMI_CC_REQ_DATA_LEN_INVALID;
776 }
777
Patrick Venture5b355062018-10-31 14:42:42 -0700778 uint8_t* ipmi_req = (uint8_t*)request;
779 uint8_t* ipmi_resp = (uint8_t*)response;
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930780 uint8_t hiomap_cmd = ipmi_req[0];
781
Willy Tu89707262022-06-15 13:27:37 -0700782 if (hiomap_cmd == 0 || hiomap_cmd > hiomap_commands.size() - 1)
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930783 {
784 *data_len = 0;
785 return IPMI_CC_PARM_OUT_OF_RANGE;
786 }
Andrew Jeffery04d75132018-09-26 00:58:52 +0930787
Patrick Williams40a2afb2024-08-16 15:20:18 -0400788 bool is_unversioned =
789 (hiomap_cmd == HIOMAP_C_RESET || hiomap_cmd == HIOMAP_C_GET_INFO ||
790 hiomap_cmd == HIOMAP_C_ACK);
Andrew Jeffery04d75132018-09-26 00:58:52 +0930791 if (!is_unversioned && ctx->seq == ipmi_req[1])
792 {
793 *data_len = 0;
794 return IPMI_CC_INVALID_FIELD_REQUEST;
795 }
796
797 ctx->seq = ipmi_req[1];
798
Patrick Venture5b355062018-10-31 14:42:42 -0700799 uint8_t* flash_req = ipmi_req + 2;
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930800 size_t flash_len = *data_len - 2;
Patrick Venture5b355062018-10-31 14:42:42 -0700801 uint8_t* flash_resp = ipmi_resp + 2;
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930802
Willy Tu89707262022-06-15 13:27:37 -0700803 auto command = hiomap_commands.find(hiomap_cmd);
804 if (command == hiomap_commands.end())
805 {
806 *data_len = 0;
807 return IPMI_CC_INVALID;
808 }
809 ipmi_ret_t cc = command->second(flash_req, flash_resp, &flash_len, context);
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930810 if (cc != IPMI_CC_OK)
811 {
812 *data_len = 0;
813 return cc;
814 }
815
816 /* Populate the response command and sequence */
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930817 ipmi_resp[0] = hiomap_cmd;
Andrew Jeffery04d75132018-09-26 00:58:52 +0930818 ipmi_resp[1] = ctx->seq;
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930819
820 *data_len = flash_len + 2;
821
822 return cc;
823}
824} // namespace flash
825} // namespace openpower
826
827static void register_openpower_hiomap_commands()
828{
Andrew Jeffery619207d2018-11-22 13:09:17 +1100829 using namespace phosphor::logging;
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930830 using namespace openpower::flash;
831
Patrick Venture5b355062018-10-31 14:42:42 -0700832 struct hiomap* ctx = new hiomap();
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930833
834 /* Initialise mapping from signal and property names to status bit */
835 ctx->event_lookup["DaemonReady"] = BMC_EVENT_DAEMON_READY;
836 ctx->event_lookup["FlashControlLost"] = BMC_EVENT_FLASH_CTRL_LOST;
837 ctx->event_lookup["WindowReset"] = BMC_EVENT_WINDOW_RESET;
838 ctx->event_lookup["ProtocolReset"] = BMC_EVENT_PROTOCOL_RESET;
839
Patrick Williams6aa92362022-07-22 19:26:53 -0500840 ctx->bus = new bus_t(ipmid_get_sd_bus_connection());
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930841
842 /* Initialise signal handling */
843
844 /*
845 * Can't use temporaries here because that causes SEGFAULTs due to slot
846 * destruction (!?), so enjoy the weird wrapping.
847 */
Patrick Williams6aa92362022-07-22 19:26:53 -0500848 ctx->properties = new bus::match_t(std::move(hiomap_match_properties(ctx)));
Andrew Jeffery619207d2018-11-22 13:09:17 +1100849
Vernon Maueryee3064b2019-03-18 10:19:58 -0700850 std::function<SignalResponse(int)> shutdownHandler =
Willy Tu89707262022-06-15 13:27:37 -0700851 [ctx]([[maybe_unused]] int signalNumber) {
Patrick Williams40a2afb2024-08-16 15:20:18 -0400852 hiomap_protocol_reset(ctx);
853 return sigtermResponse;
854 };
Vernon Maueryee3064b2019-03-18 10:19:58 -0700855 registerSignalHandler(ipmi::prioMax, SIGTERM, shutdownHandler);
Andrew Jeffery0a3358e2018-08-21 10:42:09 +0930856
857 ipmi_register_callback(NETFUN_IBM_OEM, IPMI_CMD_HIOMAP, ctx,
Andrew Jeffery2c07f6f2018-08-10 16:24:32 +0930858 openpower::flash::hiomap_dispatch, SYSTEM_INTERFACE);
859}