2 * IPMI BT test cases, using the external interface for checking
4 * Copyright (c) 2012 Corey Minyard <cminyard@mvista.com>
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"
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
30 #include <netinet/tcp.h>
33 #include "libqtest-single.h"
34 #include "qemu-common.h"
38 #define IPMI_BT_BASE 0xe4
40 #define IPMI_BT_CTLREG_CLR_WR_PTR 0
41 #define IPMI_BT_CTLREG_CLR_RD_PTR 1
42 #define IPMI_BT_CTLREG_H2B_ATN 2
43 #define IPMI_BT_CTLREG_B2H_ATN 3
44 #define IPMI_BT_CTLREG_SMS_ATN 4
45 #define IPMI_BT_CTLREG_H_BUSY 6
46 #define IPMI_BT_CTLREG_B_BUSY 7
48 #define IPMI_BT_CTLREG_GET(b) ((bt_get_ctrlreg() >> (b)) & 1)
49 #define IPMI_BT_CTLREG_GET_H2B_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_H2B_ATN)
50 #define IPMI_BT_CTLREG_GET_B2H_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_B2H_ATN)
51 #define IPMI_BT_CTLREG_GET_SMS_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_SMS_ATN)
52 #define IPMI_BT_CTLREG_GET_H_BUSY() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_H_BUSY)
53 #define IPMI_BT_CTLREG_GET_B_BUSY() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_B_BUSY)
55 #define IPMI_BT_CTLREG_SET(b) bt_write_ctrlreg(1 << (b))
56 #define IPMI_BT_CTLREG_SET_CLR_WR_PTR() IPMI_BT_CTLREG_SET( \
57 IPMI_BT_CTLREG_CLR_WR_PTR)
58 #define IPMI_BT_CTLREG_SET_CLR_RD_PTR() IPMI_BT_CTLREG_SET( \
59 IPMI_BT_CTLREG_CLR_RD_PTR)
60 #define IPMI_BT_CTLREG_SET_H2B_ATN() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_H2B_ATN)
61 #define IPMI_BT_CTLREG_SET_B2H_ATN() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_B2H_ATN)
62 #define IPMI_BT_CTLREG_SET_SMS_ATN() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_SMS_ATN)
63 #define IPMI_BT_CTLREG_SET_H_BUSY() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_H_BUSY)
65 static int bt_ints_enabled
;
67 static uint8_t bt_get_ctrlreg(void)
69 return inb(IPMI_BT_BASE
);
72 static void bt_write_ctrlreg(uint8_t val
)
74 outb(IPMI_BT_BASE
, val
);
77 static uint8_t bt_get_buf(void)
79 return inb(IPMI_BT_BASE
+ 1);
82 static void bt_write_buf(uint8_t val
)
84 outb(IPMI_BT_BASE
+ 1, val
);
87 static uint8_t bt_get_irqreg(void)
89 return inb(IPMI_BT_BASE
+ 2);
92 static void bt_write_irqreg(uint8_t val
)
94 outb(IPMI_BT_BASE
+ 2, val
);
97 static void bt_wait_b_busy(void)
99 unsigned int count
= 1000;
100 while (IPMI_BT_CTLREG_GET_B_BUSY() != 0) {
101 g_assert(--count
!= 0);
106 static void bt_wait_b2h_atn(void)
108 unsigned int count
= 1000;
109 while (IPMI_BT_CTLREG_GET_B2H_ATN() == 0) {
110 g_assert(--count
!= 0);
118 static in_port_t emu_port
;
119 static uint8_t inbuf
[100];
120 static unsigned int inbuf_len
;
121 static unsigned int inbuf_pos
;
122 static int last_was_aa
;
124 static void read_emu_data(void)
131 FD_SET(emu_fd
, &readfds
);
134 rv
= select(emu_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
139 rv
= read(emu_fd
, inbuf
, sizeof(inbuf
));
148 static void write_emu_msg(uint8_t *msg
, unsigned int len
)
156 for (i
= 0; i
< len
; i
++) {
157 printf(" %2.2x", msg
[i
]);
162 rv
= write(emu_fd
, msg
, len
);
166 static void get_emu_msg(uint8_t *msg
, unsigned int *len
)
168 unsigned int outpos
= 0;
171 while (inbuf_pos
< inbuf_len
) {
172 uint8_t ch
= inbuf
[inbuf_pos
++];
174 g_assert(outpos
< *len
);
177 msg
[outpos
++] = ch
& ~0x10;
179 } else if (ch
== 0xaa) {
183 if ((ch
== 0xa0) || (ch
== 0xa1)) {
184 /* Message complete */
197 for (i
= 0; i
< outpos
; i
++) {
198 printf(" %2.2x", msg
[i
]);
207 ipmb_checksum(const unsigned char *data
, int size
, unsigned char start
)
209 unsigned char csum
= start
;
211 for (; size
> 0; size
--, data
++) {
217 static uint8_t get_dev_id_cmd
[] = { 0x18, 0x01 };
218 static uint8_t get_dev_id_rsp
[] = { 0x1c, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00,
219 0x02, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 };
221 static uint8_t set_bmc_globals_cmd
[] = { 0x18, 0x2e, 0x0f };
222 static uint8_t set_bmc_globals_rsp
[] = { 0x1c, 0x2e, 0x00 };
223 static uint8_t enable_irq_cmd
[] = { 0x05, 0xa1 };
225 static void emu_msg_handler(void)
228 unsigned int msg_len
= sizeof(msg
);
230 get_emu_msg(msg
, &msg_len
);
231 g_assert(msg_len
>= 5);
232 g_assert(msg
[msg_len
- 1] == 0xa0);
234 g_assert(ipmb_checksum(msg
, msg_len
, 0) == 0);
236 if ((msg
[1] == get_dev_id_cmd
[0]) && (msg
[2] == get_dev_id_cmd
[1])) {
237 memcpy(msg
+ 1, get_dev_id_rsp
, sizeof(get_dev_id_rsp
));
238 msg_len
= sizeof(get_dev_id_rsp
) + 1;
239 msg
[msg_len
] = -ipmb_checksum(msg
, msg_len
, 0);
241 msg
[msg_len
++] = 0xa0;
242 write_emu_msg(msg
, msg_len
);
243 } else if ((msg
[1] == set_bmc_globals_cmd
[0]) &&
244 (msg
[2] == set_bmc_globals_cmd
[1])) {
245 write_emu_msg(enable_irq_cmd
, sizeof(enable_irq_cmd
));
246 memcpy(msg
+ 1, set_bmc_globals_rsp
, sizeof(set_bmc_globals_rsp
));
247 msg_len
= sizeof(set_bmc_globals_rsp
) + 1;
248 msg
[msg_len
] = -ipmb_checksum(msg
, msg_len
, 0);
250 msg
[msg_len
++] = 0xa0;
251 write_emu_msg(msg
, msg_len
);
257 static void bt_cmd(uint8_t *cmd
, unsigned int cmd_len
,
258 uint8_t *rsp
, unsigned int *rsp_len
)
260 unsigned int i
, len
, j
= 0;
264 g_assert(bt_get_ctrlreg() == 0);
267 IPMI_BT_CTLREG_SET_CLR_WR_PTR();
268 bt_write_buf(cmd_len
+ 1);
269 bt_write_buf(cmd
[0]);
271 for (i
= 1; i
< cmd_len
; i
++) {
272 bt_write_buf(cmd
[i
]);
274 IPMI_BT_CTLREG_SET_H2B_ATN();
276 emu_msg_handler(); /* We should get a message on the socket here. */
279 if (bt_ints_enabled
) {
280 g_assert((bt_get_irqreg() & 0x02) == 0x02);
281 g_assert(get_irq(IPMI_IRQ
));
282 bt_write_irqreg(0x03);
284 g_assert(!get_irq(IPMI_IRQ
));
286 IPMI_BT_CTLREG_SET_H_BUSY();
287 IPMI_BT_CTLREG_SET_B2H_ATN();
288 IPMI_BT_CTLREG_SET_CLR_RD_PTR();
291 rsp
[0] = bt_get_buf();
292 assert(bt_get_buf() == seq
);
294 for (j
= 1; j
< len
; j
++) {
295 rsp
[j
] = bt_get_buf();
297 IPMI_BT_CTLREG_SET_H_BUSY();
303 * We should get a connect request and a short message with capabilities.
305 static void test_connect(void)
313 static uint8_t exp1
[] = { 0xff, 0x01, 0xa1 }; /* A protocol version */
314 static uint8_t exp2
[] = { 0x08, 0x3f, 0xa1 }; /* A capabilities cmd */
317 FD_SET(emu_lfd
, &readfds
);
320 rv
= select(emu_lfd
+ 1, &readfds
, NULL
, NULL
, &tv
);
322 emu_fd
= accept(emu_lfd
, NULL
, 0);
326 g_assert(emu_fd
>= 0);
329 rv
= setsockopt(emu_fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
332 /* Report our version */
333 write_emu_msg(exp1
, sizeof(exp1
));
335 /* Validate that we get the info we expect. */
336 msglen
= sizeof(msg
);
337 get_emu_msg(msg
, &msglen
);
338 g_assert(msglen
== sizeof(exp1
));
339 g_assert(memcmp(msg
, exp1
, msglen
) == 0);
340 msglen
= sizeof(msg
);
341 get_emu_msg(msg
, &msglen
);
342 g_assert(msglen
== sizeof(exp2
));
343 g_assert(memcmp(msg
, exp2
, msglen
) == 0);
347 * Send a get_device_id to do a basic test.
349 static void test_bt_base(void)
352 unsigned int rsplen
= sizeof(rsp
);
354 bt_cmd(get_dev_id_cmd
, sizeof(get_dev_id_cmd
), rsp
, &rsplen
);
355 g_assert(rsplen
== sizeof(get_dev_id_rsp
));
356 g_assert(memcmp(get_dev_id_rsp
, rsp
, rsplen
) == 0);
360 * Enable IRQs for the interface.
362 static void test_enable_irq(void)
365 unsigned int rsplen
= sizeof(rsp
);
367 bt_cmd(set_bmc_globals_cmd
, sizeof(set_bmc_globals_cmd
), rsp
, &rsplen
);
368 g_assert(rsplen
== sizeof(set_bmc_globals_rsp
));
369 g_assert(memcmp(set_bmc_globals_rsp
, rsp
, rsplen
) == 0);
370 bt_write_irqreg(0x01);
375 * Create a local TCP socket with any port, then save off the port we got.
377 static void open_socket(void)
379 struct sockaddr_in myaddr
;
382 myaddr
.sin_family
= AF_INET
;
383 myaddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
385 emu_lfd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
390 if (bind(emu_lfd
, (struct sockaddr
*) &myaddr
, sizeof(myaddr
)) == -1) {
394 addrlen
= sizeof(myaddr
);
395 if (getsockname(emu_lfd
, (struct sockaddr
*) &myaddr
, &addrlen
) == -1) {
396 perror("getsockname");
399 emu_port
= ntohs(myaddr
.sin_port
);
400 assert(listen(emu_lfd
, 1) != -1);
403 int main(int argc
, char **argv
)
410 g_test_init(&argc
, &argv
, NULL
);
412 global_qtest
= qtest_initf(
413 " -chardev socket,id=ipmi0,host=localhost,port=%d,reconnect=10"
414 " -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0"
415 " -device isa-ipmi-bt,bmc=bmc0", emu_port
);
416 qtest_irq_intercept_in(global_qtest
, "ioapic");
417 qtest_add_func("/ipmi/extern/connect", test_connect
);
418 qtest_add_func("/ipmi/extern/bt_base", test_bt_base
);
419 qtest_add_func("/ipmi/extern/bt_enable_irq", test_enable_irq
);
420 qtest_add_func("/ipmi/extern/bt_base_irq", test_bt_base
);
422 qtest_quit(global_qtest
);