4 * Copyright (c) 2015 Corey Minyard, MontaVista Software, LLC
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "sysemu/sysemu.h"
27 #include "qemu/timer.h"
28 #include "hw/ipmi/ipmi.h"
29 #include "qemu/error-report.h"
30 #include "hw/loader.h"
32 #define IPMI_NETFN_CHASSIS 0x00
34 #define IPMI_CMD_GET_CHASSIS_CAPABILITIES 0x00
35 #define IPMI_CMD_GET_CHASSIS_STATUS 0x01
36 #define IPMI_CMD_CHASSIS_CONTROL 0x02
37 #define IPMI_CMD_GET_SYS_RESTART_CAUSE 0x09
39 #define IPMI_NETFN_SENSOR_EVENT 0x04
41 #define IPMI_CMD_SET_SENSOR_EVT_ENABLE 0x28
42 #define IPMI_CMD_GET_SENSOR_EVT_ENABLE 0x29
43 #define IPMI_CMD_REARM_SENSOR_EVTS 0x2a
44 #define IPMI_CMD_GET_SENSOR_EVT_STATUS 0x2b
45 #define IPMI_CMD_GET_SENSOR_READING 0x2d
46 #define IPMI_CMD_SET_SENSOR_TYPE 0x2e
47 #define IPMI_CMD_GET_SENSOR_TYPE 0x2f
49 /* #define IPMI_NETFN_APP 0x06 In ipmi.h */
51 #define IPMI_CMD_GET_DEVICE_ID 0x01
52 #define IPMI_CMD_COLD_RESET 0x02
53 #define IPMI_CMD_WARM_RESET 0x03
54 #define IPMI_CMD_SET_ACPI_POWER_STATE 0x06
55 #define IPMI_CMD_GET_ACPI_POWER_STATE 0x07
56 #define IPMI_CMD_GET_DEVICE_GUID 0x08
57 #define IPMI_CMD_RESET_WATCHDOG_TIMER 0x22
58 #define IPMI_CMD_SET_WATCHDOG_TIMER 0x24
59 #define IPMI_CMD_GET_WATCHDOG_TIMER 0x25
60 #define IPMI_CMD_SET_BMC_GLOBAL_ENABLES 0x2e
61 #define IPMI_CMD_GET_BMC_GLOBAL_ENABLES 0x2f
62 #define IPMI_CMD_CLR_MSG_FLAGS 0x30
63 #define IPMI_CMD_GET_MSG_FLAGS 0x31
64 #define IPMI_CMD_GET_MSG 0x33
65 #define IPMI_CMD_SEND_MSG 0x34
66 #define IPMI_CMD_READ_EVT_MSG_BUF 0x35
68 #define IPMI_NETFN_STORAGE 0x0a
70 #define IPMI_CMD_GET_SDR_REP_INFO 0x20
71 #define IPMI_CMD_GET_SDR_REP_ALLOC_INFO 0x21
72 #define IPMI_CMD_RESERVE_SDR_REP 0x22
73 #define IPMI_CMD_GET_SDR 0x23
74 #define IPMI_CMD_ADD_SDR 0x24
75 #define IPMI_CMD_PARTIAL_ADD_SDR 0x25
76 #define IPMI_CMD_DELETE_SDR 0x26
77 #define IPMI_CMD_CLEAR_SDR_REP 0x27
78 #define IPMI_CMD_GET_SDR_REP_TIME 0x28
79 #define IPMI_CMD_SET_SDR_REP_TIME 0x29
80 #define IPMI_CMD_ENTER_SDR_REP_UPD_MODE 0x2A
81 #define IPMI_CMD_EXIT_SDR_REP_UPD_MODE 0x2B
82 #define IPMI_CMD_RUN_INIT_AGENT 0x2C
83 #define IPMI_CMD_GET_FRU_AREA_INFO 0x10
84 #define IPMI_CMD_READ_FRU_DATA 0x11
85 #define IPMI_CMD_WRITE_FRU_DATA 0x12
86 #define IPMI_CMD_GET_SEL_INFO 0x40
87 #define IPMI_CMD_GET_SEL_ALLOC_INFO 0x41
88 #define IPMI_CMD_RESERVE_SEL 0x42
89 #define IPMI_CMD_GET_SEL_ENTRY 0x43
90 #define IPMI_CMD_ADD_SEL_ENTRY 0x44
91 #define IPMI_CMD_PARTIAL_ADD_SEL_ENTRY 0x45
92 #define IPMI_CMD_DELETE_SEL_ENTRY 0x46
93 #define IPMI_CMD_CLEAR_SEL 0x47
94 #define IPMI_CMD_GET_SEL_TIME 0x48
95 #define IPMI_CMD_SET_SEL_TIME 0x49
98 /* Same as a timespec struct. */
104 #define MAX_SEL_SIZE 128
106 typedef struct IPMISel
{
107 uint8_t sel
[MAX_SEL_SIZE
][16];
108 unsigned int next_free
;
110 uint16_t reservation
;
111 uint8_t last_addition
[4];
112 uint8_t last_clear
[4];
116 #define MAX_SDR_SIZE 16384
118 typedef struct IPMISdr
{
119 uint8_t sdr
[MAX_SDR_SIZE
];
120 unsigned int next_free
;
121 uint16_t next_rec_id
;
122 uint16_t reservation
;
123 uint8_t last_addition
[4];
124 uint8_t last_clear
[4];
128 typedef struct IPMIFru
{
130 unsigned int nentries
;
135 typedef struct IPMISensor
{
138 uint16_t states_suppt
;
139 uint16_t assert_suppt
;
140 uint16_t deassert_suppt
;
142 uint16_t assert_states
;
143 uint16_t deassert_states
;
144 uint16_t assert_enable
;
145 uint16_t deassert_enable
;
147 uint8_t evt_reading_type_code
;
149 #define IPMI_SENSOR_GET_PRESENT(s) ((s)->status & 0x01)
150 #define IPMI_SENSOR_SET_PRESENT(s, v) ((s)->status = (s->status & ~0x01) | \
152 #define IPMI_SENSOR_GET_SCAN_ON(s) ((s)->status & 0x40)
153 #define IPMI_SENSOR_SET_SCAN_ON(s, v) ((s)->status = (s->status & ~0x40) | \
155 #define IPMI_SENSOR_GET_EVENTS_ON(s) ((s)->status & 0x80)
156 #define IPMI_SENSOR_SET_EVENTS_ON(s, v) ((s)->status = (s->status & ~0x80) | \
158 #define IPMI_SENSOR_GET_RET_STATUS(s) ((s)->status & 0xc0)
159 #define IPMI_SENSOR_SET_RET_STATUS(s, v) ((s)->status = (s->status & ~0xc0) | \
161 #define IPMI_SENSOR_IS_DISCRETE(s) ((s)->evt_reading_type_code != 1)
163 #define MAX_SENSORS 20
164 #define IPMI_WATCHDOG_SENSOR 0
166 typedef struct IPMIBmcSim IPMIBmcSim
;
167 typedef struct RspBuffer RspBuffer
;
169 #define MAX_NETFNS 64
171 typedef struct IPMICmdHandler
{
172 void (*cmd_handler
)(IPMIBmcSim
*s
,
173 uint8_t *cmd
, unsigned int cmd_len
,
175 unsigned int cmd_len_min
;
178 typedef struct IPMINetfn
{
179 unsigned int cmd_nums
;
180 const IPMICmdHandler
*cmd_handlers
;
183 typedef struct IPMIRcvBufEntry
{
184 QTAILQ_ENTRY(IPMIRcvBufEntry
) entry
;
186 uint8_t buf
[MAX_IPMI_MSG_SIZE
];
189 #define TYPE_IPMI_BMC_SIMULATOR "ipmi-bmc-sim"
190 #define IPMI_BMC_SIMULATOR(obj) OBJECT_CHECK(IPMIBmcSim, (obj), \
191 TYPE_IPMI_BMC_SIMULATOR)
197 uint8_t bmc_global_enables
;
200 bool watchdog_initialized
;
201 uint8_t watchdog_use
;
202 uint8_t watchdog_action
;
203 uint8_t watchdog_pretimeout
; /* In seconds */
204 bool watchdog_expired
;
205 uint16_t watchdog_timeout
; /* in 100's of milliseconds */
207 bool watchdog_running
;
208 bool watchdog_preaction_ran
;
209 int64_t watchdog_expiry
;
212 uint8_t ipmi_version
;
217 uint8_t product_id
[2];
219 uint8_t restart_cause
;
221 uint8_t acpi_power_state
[2];
227 IPMISensor sensors
[MAX_SENSORS
];
230 /* Odd netfns are for responses, so we only need the even ones. */
231 const IPMINetfn
*netfns
[MAX_NETFNS
/ 2];
233 /* We allow one event in the buffer */
236 QTAILQ_HEAD(, IPMIRcvBufEntry
) rcvbufs
;
239 #define IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK (1 << 3)
240 #define IPMI_BMC_MSG_FLAG_EVT_BUF_FULL (1 << 1)
241 #define IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE (1 << 0)
242 #define IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK_SET(s) \
243 (IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK & (s)->msg_flags)
244 #define IPMI_BMC_MSG_FLAG_EVT_BUF_FULL_SET(s) \
245 (IPMI_BMC_MSG_FLAG_EVT_BUF_FULL & (s)->msg_flags)
246 #define IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE_SET(s) \
247 (IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE & (s)->msg_flags)
249 #define IPMI_BMC_RCV_MSG_QUEUE_INT_BIT 0
250 #define IPMI_BMC_EVBUF_FULL_INT_BIT 1
251 #define IPMI_BMC_EVENT_MSG_BUF_BIT 2
252 #define IPMI_BMC_EVENT_LOG_BIT 3
253 #define IPMI_BMC_MSG_INTS_ON(s) ((s)->bmc_global_enables & \
254 (1 << IPMI_BMC_RCV_MSG_QUEUE_INT_BIT))
255 #define IPMI_BMC_EVBUF_FULL_INT_ENABLED(s) ((s)->bmc_global_enables & \
256 (1 << IPMI_BMC_EVBUF_FULL_INT_BIT))
257 #define IPMI_BMC_EVENT_LOG_ENABLED(s) ((s)->bmc_global_enables & \
258 (1 << IPMI_BMC_EVENT_LOG_BIT))
259 #define IPMI_BMC_EVENT_MSG_BUF_ENABLED(s) ((s)->bmc_global_enables & \
260 (1 << IPMI_BMC_EVENT_MSG_BUF_BIT))
262 #define IPMI_BMC_WATCHDOG_USE_MASK 0xc7
263 #define IPMI_BMC_WATCHDOG_ACTION_MASK 0x77
264 #define IPMI_BMC_WATCHDOG_GET_USE(s) ((s)->watchdog_use & 0x7)
265 #define IPMI_BMC_WATCHDOG_GET_DONT_LOG(s) (((s)->watchdog_use >> 7) & 0x1)
266 #define IPMI_BMC_WATCHDOG_GET_DONT_STOP(s) (((s)->watchdog_use >> 6) & 0x1)
267 #define IPMI_BMC_WATCHDOG_GET_PRE_ACTION(s) (((s)->watchdog_action >> 4) & 0x7)
268 #define IPMI_BMC_WATCHDOG_PRE_NONE 0
269 #define IPMI_BMC_WATCHDOG_PRE_SMI 1
270 #define IPMI_BMC_WATCHDOG_PRE_NMI 2
271 #define IPMI_BMC_WATCHDOG_PRE_MSG_INT 3
272 #define IPMI_BMC_WATCHDOG_GET_ACTION(s) ((s)->watchdog_action & 0x7)
273 #define IPMI_BMC_WATCHDOG_ACTION_NONE 0
274 #define IPMI_BMC_WATCHDOG_ACTION_RESET 1
275 #define IPMI_BMC_WATCHDOG_ACTION_POWER_DOWN 2
276 #define IPMI_BMC_WATCHDOG_ACTION_POWER_CYCLE 3
279 uint8_t buffer
[MAX_IPMI_MSG_SIZE
];
283 #define RSP_BUFFER_INITIALIZER { }
285 static inline void rsp_buffer_set_error(RspBuffer
*rsp
, uint8_t byte
)
287 rsp
->buffer
[2] = byte
;
290 /* Add a byte to the response. */
291 static inline void rsp_buffer_push(RspBuffer
*rsp
, uint8_t byte
)
293 if (rsp
->len
>= sizeof(rsp
->buffer
)) {
294 rsp_buffer_set_error(rsp
, IPMI_CC_REQUEST_DATA_TRUNCATED
);
297 rsp
->buffer
[rsp
->len
++] = byte
;
300 static inline void rsp_buffer_pushmore(RspBuffer
*rsp
, uint8_t *bytes
,
303 if (rsp
->len
+ n
>= sizeof(rsp
->buffer
)) {
304 rsp_buffer_set_error(rsp
, IPMI_CC_REQUEST_DATA_TRUNCATED
);
308 memcpy(&rsp
->buffer
[rsp
->len
], bytes
, n
);
312 static void ipmi_sim_handle_timeout(IPMIBmcSim
*ibs
);
314 static void ipmi_gettime(struct ipmi_time
*time
)
318 stime
= qemu_clock_get_ns(QEMU_CLOCK_HOST
);
319 time
->tv_sec
= stime
/ 1000000000LL;
320 time
->tv_nsec
= stime
% 1000000000LL;
323 static int64_t ipmi_getmonotime(void)
325 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
328 static void ipmi_timeout(void *opaque
)
330 IPMIBmcSim
*ibs
= opaque
;
332 ipmi_sim_handle_timeout(ibs
);
335 static void set_timestamp(IPMIBmcSim
*ibs
, uint8_t *ts
)
338 struct ipmi_time now
;
341 val
= now
.tv_sec
+ ibs
->sel
.time_offset
;
343 ts
[1] = (val
>> 8) & 0xff;
344 ts
[2] = (val
>> 16) & 0xff;
345 ts
[3] = (val
>> 24) & 0xff;
348 static void sdr_inc_reservation(IPMISdr
*sdr
)
351 if (sdr
->reservation
== 0) {
352 sdr
->reservation
= 1;
356 static int sdr_add_entry(IPMIBmcSim
*ibs
,
357 const struct ipmi_sdr_header
*sdrh_entry
,
358 unsigned int len
, uint16_t *recid
)
360 struct ipmi_sdr_header
*sdrh
=
361 (struct ipmi_sdr_header
*) &ibs
->sdr
.sdr
[ibs
->sdr
.next_free
];
363 if ((len
< IPMI_SDR_HEADER_SIZE
) || (len
> 255)) {
367 if (ipmi_sdr_length(sdrh_entry
) != len
) {
371 if (ibs
->sdr
.next_free
+ len
> MAX_SDR_SIZE
) {
372 ibs
->sdr
.overflow
= 1;
376 memcpy(sdrh
, sdrh_entry
, len
);
377 sdrh
->rec_id
[0] = ibs
->sdr
.next_rec_id
& 0xff;
378 sdrh
->rec_id
[1] = (ibs
->sdr
.next_rec_id
>> 8) & 0xff;
379 sdrh
->sdr_version
= 0x51; /* Conform to IPMI 1.5 spec */
382 *recid
= ibs
->sdr
.next_rec_id
;
384 ibs
->sdr
.next_rec_id
++;
385 set_timestamp(ibs
, ibs
->sdr
.last_addition
);
386 ibs
->sdr
.next_free
+= len
;
387 sdr_inc_reservation(&ibs
->sdr
);
391 static int sdr_find_entry(IPMISdr
*sdr
, uint16_t recid
,
392 unsigned int *retpos
, uint16_t *nextrec
)
394 unsigned int pos
= *retpos
;
396 while (pos
< sdr
->next_free
) {
397 struct ipmi_sdr_header
*sdrh
=
398 (struct ipmi_sdr_header
*) &sdr
->sdr
[pos
];
399 uint16_t trec
= ipmi_sdr_recid(sdrh
);
400 unsigned int nextpos
= pos
+ ipmi_sdr_length(sdrh
);
404 if (nextpos
>= sdr
->next_free
) {
407 *nextrec
= (sdr
->sdr
[nextpos
] |
408 (sdr
->sdr
[nextpos
+ 1] << 8));
419 int ipmi_bmc_sdr_find(IPMIBmc
*b
, uint16_t recid
,
420 const struct ipmi_sdr_compact
**sdr
, uint16_t *nextrec
)
423 IPMIBmcSim
*ibs
= IPMI_BMC_SIMULATOR(b
);
427 if (sdr_find_entry(&ibs
->sdr
, recid
, &pos
, nextrec
)) {
431 *sdr
= (const struct ipmi_sdr_compact
*) &ibs
->sdr
.sdr
[pos
];
435 static void sel_inc_reservation(IPMISel
*sel
)
438 if (sel
->reservation
== 0) {
439 sel
->reservation
= 1;
443 /* Returns 1 if the SEL is full and can't hold the event. */
444 static int sel_add_event(IPMIBmcSim
*ibs
, uint8_t *event
)
448 set_timestamp(ibs
, event
+ 3);
449 if (ibs
->sel
.next_free
== MAX_SEL_SIZE
) {
450 ibs
->sel
.overflow
= 1;
453 event
[0] = ibs
->sel
.next_free
& 0xff;
454 event
[1] = (ibs
->sel
.next_free
>> 8) & 0xff;
455 memcpy(ibs
->sel
.last_addition
, event
+ 3, 4);
456 memcpy(ibs
->sel
.sel
[ibs
->sel
.next_free
], event
, 16);
457 ibs
->sel
.next_free
++;
458 sel_inc_reservation(&ibs
->sel
);
462 static int attn_set(IPMIBmcSim
*ibs
)
464 return IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE_SET(ibs
)
465 || IPMI_BMC_MSG_FLAG_EVT_BUF_FULL_SET(ibs
)
466 || IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK_SET(ibs
);
469 static int attn_irq_enabled(IPMIBmcSim
*ibs
)
471 return (IPMI_BMC_MSG_INTS_ON(ibs
) && IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE_SET(ibs
))
472 || (IPMI_BMC_EVBUF_FULL_INT_ENABLED(ibs
) &&
473 IPMI_BMC_MSG_FLAG_EVT_BUF_FULL_SET(ibs
));
476 void ipmi_bmc_gen_event(IPMIBmc
*b
, uint8_t *evt
, bool log
)
478 IPMIBmcSim
*ibs
= IPMI_BMC_SIMULATOR(b
);
479 IPMIInterface
*s
= ibs
->parent
.intf
;
480 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
482 if (!IPMI_BMC_EVENT_MSG_BUF_ENABLED(ibs
)) {
486 if (log
&& IPMI_BMC_EVENT_LOG_ENABLED(ibs
)) {
487 sel_add_event(ibs
, evt
);
490 if (ibs
->msg_flags
& IPMI_BMC_MSG_FLAG_EVT_BUF_FULL
) {
494 memcpy(ibs
->evtbuf
, evt
, 16);
495 ibs
->msg_flags
|= IPMI_BMC_MSG_FLAG_EVT_BUF_FULL
;
496 k
->set_atn(s
, 1, attn_irq_enabled(ibs
));
500 static void gen_event(IPMIBmcSim
*ibs
, unsigned int sens_num
, uint8_t deassert
,
501 uint8_t evd1
, uint8_t evd2
, uint8_t evd3
)
503 IPMIInterface
*s
= ibs
->parent
.intf
;
504 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
506 IPMISensor
*sens
= ibs
->sensors
+ sens_num
;
508 if (!IPMI_BMC_EVENT_MSG_BUF_ENABLED(ibs
)) {
511 if (!IPMI_SENSOR_GET_EVENTS_ON(sens
)) {
515 evt
[2] = 0x2; /* System event record */
516 evt
[7] = ibs
->parent
.slave_addr
;
518 evt
[9] = 0x04; /* Format version */
519 evt
[10] = sens
->sensor_type
;
521 evt
[12] = sens
->evt_reading_type_code
| (!!deassert
<< 7);
526 if (IPMI_BMC_EVENT_LOG_ENABLED(ibs
)) {
527 sel_add_event(ibs
, evt
);
530 if (ibs
->msg_flags
& IPMI_BMC_MSG_FLAG_EVT_BUF_FULL
) {
534 memcpy(ibs
->evtbuf
, evt
, 16);
535 ibs
->msg_flags
|= IPMI_BMC_MSG_FLAG_EVT_BUF_FULL
;
536 k
->set_atn(s
, 1, attn_irq_enabled(ibs
));
539 static void sensor_set_discrete_bit(IPMIBmcSim
*ibs
, unsigned int sensor
,
540 unsigned int bit
, unsigned int val
,
541 uint8_t evd1
, uint8_t evd2
, uint8_t evd3
)
546 if (sensor
>= MAX_SENSORS
) {
554 sens
= ibs
->sensors
+ sensor
;
556 sens
->states
|= mask
& sens
->states_suppt
;
557 if (sens
->assert_states
& mask
) {
558 return; /* Already asserted */
560 sens
->assert_states
|= mask
& sens
->assert_suppt
;
561 if (sens
->assert_enable
& mask
& sens
->assert_states
) {
562 /* Send an event on assert */
563 gen_event(ibs
, sensor
, 0, evd1
, evd2
, evd3
);
566 sens
->states
&= ~(mask
& sens
->states_suppt
);
567 if (sens
->deassert_states
& mask
) {
568 return; /* Already deasserted */
570 sens
->deassert_states
|= mask
& sens
->deassert_suppt
;
571 if (sens
->deassert_enable
& mask
& sens
->deassert_states
) {
572 /* Send an event on deassert */
573 gen_event(ibs
, sensor
, 1, evd1
, evd2
, evd3
);
578 static void ipmi_init_sensors_from_sdrs(IPMIBmcSim
*s
)
583 for (i
= 0; i
< MAX_SENSORS
; i
++) {
584 memset(s
->sensors
+ i
, 0, sizeof(*sens
));
588 for (i
= 0; !sdr_find_entry(&s
->sdr
, i
, &pos
, NULL
); i
++) {
589 struct ipmi_sdr_compact
*sdr
=
590 (struct ipmi_sdr_compact
*) &s
->sdr
.sdr
[pos
];
591 unsigned int len
= sdr
->header
.rec_length
;
596 if (sdr
->header
.rec_type
!= IPMI_SDR_COMPACT_TYPE
) {
597 continue; /* Not a sensor SDR we set from */
600 if (sdr
->sensor_owner_number
>= MAX_SENSORS
) {
603 sens
= s
->sensors
+ sdr
->sensor_owner_number
;
605 IPMI_SENSOR_SET_PRESENT(sens
, 1);
606 IPMI_SENSOR_SET_SCAN_ON(sens
, (sdr
->sensor_init
>> 6) & 1);
607 IPMI_SENSOR_SET_EVENTS_ON(sens
, (sdr
->sensor_init
>> 5) & 1);
608 sens
->assert_suppt
= sdr
->assert_mask
[0] | (sdr
->assert_mask
[1] << 8);
609 sens
->deassert_suppt
=
610 sdr
->deassert_mask
[0] | (sdr
->deassert_mask
[1] << 8);
612 sdr
->discrete_mask
[0] | (sdr
->discrete_mask
[1] << 8);
613 sens
->sensor_type
= sdr
->sensor_type
;
614 sens
->evt_reading_type_code
= sdr
->reading_type
& 0x7f;
616 /* Enable all the events that are supported. */
617 sens
->assert_enable
= sens
->assert_suppt
;
618 sens
->deassert_enable
= sens
->deassert_suppt
;
622 static int ipmi_register_netfn(IPMIBmcSim
*s
, unsigned int netfn
,
623 const IPMINetfn
*netfnd
)
625 if ((netfn
& 1) || (netfn
>= MAX_NETFNS
) || (s
->netfns
[netfn
/ 2])) {
628 s
->netfns
[netfn
/ 2] = netfnd
;
632 static const IPMICmdHandler
*ipmi_get_handler(IPMIBmcSim
*ibs
,
636 const IPMICmdHandler
*hdl
;
638 if (netfn
& 1 || netfn
>= MAX_NETFNS
|| !ibs
->netfns
[netfn
/ 2]) {
642 if (cmd
>= ibs
->netfns
[netfn
/ 2]->cmd_nums
) {
646 hdl
= &ibs
->netfns
[netfn
/ 2]->cmd_handlers
[cmd
];
647 if (!hdl
->cmd_handler
) {
654 static void next_timeout(IPMIBmcSim
*ibs
)
657 if (ibs
->watchdog_running
) {
658 next
= ibs
->watchdog_expiry
;
661 next
= ipmi_getmonotime() + 60 * 1000000000LL;
663 timer_mod_ns(ibs
->timer
, next
);
666 static void ipmi_sim_handle_command(IPMIBmc
*b
,
667 uint8_t *cmd
, unsigned int cmd_len
,
668 unsigned int max_cmd_len
,
671 IPMIBmcSim
*ibs
= IPMI_BMC_SIMULATOR(b
);
672 IPMIInterface
*s
= ibs
->parent
.intf
;
673 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
674 const IPMICmdHandler
*hdl
;
675 RspBuffer rsp
= RSP_BUFFER_INITIALIZER
;
677 /* Set up the response, set the low bit of NETFN. */
678 /* Note that max_rsp_len must be at least 3 */
679 if (sizeof(rsp
.buffer
) < 3) {
680 rsp_buffer_set_error(&rsp
, IPMI_CC_REQUEST_DATA_TRUNCATED
);
684 rsp_buffer_push(&rsp
, cmd
[0] | 0x04);
685 rsp_buffer_push(&rsp
, cmd
[1]);
686 rsp_buffer_push(&rsp
, 0); /* Assume success */
688 /* If it's too short or it was truncated, return an error. */
690 rsp_buffer_set_error(&rsp
, IPMI_CC_REQUEST_DATA_LENGTH_INVALID
);
693 if (cmd_len
> max_cmd_len
) {
694 rsp_buffer_set_error(&rsp
, IPMI_CC_REQUEST_DATA_TRUNCATED
);
698 if ((cmd
[0] & 0x03) != 0) {
699 /* Only have stuff on LUN 0 */
700 rsp_buffer_set_error(&rsp
, IPMI_CC_COMMAND_INVALID_FOR_LUN
);
704 hdl
= ipmi_get_handler(ibs
, cmd
[0] >> 2, cmd
[1]);
706 rsp_buffer_set_error(&rsp
, IPMI_CC_INVALID_CMD
);
710 if (cmd_len
< hdl
->cmd_len_min
) {
711 rsp_buffer_set_error(&rsp
, IPMI_CC_REQUEST_DATA_LENGTH_INVALID
);
715 hdl
->cmd_handler(ibs
, cmd
, cmd_len
, &rsp
);
718 k
->handle_rsp(s
, msg_id
, rsp
.buffer
, rsp
.len
);
723 static void ipmi_sim_handle_timeout(IPMIBmcSim
*ibs
)
725 IPMIInterface
*s
= ibs
->parent
.intf
;
726 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
728 if (!ibs
->watchdog_running
) {
732 if (!ibs
->watchdog_preaction_ran
) {
733 switch (IPMI_BMC_WATCHDOG_GET_PRE_ACTION(ibs
)) {
734 case IPMI_BMC_WATCHDOG_PRE_NMI
:
735 ibs
->msg_flags
|= IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK
;
736 k
->do_hw_op(s
, IPMI_SEND_NMI
, 0);
737 sensor_set_discrete_bit(ibs
, IPMI_WATCHDOG_SENSOR
, 8, 1,
738 0xc8, (2 << 4) | 0xf, 0xff);
741 case IPMI_BMC_WATCHDOG_PRE_MSG_INT
:
742 ibs
->msg_flags
|= IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK
;
743 k
->set_atn(s
, 1, attn_irq_enabled(ibs
));
744 sensor_set_discrete_bit(ibs
, IPMI_WATCHDOG_SENSOR
, 8, 1,
745 0xc8, (3 << 4) | 0xf, 0xff);
752 ibs
->watchdog_preaction_ran
= 1;
753 /* Issued the pretimeout, do the rest of the timeout now. */
754 ibs
->watchdog_expiry
= ipmi_getmonotime();
755 ibs
->watchdog_expiry
+= ibs
->watchdog_pretimeout
* 1000000000LL;
760 ibs
->watchdog_running
= 0; /* Stop the watchdog on a timeout */
761 ibs
->watchdog_expired
|= (1 << IPMI_BMC_WATCHDOG_GET_USE(ibs
));
762 switch (IPMI_BMC_WATCHDOG_GET_ACTION(ibs
)) {
763 case IPMI_BMC_WATCHDOG_ACTION_NONE
:
764 sensor_set_discrete_bit(ibs
, IPMI_WATCHDOG_SENSOR
, 0, 1,
765 0xc0, ibs
->watchdog_use
& 0xf, 0xff);
768 case IPMI_BMC_WATCHDOG_ACTION_RESET
:
769 sensor_set_discrete_bit(ibs
, IPMI_WATCHDOG_SENSOR
, 1, 1,
770 0xc1, ibs
->watchdog_use
& 0xf, 0xff);
771 k
->do_hw_op(s
, IPMI_RESET_CHASSIS
, 0);
774 case IPMI_BMC_WATCHDOG_ACTION_POWER_DOWN
:
775 sensor_set_discrete_bit(ibs
, IPMI_WATCHDOG_SENSOR
, 2, 1,
776 0xc2, ibs
->watchdog_use
& 0xf, 0xff);
777 k
->do_hw_op(s
, IPMI_POWEROFF_CHASSIS
, 0);
780 case IPMI_BMC_WATCHDOG_ACTION_POWER_CYCLE
:
781 sensor_set_discrete_bit(ibs
, IPMI_WATCHDOG_SENSOR
, 2, 1,
782 0xc3, ibs
->watchdog_use
& 0xf, 0xff);
783 k
->do_hw_op(s
, IPMI_POWERCYCLE_CHASSIS
, 0);
791 static void chassis_capabilities(IPMIBmcSim
*ibs
,
792 uint8_t *cmd
, unsigned int cmd_len
,
795 rsp_buffer_push(rsp
, 0);
796 rsp_buffer_push(rsp
, ibs
->parent
.slave_addr
);
797 rsp_buffer_push(rsp
, ibs
->parent
.slave_addr
);
798 rsp_buffer_push(rsp
, ibs
->parent
.slave_addr
);
799 rsp_buffer_push(rsp
, ibs
->parent
.slave_addr
);
802 static void chassis_status(IPMIBmcSim
*ibs
,
803 uint8_t *cmd
, unsigned int cmd_len
,
806 rsp_buffer_push(rsp
, 0x61); /* Unknown power restore, power is on */
807 rsp_buffer_push(rsp
, 0);
808 rsp_buffer_push(rsp
, 0);
809 rsp_buffer_push(rsp
, 0);
812 static void chassis_control(IPMIBmcSim
*ibs
,
813 uint8_t *cmd
, unsigned int cmd_len
,
816 IPMIInterface
*s
= ibs
->parent
.intf
;
817 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
819 switch (cmd
[2] & 0xf) {
820 case 0: /* power down */
821 rsp_buffer_set_error(rsp
, k
->do_hw_op(s
, IPMI_POWEROFF_CHASSIS
, 0));
823 case 1: /* power up */
824 rsp_buffer_set_error(rsp
, k
->do_hw_op(s
, IPMI_POWERON_CHASSIS
, 0));
826 case 2: /* power cycle */
827 rsp_buffer_set_error(rsp
, k
->do_hw_op(s
, IPMI_POWERCYCLE_CHASSIS
, 0));
829 case 3: /* hard reset */
830 rsp_buffer_set_error(rsp
, k
->do_hw_op(s
, IPMI_RESET_CHASSIS
, 0));
832 case 4: /* pulse diagnostic interrupt */
833 rsp_buffer_set_error(rsp
, k
->do_hw_op(s
, IPMI_PULSE_DIAG_IRQ
, 0));
835 case 5: /* soft shutdown via ACPI by overtemp emulation */
836 rsp_buffer_set_error(rsp
, k
->do_hw_op(s
,
837 IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP
, 0));
840 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
845 static void chassis_get_sys_restart_cause(IPMIBmcSim
*ibs
,
846 uint8_t *cmd
, unsigned int cmd_len
,
850 rsp_buffer_push(rsp
, ibs
->restart_cause
& 0xf); /* Restart Cause */
851 rsp_buffer_push(rsp
, 0); /* Channel 0 */
854 static void get_device_id(IPMIBmcSim
*ibs
,
855 uint8_t *cmd
, unsigned int cmd_len
,
858 rsp_buffer_push(rsp
, ibs
->device_id
);
859 rsp_buffer_push(rsp
, ibs
->device_rev
& 0xf);
860 rsp_buffer_push(rsp
, ibs
->fwrev1
& 0x7f);
861 rsp_buffer_push(rsp
, ibs
->fwrev2
);
862 rsp_buffer_push(rsp
, ibs
->ipmi_version
);
863 rsp_buffer_push(rsp
, 0x07); /* sensor, SDR, and SEL. */
864 rsp_buffer_push(rsp
, ibs
->mfg_id
[0]);
865 rsp_buffer_push(rsp
, ibs
->mfg_id
[1]);
866 rsp_buffer_push(rsp
, ibs
->mfg_id
[2]);
867 rsp_buffer_push(rsp
, ibs
->product_id
[0]);
868 rsp_buffer_push(rsp
, ibs
->product_id
[1]);
871 static void set_global_enables(IPMIBmcSim
*ibs
, uint8_t val
)
873 IPMIInterface
*s
= ibs
->parent
.intf
;
874 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
877 ibs
->bmc_global_enables
= val
;
879 irqs_on
= val
& (IPMI_BMC_EVBUF_FULL_INT_BIT
|
880 IPMI_BMC_RCV_MSG_QUEUE_INT_BIT
);
882 k
->set_irq_enable(s
, irqs_on
);
885 static void cold_reset(IPMIBmcSim
*ibs
,
886 uint8_t *cmd
, unsigned int cmd_len
,
889 IPMIInterface
*s
= ibs
->parent
.intf
;
890 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
892 /* Disable all interrupts */
893 set_global_enables(ibs
, 1 << IPMI_BMC_EVENT_LOG_BIT
);
900 static void warm_reset(IPMIBmcSim
*ibs
,
901 uint8_t *cmd
, unsigned int cmd_len
,
904 IPMIInterface
*s
= ibs
->parent
.intf
;
905 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
911 static void set_acpi_power_state(IPMIBmcSim
*ibs
,
912 uint8_t *cmd
, unsigned int cmd_len
,
915 ibs
->acpi_power_state
[0] = cmd
[2];
916 ibs
->acpi_power_state
[1] = cmd
[3];
919 static void get_acpi_power_state(IPMIBmcSim
*ibs
,
920 uint8_t *cmd
, unsigned int cmd_len
,
923 rsp_buffer_push(rsp
, ibs
->acpi_power_state
[0]);
924 rsp_buffer_push(rsp
, ibs
->acpi_power_state
[1]);
927 static void get_device_guid(IPMIBmcSim
*ibs
,
928 uint8_t *cmd
, unsigned int cmd_len
,
933 for (i
= 0; i
< 16; i
++) {
934 rsp_buffer_push(rsp
, ibs
->uuid
[i
]);
938 static void set_bmc_global_enables(IPMIBmcSim
*ibs
,
939 uint8_t *cmd
, unsigned int cmd_len
,
942 set_global_enables(ibs
, cmd
[2]);
945 static void get_bmc_global_enables(IPMIBmcSim
*ibs
,
946 uint8_t *cmd
, unsigned int cmd_len
,
949 rsp_buffer_push(rsp
, ibs
->bmc_global_enables
);
952 static void clr_msg_flags(IPMIBmcSim
*ibs
,
953 uint8_t *cmd
, unsigned int cmd_len
,
956 IPMIInterface
*s
= ibs
->parent
.intf
;
957 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
959 ibs
->msg_flags
&= ~cmd
[2];
960 k
->set_atn(s
, attn_set(ibs
), attn_irq_enabled(ibs
));
963 static void get_msg_flags(IPMIBmcSim
*ibs
,
964 uint8_t *cmd
, unsigned int cmd_len
,
967 rsp_buffer_push(rsp
, ibs
->msg_flags
);
970 static void read_evt_msg_buf(IPMIBmcSim
*ibs
,
971 uint8_t *cmd
, unsigned int cmd_len
,
974 IPMIInterface
*s
= ibs
->parent
.intf
;
975 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
978 if (!(ibs
->msg_flags
& IPMI_BMC_MSG_FLAG_EVT_BUF_FULL
)) {
979 rsp_buffer_set_error(rsp
, 0x80);
982 for (i
= 0; i
< 16; i
++) {
983 rsp_buffer_push(rsp
, ibs
->evtbuf
[i
]);
985 ibs
->msg_flags
&= ~IPMI_BMC_MSG_FLAG_EVT_BUF_FULL
;
986 k
->set_atn(s
, attn_set(ibs
), attn_irq_enabled(ibs
));
989 static void get_msg(IPMIBmcSim
*ibs
,
990 uint8_t *cmd
, unsigned int cmd_len
,
993 IPMIRcvBufEntry
*msg
;
995 if (QTAILQ_EMPTY(&ibs
->rcvbufs
)) {
996 rsp_buffer_set_error(rsp
, 0x80); /* Queue empty */
999 rsp_buffer_push(rsp
, 0); /* Channel 0 */
1000 msg
= QTAILQ_FIRST(&ibs
->rcvbufs
);
1001 rsp_buffer_pushmore(rsp
, msg
->buf
, msg
->len
);
1002 QTAILQ_REMOVE(&ibs
->rcvbufs
, msg
, entry
);
1005 if (QTAILQ_EMPTY(&ibs
->rcvbufs
)) {
1006 IPMIInterface
*s
= ibs
->parent
.intf
;
1007 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
1009 ibs
->msg_flags
&= ~IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE
;
1010 k
->set_atn(s
, attn_set(ibs
), attn_irq_enabled(ibs
));
1017 static unsigned char
1018 ipmb_checksum(unsigned char *data
, int size
, unsigned char csum
)
1020 for (; size
> 0; size
--, data
++) {
1027 static void send_msg(IPMIBmcSim
*ibs
,
1028 uint8_t *cmd
, unsigned int cmd_len
,
1031 IPMIInterface
*s
= ibs
->parent
.intf
;
1032 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
1033 IPMIRcvBufEntry
*msg
;
1035 uint8_t netfn
, rqLun
, rsLun
, rqSeq
;
1038 /* We only handle channel 0 with no options */
1039 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1044 rsp_buffer_set_error(rsp
, IPMI_CC_REQUEST_DATA_LENGTH_INVALID
);
1048 if (cmd
[3] != 0x40) {
1049 /* We only emulate a MC at address 0x40. */
1050 rsp_buffer_set_error(rsp
, 0x83); /* NAK on write */
1054 cmd
+= 3; /* Skip the header. */
1058 * At this point we "send" the message successfully. Any error will
1059 * be returned in the response.
1061 if (ipmb_checksum(cmd
, cmd_len
, 0) != 0 ||
1062 cmd
[3] != 0x20) { /* Improper response address */
1063 return; /* No response */
1066 netfn
= cmd
[1] >> 2;
1067 rqLun
= cmd
[4] & 0x3;
1068 rsLun
= cmd
[1] & 0x3;
1069 rqSeq
= cmd
[4] >> 2;
1072 /* We only support LUN 2 coming back to us. */
1076 msg
= g_malloc(sizeof(*msg
));
1077 msg
->buf
[0] = ((netfn
| 1) << 2) | rqLun
; /* NetFN, and make a response */
1078 msg
->buf
[1] = ipmb_checksum(msg
->buf
, 1, 0);
1079 msg
->buf
[2] = cmd
[0]; /* rsSA */
1080 msg
->buf
[3] = (rqSeq
<< 2) | rsLun
;
1081 msg
->buf
[4] = cmd
[5]; /* Cmd */
1082 msg
->buf
[5] = 0; /* Completion Code */
1085 if ((cmd
[1] >> 2) != IPMI_NETFN_APP
|| cmd
[5] != IPMI_CMD_GET_DEVICE_ID
) {
1086 /* Not a command we handle. */
1087 msg
->buf
[5] = IPMI_CC_INVALID_CMD
;
1091 buf
= msg
->buf
+ msg
->len
; /* After the CC */
1106 msg
->buf
[msg
->len
] = ipmb_checksum(msg
->buf
, msg
->len
, 0);
1108 QTAILQ_INSERT_TAIL(&ibs
->rcvbufs
, msg
, entry
);
1109 ibs
->msg_flags
|= IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE
;
1110 k
->set_atn(s
, 1, attn_irq_enabled(ibs
));
1113 static void do_watchdog_reset(IPMIBmcSim
*ibs
)
1115 if (IPMI_BMC_WATCHDOG_GET_ACTION(ibs
) ==
1116 IPMI_BMC_WATCHDOG_ACTION_NONE
) {
1117 ibs
->watchdog_running
= 0;
1120 ibs
->watchdog_preaction_ran
= 0;
1123 /* Timeout is in tenths of a second, offset is in seconds */
1124 ibs
->watchdog_expiry
= ipmi_getmonotime();
1125 ibs
->watchdog_expiry
+= ibs
->watchdog_timeout
* 100000000LL;
1126 if (IPMI_BMC_WATCHDOG_GET_PRE_ACTION(ibs
) != IPMI_BMC_WATCHDOG_PRE_NONE
) {
1127 ibs
->watchdog_expiry
-= ibs
->watchdog_pretimeout
* 1000000000LL;
1129 ibs
->watchdog_running
= 1;
1132 static void reset_watchdog_timer(IPMIBmcSim
*ibs
,
1133 uint8_t *cmd
, unsigned int cmd_len
,
1136 if (!ibs
->watchdog_initialized
) {
1137 rsp_buffer_set_error(rsp
, 0x80);
1140 do_watchdog_reset(ibs
);
1143 static void set_watchdog_timer(IPMIBmcSim
*ibs
,
1144 uint8_t *cmd
, unsigned int cmd_len
,
1147 IPMIInterface
*s
= ibs
->parent
.intf
;
1148 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
1151 val
= cmd
[2] & 0x7; /* Validate use */
1152 if (val
== 0 || val
> 5) {
1153 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1156 val
= cmd
[3] & 0x7; /* Validate action */
1158 case IPMI_BMC_WATCHDOG_ACTION_NONE
:
1161 case IPMI_BMC_WATCHDOG_ACTION_RESET
:
1162 rsp_buffer_set_error(rsp
, k
->do_hw_op(s
, IPMI_RESET_CHASSIS
, 1));
1165 case IPMI_BMC_WATCHDOG_ACTION_POWER_DOWN
:
1166 rsp_buffer_set_error(rsp
, k
->do_hw_op(s
, IPMI_POWEROFF_CHASSIS
, 1));
1169 case IPMI_BMC_WATCHDOG_ACTION_POWER_CYCLE
:
1170 rsp_buffer_set_error(rsp
, k
->do_hw_op(s
, IPMI_POWERCYCLE_CHASSIS
, 1));
1174 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1176 if (rsp
->buffer
[2]) {
1177 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1181 val
= (cmd
[3] >> 4) & 0x7; /* Validate preaction */
1183 case IPMI_BMC_WATCHDOG_PRE_MSG_INT
:
1184 case IPMI_BMC_WATCHDOG_PRE_NONE
:
1187 case IPMI_BMC_WATCHDOG_PRE_NMI
:
1188 if (!k
->do_hw_op(s
, IPMI_SEND_NMI
, 1)) {
1189 /* NMI not supported. */
1190 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1196 /* We don't support PRE_SMI */
1197 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1201 ibs
->watchdog_initialized
= 1;
1202 ibs
->watchdog_use
= cmd
[2] & IPMI_BMC_WATCHDOG_USE_MASK
;
1203 ibs
->watchdog_action
= cmd
[3] & IPMI_BMC_WATCHDOG_ACTION_MASK
;
1204 ibs
->watchdog_pretimeout
= cmd
[4];
1205 ibs
->watchdog_expired
&= ~cmd
[5];
1206 ibs
->watchdog_timeout
= cmd
[6] | (((uint16_t) cmd
[7]) << 8);
1207 if (ibs
->watchdog_running
& IPMI_BMC_WATCHDOG_GET_DONT_STOP(ibs
)) {
1208 do_watchdog_reset(ibs
);
1210 ibs
->watchdog_running
= 0;
1214 static void get_watchdog_timer(IPMIBmcSim
*ibs
,
1215 uint8_t *cmd
, unsigned int cmd_len
,
1218 rsp_buffer_push(rsp
, ibs
->watchdog_use
);
1219 rsp_buffer_push(rsp
, ibs
->watchdog_action
);
1220 rsp_buffer_push(rsp
, ibs
->watchdog_pretimeout
);
1221 rsp_buffer_push(rsp
, ibs
->watchdog_expired
);
1222 if (ibs
->watchdog_running
) {
1224 timeout
= ((ibs
->watchdog_expiry
- ipmi_getmonotime() + 50000000)
1226 rsp_buffer_push(rsp
, timeout
& 0xff);
1227 rsp_buffer_push(rsp
, (timeout
>> 8) & 0xff);
1229 rsp_buffer_push(rsp
, 0);
1230 rsp_buffer_push(rsp
, 0);
1234 static void get_sdr_rep_info(IPMIBmcSim
*ibs
,
1235 uint8_t *cmd
, unsigned int cmd_len
,
1240 rsp_buffer_push(rsp
, 0x51); /* Conform to IPMI 1.5 spec */
1241 rsp_buffer_push(rsp
, ibs
->sdr
.next_rec_id
& 0xff);
1242 rsp_buffer_push(rsp
, (ibs
->sdr
.next_rec_id
>> 8) & 0xff);
1243 rsp_buffer_push(rsp
, (MAX_SDR_SIZE
- ibs
->sdr
.next_free
) & 0xff);
1244 rsp_buffer_push(rsp
, ((MAX_SDR_SIZE
- ibs
->sdr
.next_free
) >> 8) & 0xff);
1245 for (i
= 0; i
< 4; i
++) {
1246 rsp_buffer_push(rsp
, ibs
->sdr
.last_addition
[i
]);
1248 for (i
= 0; i
< 4; i
++) {
1249 rsp_buffer_push(rsp
, ibs
->sdr
.last_clear
[i
]);
1251 /* Only modal support, reserve supported */
1252 rsp_buffer_push(rsp
, (ibs
->sdr
.overflow
<< 7) | 0x22);
1255 static void reserve_sdr_rep(IPMIBmcSim
*ibs
,
1256 uint8_t *cmd
, unsigned int cmd_len
,
1259 rsp_buffer_push(rsp
, ibs
->sdr
.reservation
& 0xff);
1260 rsp_buffer_push(rsp
, (ibs
->sdr
.reservation
>> 8) & 0xff);
1263 static void get_sdr(IPMIBmcSim
*ibs
,
1264 uint8_t *cmd
, unsigned int cmd_len
,
1269 struct ipmi_sdr_header
*sdrh
;
1272 if ((cmd
[2] | (cmd
[3] << 8)) != ibs
->sdr
.reservation
) {
1273 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_RESERVATION
);
1279 if (sdr_find_entry(&ibs
->sdr
, cmd
[4] | (cmd
[5] << 8),
1281 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1285 sdrh
= (struct ipmi_sdr_header
*) &ibs
->sdr
.sdr
[pos
];
1287 if (cmd
[6] > ipmi_sdr_length(sdrh
)) {
1288 rsp_buffer_set_error(rsp
, IPMI_CC_PARM_OUT_OF_RANGE
);
1292 rsp_buffer_push(rsp
, nextrec
& 0xff);
1293 rsp_buffer_push(rsp
, (nextrec
>> 8) & 0xff);
1295 if (cmd
[7] == 0xff) {
1296 cmd
[7] = ipmi_sdr_length(sdrh
) - cmd
[6];
1299 if ((cmd
[7] + rsp
->len
) > sizeof(rsp
->buffer
)) {
1300 rsp_buffer_set_error(rsp
, IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES
);
1304 rsp_buffer_pushmore(rsp
, ibs
->sdr
.sdr
+ pos
+ cmd
[6], cmd
[7]);
1307 static void add_sdr(IPMIBmcSim
*ibs
,
1308 uint8_t *cmd
, unsigned int cmd_len
,
1312 struct ipmi_sdr_header
*sdrh
= (struct ipmi_sdr_header
*) cmd
+ 2;
1314 if (sdr_add_entry(ibs
, sdrh
, cmd_len
- 2, &recid
)) {
1315 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1318 rsp_buffer_push(rsp
, recid
& 0xff);
1319 rsp_buffer_push(rsp
, (recid
>> 8) & 0xff);
1322 static void clear_sdr_rep(IPMIBmcSim
*ibs
,
1323 uint8_t *cmd
, unsigned int cmd_len
,
1326 if ((cmd
[2] | (cmd
[3] << 8)) != ibs
->sdr
.reservation
) {
1327 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_RESERVATION
);
1331 if (cmd
[4] != 'C' || cmd
[5] != 'L' || cmd
[6] != 'R') {
1332 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1335 if (cmd
[7] == 0xaa) {
1336 ibs
->sdr
.next_free
= 0;
1337 ibs
->sdr
.overflow
= 0;
1338 set_timestamp(ibs
, ibs
->sdr
.last_clear
);
1339 rsp_buffer_push(rsp
, 1); /* Erasure complete */
1340 sdr_inc_reservation(&ibs
->sdr
);
1341 } else if (cmd
[7] == 0) {
1342 rsp_buffer_push(rsp
, 1); /* Erasure complete */
1344 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1349 static void get_sel_info(IPMIBmcSim
*ibs
,
1350 uint8_t *cmd
, unsigned int cmd_len
,
1353 unsigned int i
, val
;
1355 rsp_buffer_push(rsp
, 0x51); /* Conform to IPMI 1.5 */
1356 rsp_buffer_push(rsp
, ibs
->sel
.next_free
& 0xff);
1357 rsp_buffer_push(rsp
, (ibs
->sel
.next_free
>> 8) & 0xff);
1358 val
= (MAX_SEL_SIZE
- ibs
->sel
.next_free
) * 16;
1359 rsp_buffer_push(rsp
, val
& 0xff);
1360 rsp_buffer_push(rsp
, (val
>> 8) & 0xff);
1361 for (i
= 0; i
< 4; i
++) {
1362 rsp_buffer_push(rsp
, ibs
->sel
.last_addition
[i
]);
1364 for (i
= 0; i
< 4; i
++) {
1365 rsp_buffer_push(rsp
, ibs
->sel
.last_clear
[i
]);
1367 /* Only support Reserve SEL */
1368 rsp_buffer_push(rsp
, (ibs
->sel
.overflow
<< 7) | 0x02);
1371 static void get_fru_area_info(IPMIBmcSim
*ibs
,
1372 uint8_t *cmd
, unsigned int cmd_len
,
1376 uint16_t fru_entry_size
;
1380 if (fruid
>= ibs
->fru
.nentries
) {
1381 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1385 fru_entry_size
= ibs
->fru
.areasize
;
1387 rsp_buffer_push(rsp
, fru_entry_size
& 0xff);
1388 rsp_buffer_push(rsp
, fru_entry_size
>> 8 & 0xff);
1389 rsp_buffer_push(rsp
, 0x0);
1392 static void read_fru_data(IPMIBmcSim
*ibs
,
1393 uint8_t *cmd
, unsigned int cmd_len
,
1403 offset
= (cmd
[3] | cmd
[4] << 8);
1405 if (fruid
>= ibs
->fru
.nentries
) {
1406 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1410 if (offset
>= ibs
->fru
.areasize
- 1) {
1411 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1415 fru_entry
= &ibs
->fru
.data
[fruid
* ibs
->fru
.areasize
];
1417 count
= MIN(cmd
[5], ibs
->fru
.areasize
- offset
);
1419 rsp_buffer_push(rsp
, count
& 0xff);
1420 for (i
= 0; i
< count
; i
++) {
1421 rsp_buffer_push(rsp
, fru_entry
[offset
+ i
]);
1425 static void write_fru_data(IPMIBmcSim
*ibs
,
1426 uint8_t *cmd
, unsigned int cmd_len
,
1435 offset
= (cmd
[3] | cmd
[4] << 8);
1437 if (fruid
>= ibs
->fru
.nentries
) {
1438 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1442 if (offset
>= ibs
->fru
.areasize
- 1) {
1443 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1447 fru_entry
= &ibs
->fru
.data
[fruid
* ibs
->fru
.areasize
];
1449 count
= MIN(cmd_len
- 5, ibs
->fru
.areasize
- offset
);
1451 memcpy(fru_entry
+ offset
, cmd
+ 5, count
);
1453 rsp_buffer_push(rsp
, count
& 0xff);
1456 static void reserve_sel(IPMIBmcSim
*ibs
,
1457 uint8_t *cmd
, unsigned int cmd_len
,
1460 rsp_buffer_push(rsp
, ibs
->sel
.reservation
& 0xff);
1461 rsp_buffer_push(rsp
, (ibs
->sel
.reservation
>> 8) & 0xff);
1464 static void get_sel_entry(IPMIBmcSim
*ibs
,
1465 uint8_t *cmd
, unsigned int cmd_len
,
1471 if ((cmd
[2] | (cmd
[3] << 8)) != ibs
->sel
.reservation
) {
1472 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_RESERVATION
);
1476 if (ibs
->sel
.next_free
== 0) {
1477 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1481 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1484 if (cmd
[7] == 0xff) {
1486 } else if ((cmd
[7] + cmd
[6]) > 16) {
1487 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1493 val
= cmd
[4] | (cmd
[5] << 8);
1494 if (val
== 0xffff) {
1495 val
= ibs
->sel
.next_free
- 1;
1496 } else if (val
>= ibs
->sel
.next_free
) {
1497 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1500 if ((val
+ 1) == ibs
->sel
.next_free
) {
1501 rsp_buffer_push(rsp
, 0xff);
1502 rsp_buffer_push(rsp
, 0xff);
1504 rsp_buffer_push(rsp
, (val
+ 1) & 0xff);
1505 rsp_buffer_push(rsp
, ((val
+ 1) >> 8) & 0xff);
1507 for (; cmd
[6] < cmd
[7]; cmd
[6]++) {
1508 rsp_buffer_push(rsp
, ibs
->sel
.sel
[val
][cmd
[6]]);
1512 static void add_sel_entry(IPMIBmcSim
*ibs
,
1513 uint8_t *cmd
, unsigned int cmd_len
,
1516 if (sel_add_event(ibs
, cmd
+ 2)) {
1517 rsp_buffer_set_error(rsp
, IPMI_CC_OUT_OF_SPACE
);
1520 /* sel_add_event fills in the record number. */
1521 rsp_buffer_push(rsp
, cmd
[2]);
1522 rsp_buffer_push(rsp
, cmd
[3]);
1525 static void clear_sel(IPMIBmcSim
*ibs
,
1526 uint8_t *cmd
, unsigned int cmd_len
,
1529 if ((cmd
[2] | (cmd
[3] << 8)) != ibs
->sel
.reservation
) {
1530 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_RESERVATION
);
1534 if (cmd
[4] != 'C' || cmd
[5] != 'L' || cmd
[6] != 'R') {
1535 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1538 if (cmd
[7] == 0xaa) {
1539 ibs
->sel
.next_free
= 0;
1540 ibs
->sel
.overflow
= 0;
1541 set_timestamp(ibs
, ibs
->sdr
.last_clear
);
1542 rsp_buffer_push(rsp
, 1); /* Erasure complete */
1543 sel_inc_reservation(&ibs
->sel
);
1544 } else if (cmd
[7] == 0) {
1545 rsp_buffer_push(rsp
, 1); /* Erasure complete */
1547 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1552 static void get_sel_time(IPMIBmcSim
*ibs
,
1553 uint8_t *cmd
, unsigned int cmd_len
,
1557 struct ipmi_time now
;
1560 val
= now
.tv_sec
+ ibs
->sel
.time_offset
;
1561 rsp_buffer_push(rsp
, val
& 0xff);
1562 rsp_buffer_push(rsp
, (val
>> 8) & 0xff);
1563 rsp_buffer_push(rsp
, (val
>> 16) & 0xff);
1564 rsp_buffer_push(rsp
, (val
>> 24) & 0xff);
1567 static void set_sel_time(IPMIBmcSim
*ibs
,
1568 uint8_t *cmd
, unsigned int cmd_len
,
1572 struct ipmi_time now
;
1574 val
= cmd
[2] | (cmd
[3] << 8) | (cmd
[4] << 16) | (cmd
[5] << 24);
1576 ibs
->sel
.time_offset
= now
.tv_sec
- ((long) val
);
1579 static void set_sensor_evt_enable(IPMIBmcSim
*ibs
,
1580 uint8_t *cmd
, unsigned int cmd_len
,
1585 if ((cmd
[2] >= MAX_SENSORS
) ||
1586 !IPMI_SENSOR_GET_PRESENT(ibs
->sensors
+ cmd
[2])) {
1587 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1590 sens
= ibs
->sensors
+ cmd
[2];
1591 switch ((cmd
[3] >> 4) & 0x3) {
1592 case 0: /* Do not change */
1594 case 1: /* Enable bits */
1596 sens
->assert_enable
|= cmd
[4];
1599 sens
->assert_enable
|= cmd
[5] << 8;
1602 sens
->deassert_enable
|= cmd
[6];
1605 sens
->deassert_enable
|= cmd
[7] << 8;
1608 case 2: /* Disable bits */
1610 sens
->assert_enable
&= ~cmd
[4];
1613 sens
->assert_enable
&= ~(cmd
[5] << 8);
1616 sens
->deassert_enable
&= ~cmd
[6];
1619 sens
->deassert_enable
&= ~(cmd
[7] << 8);
1623 rsp_buffer_set_error(rsp
, IPMI_CC_INVALID_DATA_FIELD
);
1626 IPMI_SENSOR_SET_RET_STATUS(sens
, cmd
[3]);
1629 static void get_sensor_evt_enable(IPMIBmcSim
*ibs
,
1630 uint8_t *cmd
, unsigned int cmd_len
,
1635 if ((cmd
[2] >= MAX_SENSORS
) ||
1636 !IPMI_SENSOR_GET_PRESENT(ibs
->sensors
+ cmd
[2])) {
1637 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1640 sens
= ibs
->sensors
+ cmd
[2];
1641 rsp_buffer_push(rsp
, IPMI_SENSOR_GET_RET_STATUS(sens
));
1642 rsp_buffer_push(rsp
, sens
->assert_enable
& 0xff);
1643 rsp_buffer_push(rsp
, (sens
->assert_enable
>> 8) & 0xff);
1644 rsp_buffer_push(rsp
, sens
->deassert_enable
& 0xff);
1645 rsp_buffer_push(rsp
, (sens
->deassert_enable
>> 8) & 0xff);
1648 static void rearm_sensor_evts(IPMIBmcSim
*ibs
,
1649 uint8_t *cmd
, unsigned int cmd_len
,
1654 if ((cmd
[2] >= MAX_SENSORS
) ||
1655 !IPMI_SENSOR_GET_PRESENT(ibs
->sensors
+ cmd
[2])) {
1656 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1659 sens
= ibs
->sensors
+ cmd
[2];
1661 if ((cmd
[3] & 0x80) == 0) {
1662 /* Just clear everything */
1668 static void get_sensor_evt_status(IPMIBmcSim
*ibs
,
1669 uint8_t *cmd
, unsigned int cmd_len
,
1674 if ((cmd
[2] >= MAX_SENSORS
) ||
1675 !IPMI_SENSOR_GET_PRESENT(ibs
->sensors
+ cmd
[2])) {
1676 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1679 sens
= ibs
->sensors
+ cmd
[2];
1680 rsp_buffer_push(rsp
, sens
->reading
);
1681 rsp_buffer_push(rsp
, IPMI_SENSOR_GET_RET_STATUS(sens
));
1682 rsp_buffer_push(rsp
, sens
->assert_states
& 0xff);
1683 rsp_buffer_push(rsp
, (sens
->assert_states
>> 8) & 0xff);
1684 rsp_buffer_push(rsp
, sens
->deassert_states
& 0xff);
1685 rsp_buffer_push(rsp
, (sens
->deassert_states
>> 8) & 0xff);
1688 static void get_sensor_reading(IPMIBmcSim
*ibs
,
1689 uint8_t *cmd
, unsigned int cmd_len
,
1694 if ((cmd
[2] >= MAX_SENSORS
) ||
1695 !IPMI_SENSOR_GET_PRESENT(ibs
->sensors
+ cmd
[2])) {
1696 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1699 sens
= ibs
->sensors
+ cmd
[2];
1700 rsp_buffer_push(rsp
, sens
->reading
);
1701 rsp_buffer_push(rsp
, IPMI_SENSOR_GET_RET_STATUS(sens
));
1702 rsp_buffer_push(rsp
, sens
->states
& 0xff);
1703 if (IPMI_SENSOR_IS_DISCRETE(sens
)) {
1704 rsp_buffer_push(rsp
, (sens
->states
>> 8) & 0xff);
1708 static void set_sensor_type(IPMIBmcSim
*ibs
,
1709 uint8_t *cmd
, unsigned int cmd_len
,
1715 if ((cmd
[2] >= MAX_SENSORS
) ||
1716 !IPMI_SENSOR_GET_PRESENT(ibs
->sensors
+ cmd
[2])) {
1717 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1720 sens
= ibs
->sensors
+ cmd
[2];
1721 sens
->sensor_type
= cmd
[3];
1722 sens
->evt_reading_type_code
= cmd
[4] & 0x7f;
1725 static void get_sensor_type(IPMIBmcSim
*ibs
,
1726 uint8_t *cmd
, unsigned int cmd_len
,
1732 if ((cmd
[2] >= MAX_SENSORS
) ||
1733 !IPMI_SENSOR_GET_PRESENT(ibs
->sensors
+ cmd
[2])) {
1734 rsp_buffer_set_error(rsp
, IPMI_CC_REQ_ENTRY_NOT_PRESENT
);
1737 sens
= ibs
->sensors
+ cmd
[2];
1738 rsp_buffer_push(rsp
, sens
->sensor_type
);
1739 rsp_buffer_push(rsp
, sens
->evt_reading_type_code
);
1743 static const IPMICmdHandler chassis_cmds
[] = {
1744 [IPMI_CMD_GET_CHASSIS_CAPABILITIES
] = { chassis_capabilities
},
1745 [IPMI_CMD_GET_CHASSIS_STATUS
] = { chassis_status
},
1746 [IPMI_CMD_CHASSIS_CONTROL
] = { chassis_control
, 3 },
1747 [IPMI_CMD_GET_SYS_RESTART_CAUSE
] = { chassis_get_sys_restart_cause
}
1749 static const IPMINetfn chassis_netfn
= {
1750 .cmd_nums
= ARRAY_SIZE(chassis_cmds
),
1751 .cmd_handlers
= chassis_cmds
1754 static const IPMICmdHandler sensor_event_cmds
[] = {
1755 [IPMI_CMD_SET_SENSOR_EVT_ENABLE
] = { set_sensor_evt_enable
, 4 },
1756 [IPMI_CMD_GET_SENSOR_EVT_ENABLE
] = { get_sensor_evt_enable
, 3 },
1757 [IPMI_CMD_REARM_SENSOR_EVTS
] = { rearm_sensor_evts
, 4 },
1758 [IPMI_CMD_GET_SENSOR_EVT_STATUS
] = { get_sensor_evt_status
, 3 },
1759 [IPMI_CMD_GET_SENSOR_READING
] = { get_sensor_reading
, 3 },
1760 [IPMI_CMD_SET_SENSOR_TYPE
] = { set_sensor_type
, 5 },
1761 [IPMI_CMD_GET_SENSOR_TYPE
] = { get_sensor_type
, 3 },
1763 static const IPMINetfn sensor_event_netfn
= {
1764 .cmd_nums
= ARRAY_SIZE(sensor_event_cmds
),
1765 .cmd_handlers
= sensor_event_cmds
1768 static const IPMICmdHandler app_cmds
[] = {
1769 [IPMI_CMD_GET_DEVICE_ID
] = { get_device_id
},
1770 [IPMI_CMD_COLD_RESET
] = { cold_reset
},
1771 [IPMI_CMD_WARM_RESET
] = { warm_reset
},
1772 [IPMI_CMD_SET_ACPI_POWER_STATE
] = { set_acpi_power_state
, 4 },
1773 [IPMI_CMD_GET_ACPI_POWER_STATE
] = { get_acpi_power_state
},
1774 [IPMI_CMD_GET_DEVICE_GUID
] = { get_device_guid
},
1775 [IPMI_CMD_SET_BMC_GLOBAL_ENABLES
] = { set_bmc_global_enables
, 3 },
1776 [IPMI_CMD_GET_BMC_GLOBAL_ENABLES
] = { get_bmc_global_enables
},
1777 [IPMI_CMD_CLR_MSG_FLAGS
] = { clr_msg_flags
, 3 },
1778 [IPMI_CMD_GET_MSG_FLAGS
] = { get_msg_flags
},
1779 [IPMI_CMD_GET_MSG
] = { get_msg
},
1780 [IPMI_CMD_SEND_MSG
] = { send_msg
, 3 },
1781 [IPMI_CMD_READ_EVT_MSG_BUF
] = { read_evt_msg_buf
},
1782 [IPMI_CMD_RESET_WATCHDOG_TIMER
] = { reset_watchdog_timer
},
1783 [IPMI_CMD_SET_WATCHDOG_TIMER
] = { set_watchdog_timer
, 8 },
1784 [IPMI_CMD_GET_WATCHDOG_TIMER
] = { get_watchdog_timer
},
1786 static const IPMINetfn app_netfn
= {
1787 .cmd_nums
= ARRAY_SIZE(app_cmds
),
1788 .cmd_handlers
= app_cmds
1791 static const IPMICmdHandler storage_cmds
[] = {
1792 [IPMI_CMD_GET_FRU_AREA_INFO
] = { get_fru_area_info
, 3 },
1793 [IPMI_CMD_READ_FRU_DATA
] = { read_fru_data
, 5 },
1794 [IPMI_CMD_WRITE_FRU_DATA
] = { write_fru_data
, 5 },
1795 [IPMI_CMD_GET_SDR_REP_INFO
] = { get_sdr_rep_info
},
1796 [IPMI_CMD_RESERVE_SDR_REP
] = { reserve_sdr_rep
},
1797 [IPMI_CMD_GET_SDR
] = { get_sdr
, 8 },
1798 [IPMI_CMD_ADD_SDR
] = { add_sdr
},
1799 [IPMI_CMD_CLEAR_SDR_REP
] = { clear_sdr_rep
, 8 },
1800 [IPMI_CMD_GET_SEL_INFO
] = { get_sel_info
},
1801 [IPMI_CMD_RESERVE_SEL
] = { reserve_sel
},
1802 [IPMI_CMD_GET_SEL_ENTRY
] = { get_sel_entry
, 8 },
1803 [IPMI_CMD_ADD_SEL_ENTRY
] = { add_sel_entry
, 18 },
1804 [IPMI_CMD_CLEAR_SEL
] = { clear_sel
, 8 },
1805 [IPMI_CMD_GET_SEL_TIME
] = { get_sel_time
, 6 },
1806 [IPMI_CMD_SET_SEL_TIME
] = { set_sel_time
},
1809 static const IPMINetfn storage_netfn
= {
1810 .cmd_nums
= ARRAY_SIZE(storage_cmds
),
1811 .cmd_handlers
= storage_cmds
1814 static void register_cmds(IPMIBmcSim
*s
)
1816 ipmi_register_netfn(s
, IPMI_NETFN_CHASSIS
, &chassis_netfn
);
1817 ipmi_register_netfn(s
, IPMI_NETFN_SENSOR_EVENT
, &sensor_event_netfn
);
1818 ipmi_register_netfn(s
, IPMI_NETFN_APP
, &app_netfn
);
1819 ipmi_register_netfn(s
, IPMI_NETFN_STORAGE
, &storage_netfn
);
1822 static uint8_t init_sdrs
[] = {
1823 /* Watchdog device */
1824 0x00, 0x00, 0x51, 0x02, 35, 0x20, 0x00, 0x00,
1825 0x23, 0x01, 0x63, 0x00, 0x23, 0x6f, 0x0f, 0x01,
1826 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1827 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8,
1828 'W', 'a', 't', 'c', 'h', 'd', 'o', 'g',
1831 static void ipmi_sdr_init(IPMIBmcSim
*ibs
)
1838 sdrs_size
= sizeof(init_sdrs
);
1840 if (ibs
->sdr_filename
&&
1841 !g_file_get_contents(ibs
->sdr_filename
, (gchar
**) &sdrs
, &sdrs_size
,
1843 error_report("failed to load sdr file '%s'", ibs
->sdr_filename
);
1844 sdrs_size
= sizeof(init_sdrs
);
1848 for (i
= 0; i
< sdrs_size
; i
+= len
) {
1849 struct ipmi_sdr_header
*sdrh
;
1851 if (i
+ IPMI_SDR_HEADER_SIZE
> sdrs_size
) {
1852 error_report("Problem with recid 0x%4.4x", i
);
1855 sdrh
= (struct ipmi_sdr_header
*) &sdrs
[i
];
1856 len
= ipmi_sdr_length(sdrh
);
1857 if (i
+ len
> sdrs_size
) {
1858 error_report("Problem with recid 0x%4.4x", i
);
1861 sdr_add_entry(ibs
, sdrh
, len
, NULL
);
1864 if (sdrs
!= init_sdrs
) {
1869 static const VMStateDescription vmstate_ipmi_sim
= {
1870 .name
= TYPE_IPMI_BMC_SIMULATOR
,
1872 .minimum_version_id
= 1,
1873 .fields
= (VMStateField
[]) {
1874 VMSTATE_UINT8(bmc_global_enables
, IPMIBmcSim
),
1875 VMSTATE_UINT8(msg_flags
, IPMIBmcSim
),
1876 VMSTATE_BOOL(watchdog_initialized
, IPMIBmcSim
),
1877 VMSTATE_UINT8(watchdog_use
, IPMIBmcSim
),
1878 VMSTATE_UINT8(watchdog_action
, IPMIBmcSim
),
1879 VMSTATE_UINT8(watchdog_pretimeout
, IPMIBmcSim
),
1880 VMSTATE_BOOL(watchdog_expired
, IPMIBmcSim
),
1881 VMSTATE_UINT16(watchdog_timeout
, IPMIBmcSim
),
1882 VMSTATE_BOOL(watchdog_running
, IPMIBmcSim
),
1883 VMSTATE_BOOL(watchdog_preaction_ran
, IPMIBmcSim
),
1884 VMSTATE_INT64(watchdog_expiry
, IPMIBmcSim
),
1885 VMSTATE_UINT8_ARRAY(evtbuf
, IPMIBmcSim
, 16),
1886 VMSTATE_UINT8(sensors
[IPMI_WATCHDOG_SENSOR
].status
, IPMIBmcSim
),
1887 VMSTATE_UINT8(sensors
[IPMI_WATCHDOG_SENSOR
].reading
, IPMIBmcSim
),
1888 VMSTATE_UINT16(sensors
[IPMI_WATCHDOG_SENSOR
].states
, IPMIBmcSim
),
1889 VMSTATE_UINT16(sensors
[IPMI_WATCHDOG_SENSOR
].assert_states
, IPMIBmcSim
),
1890 VMSTATE_UINT16(sensors
[IPMI_WATCHDOG_SENSOR
].deassert_states
,
1892 VMSTATE_UINT16(sensors
[IPMI_WATCHDOG_SENSOR
].assert_enable
, IPMIBmcSim
),
1893 VMSTATE_END_OF_LIST()
1897 static void ipmi_fru_init(IPMIFru
*fru
)
1902 if (!fru
->filename
) {
1906 fsize
= get_image_size(fru
->filename
);
1908 size
= QEMU_ALIGN_UP(fsize
, fru
->areasize
);
1909 fru
->data
= g_malloc0(size
);
1910 if (load_image_size(fru
->filename
, fru
->data
, fsize
) != fsize
) {
1911 error_report("Could not load file '%s'", fru
->filename
);
1919 /* give one default FRU */
1920 size
= fru
->areasize
;
1921 fru
->data
= g_malloc0(size
);
1924 fru
->nentries
= size
/ fru
->areasize
;
1927 static void ipmi_sim_realize(DeviceState
*dev
, Error
**errp
)
1929 IPMIBmc
*b
= IPMI_BMC(dev
);
1931 IPMIBmcSim
*ibs
= IPMI_BMC_SIMULATOR(b
);
1933 QTAILQ_INIT(&ibs
->rcvbufs
);
1935 ibs
->bmc_global_enables
= (1 << IPMI_BMC_EVENT_LOG_BIT
);
1936 ibs
->device_id
= 0x20;
1937 ibs
->ipmi_version
= 0x02; /* IPMI 2.0 */
1938 ibs
->restart_cause
= 0;
1939 for (i
= 0; i
< 4; i
++) {
1940 ibs
->sel
.last_addition
[i
] = 0xff;
1941 ibs
->sel
.last_clear
[i
] = 0xff;
1942 ibs
->sdr
.last_addition
[i
] = 0xff;
1943 ibs
->sdr
.last_clear
[i
] = 0xff;
1948 ipmi_fru_init(&ibs
->fru
);
1950 ibs
->acpi_power_state
[0] = 0;
1951 ibs
->acpi_power_state
[1] = 0;
1953 if (qemu_uuid_set
) {
1954 memcpy(&ibs
->uuid
, &qemu_uuid
, 16);
1956 memset(&ibs
->uuid
, 0, 16);
1959 ipmi_init_sensors_from_sdrs(ibs
);
1962 ibs
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ipmi_timeout
, ibs
);
1964 vmstate_register(NULL
, 0, &vmstate_ipmi_sim
, ibs
);
1967 static Property ipmi_sim_properties
[] = {
1968 DEFINE_PROP_UINT16("fruareasize", IPMIBmcSim
, fru
.areasize
, 1024),
1969 DEFINE_PROP_STRING("frudatafile", IPMIBmcSim
, fru
.filename
),
1970 DEFINE_PROP_STRING("sdrfile", IPMIBmcSim
, sdr_filename
),
1971 DEFINE_PROP_END_OF_LIST(),
1974 static void ipmi_sim_class_init(ObjectClass
*oc
, void *data
)
1976 DeviceClass
*dc
= DEVICE_CLASS(oc
);
1977 IPMIBmcClass
*bk
= IPMI_BMC_CLASS(oc
);
1979 dc
->hotpluggable
= false;
1980 dc
->realize
= ipmi_sim_realize
;
1981 dc
->props
= ipmi_sim_properties
;
1982 bk
->handle_command
= ipmi_sim_handle_command
;
1985 static const TypeInfo ipmi_sim_type
= {
1986 .name
= TYPE_IPMI_BMC_SIMULATOR
,
1987 .parent
= TYPE_IPMI_BMC
,
1988 .instance_size
= sizeof(IPMIBmcSim
),
1989 .class_init
= ipmi_sim_class_init
,
1992 static void ipmi_sim_register_types(void)
1994 type_register_static(&ipmi_sim_type
);
1997 type_init(ipmi_sim_register_types
)