Merge remote-tracking branch 'qemu-project/master'
[qemu/ar7.git] / tests / qtest / ipmi-bt-test.c
blob383239bcd48e9b98a569c5aa10b9846cfb339040
1 /*
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
22 * THE SOFTWARE.
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"
35 #define IPMI_IRQ 5
37 #define IPMI_BT_BASE 0xe4
39 #define IPMI_BT_CTLREG_CLR_WR_PTR 0
40 #define IPMI_BT_CTLREG_CLR_RD_PTR 1
41 #define IPMI_BT_CTLREG_H2B_ATN 2
42 #define IPMI_BT_CTLREG_B2H_ATN 3
43 #define IPMI_BT_CTLREG_SMS_ATN 4
44 #define IPMI_BT_CTLREG_H_BUSY 6
45 #define IPMI_BT_CTLREG_B_BUSY 7
47 #define IPMI_BT_CTLREG_GET(b) ((bt_get_ctrlreg() >> (b)) & 1)
48 #define IPMI_BT_CTLREG_GET_H2B_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_H2B_ATN)
49 #define IPMI_BT_CTLREG_GET_B2H_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_B2H_ATN)
50 #define IPMI_BT_CTLREG_GET_SMS_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_SMS_ATN)
51 #define IPMI_BT_CTLREG_GET_H_BUSY() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_H_BUSY)
52 #define IPMI_BT_CTLREG_GET_B_BUSY() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_B_BUSY)
54 #define IPMI_BT_CTLREG_SET(b) bt_write_ctrlreg(1 << (b))
55 #define IPMI_BT_CTLREG_SET_CLR_WR_PTR() IPMI_BT_CTLREG_SET( \
56 IPMI_BT_CTLREG_CLR_WR_PTR)
57 #define IPMI_BT_CTLREG_SET_CLR_RD_PTR() IPMI_BT_CTLREG_SET( \
58 IPMI_BT_CTLREG_CLR_RD_PTR)
59 #define IPMI_BT_CTLREG_SET_H2B_ATN() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_H2B_ATN)
60 #define IPMI_BT_CTLREG_SET_B2H_ATN() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_B2H_ATN)
61 #define IPMI_BT_CTLREG_SET_SMS_ATN() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_SMS_ATN)
62 #define IPMI_BT_CTLREG_SET_H_BUSY() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_H_BUSY)
64 static int bt_ints_enabled;
66 static uint8_t bt_get_ctrlreg(void)
68 return inb(IPMI_BT_BASE);
71 static void bt_write_ctrlreg(uint8_t val)
73 outb(IPMI_BT_BASE, val);
76 static uint8_t bt_get_buf(void)
78 return inb(IPMI_BT_BASE + 1);
81 static void bt_write_buf(uint8_t val)
83 outb(IPMI_BT_BASE + 1, val);
86 static uint8_t bt_get_irqreg(void)
88 return inb(IPMI_BT_BASE + 2);
91 static void bt_write_irqreg(uint8_t val)
93 outb(IPMI_BT_BASE + 2, val);
96 static void bt_wait_b_busy(void)
98 unsigned int count = 1000;
99 while (IPMI_BT_CTLREG_GET_B_BUSY() != 0) {
100 --count;
101 g_assert(count != 0);
102 usleep(100);
106 static void bt_wait_b2h_atn(void)
108 unsigned int count = 1000;
109 while (IPMI_BT_CTLREG_GET_B2H_ATN() == 0) {
110 --count;
111 g_assert(count != 0);
112 usleep(100);
117 static int emu_lfd;
118 static int emu_fd;
119 static in_port_t emu_port;
120 static uint8_t inbuf[100];
121 static unsigned int inbuf_len;
122 static unsigned int inbuf_pos;
123 static int last_was_aa;
125 static void read_emu_data(void)
127 fd_set readfds;
128 int rv;
129 struct timeval tv;
131 FD_ZERO(&readfds);
132 FD_SET(emu_fd, &readfds);
133 tv.tv_sec = 10;
134 tv.tv_usec = 0;
135 rv = select(emu_fd + 1, &readfds, NULL, NULL, &tv);
136 if (rv == -1) {
137 perror("select");
139 g_assert(rv == 1);
140 rv = read(emu_fd, inbuf, sizeof(inbuf));
141 if (rv == -1) {
142 perror("read");
144 g_assert(rv > 0);
145 inbuf_len = rv;
146 inbuf_pos = 0;
149 static void write_emu_msg(uint8_t *msg, unsigned int len)
151 int rv;
153 #ifdef DEBUG_TEST
155 unsigned int i;
156 printf("sending:");
157 for (i = 0; i < len; i++) {
158 printf(" %2.2x", msg[i]);
160 printf("\n");
162 #endif
163 rv = write(emu_fd, msg, len);
164 g_assert(rv == len);
167 static void get_emu_msg(uint8_t *msg, unsigned int *len)
169 unsigned int outpos = 0;
171 for (;;) {
172 while (inbuf_pos < inbuf_len) {
173 uint8_t ch = inbuf[inbuf_pos++];
175 g_assert(outpos < *len);
176 if (last_was_aa) {
177 assert(ch & 0x10);
178 msg[outpos++] = ch & ~0x10;
179 last_was_aa = 0;
180 } else if (ch == 0xaa) {
181 last_was_aa = 1;
182 } else {
183 msg[outpos++] = ch;
184 if ((ch == 0xa0) || (ch == 0xa1)) {
185 /* Message complete */
186 *len = outpos;
187 goto done;
191 read_emu_data();
193 done:
194 #ifdef DEBUG_TEST
196 unsigned int i;
197 printf("Msg:");
198 for (i = 0; i < outpos; i++) {
199 printf(" %2.2x", msg[i]);
201 printf("\n");
203 #endif
204 return;
207 static uint8_t
208 ipmb_checksum(const unsigned char *data, int size, unsigned char start)
210 unsigned char csum = start;
212 for (; size > 0; size--, data++) {
213 csum += *data;
215 return csum;
218 static uint8_t get_dev_id_cmd[] = { 0x18, 0x01 };
219 static uint8_t get_dev_id_rsp[] = { 0x1c, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00,
220 0x02, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 };
222 static uint8_t set_bmc_globals_cmd[] = { 0x18, 0x2e, 0x0f };
223 static uint8_t set_bmc_globals_rsp[] = { 0x1c, 0x2e, 0x00 };
224 static uint8_t enable_irq_cmd[] = { 0x05, 0xa1 };
226 static void emu_msg_handler(void)
228 uint8_t msg[100];
229 unsigned int msg_len = sizeof(msg);
231 get_emu_msg(msg, &msg_len);
232 g_assert(msg_len >= 5);
233 g_assert(msg[msg_len - 1] == 0xa0);
234 msg_len--;
235 g_assert(ipmb_checksum(msg, msg_len, 0) == 0);
236 msg_len--;
237 if ((msg[1] == get_dev_id_cmd[0]) && (msg[2] == get_dev_id_cmd[1])) {
238 memcpy(msg + 1, get_dev_id_rsp, sizeof(get_dev_id_rsp));
239 msg_len = sizeof(get_dev_id_rsp) + 1;
240 msg[msg_len] = -ipmb_checksum(msg, msg_len, 0);
241 msg_len++;
242 msg[msg_len++] = 0xa0;
243 write_emu_msg(msg, msg_len);
244 } else if ((msg[1] == set_bmc_globals_cmd[0]) &&
245 (msg[2] == set_bmc_globals_cmd[1])) {
246 write_emu_msg(enable_irq_cmd, sizeof(enable_irq_cmd));
247 memcpy(msg + 1, set_bmc_globals_rsp, sizeof(set_bmc_globals_rsp));
248 msg_len = sizeof(set_bmc_globals_rsp) + 1;
249 msg[msg_len] = -ipmb_checksum(msg, msg_len, 0);
250 msg_len++;
251 msg[msg_len++] = 0xa0;
252 write_emu_msg(msg, msg_len);
253 } else {
254 g_assert(0);
258 static void bt_cmd(uint8_t *cmd, unsigned int cmd_len,
259 uint8_t *rsp, unsigned int *rsp_len)
261 unsigned int i, len, j = 0;
262 uint8_t seq = 5;
264 /* Should be idle */
265 g_assert(bt_get_ctrlreg() == 0);
267 bt_wait_b_busy();
268 IPMI_BT_CTLREG_SET_CLR_WR_PTR();
269 bt_write_buf(cmd_len + 1);
270 bt_write_buf(cmd[0]);
271 bt_write_buf(seq);
272 for (i = 1; i < cmd_len; i++) {
273 bt_write_buf(cmd[i]);
275 IPMI_BT_CTLREG_SET_H2B_ATN();
277 emu_msg_handler(); /* We should get a message on the socket here. */
279 bt_wait_b2h_atn();
280 if (bt_ints_enabled) {
281 g_assert((bt_get_irqreg() & 0x02) == 0x02);
282 g_assert(get_irq(IPMI_IRQ));
283 bt_write_irqreg(0x03);
284 } else {
285 g_assert(!get_irq(IPMI_IRQ));
287 IPMI_BT_CTLREG_SET_H_BUSY();
288 IPMI_BT_CTLREG_SET_B2H_ATN();
289 IPMI_BT_CTLREG_SET_CLR_RD_PTR();
290 len = bt_get_buf();
291 g_assert(len >= 4);
292 rsp[0] = bt_get_buf();
293 assert(bt_get_buf() == seq);
294 len--;
295 for (j = 1; j < len; j++) {
296 rsp[j] = bt_get_buf();
298 IPMI_BT_CTLREG_SET_H_BUSY();
299 *rsp_len = j;
304 * We should get a connect request and a short message with capabilities.
306 static void test_connect(void)
308 fd_set readfds;
309 int rv;
310 int val;
311 struct timeval tv;
312 uint8_t msg[100];
313 unsigned int msglen;
314 static uint8_t exp1[] = { 0xff, 0x01, 0xa1 }; /* A protocol version */
315 static uint8_t exp2[] = { 0x08, 0x3f, 0xa1 }; /* A capabilities cmd */
317 FD_ZERO(&readfds);
318 FD_SET(emu_lfd, &readfds);
319 tv.tv_sec = 10;
320 tv.tv_usec = 0;
321 rv = select(emu_lfd + 1, &readfds, NULL, NULL, &tv);
322 g_assert(rv == 1);
323 emu_fd = accept(emu_lfd, NULL, 0);
324 if (emu_fd < 0) {
325 perror("accept");
327 g_assert(emu_fd >= 0);
329 val = 1;
330 rv = setsockopt(emu_fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
331 g_assert(rv != -1);
333 /* Report our version */
334 write_emu_msg(exp1, sizeof(exp1));
336 /* Validate that we get the info we expect. */
337 msglen = sizeof(msg);
338 get_emu_msg(msg, &msglen);
339 g_assert(msglen == sizeof(exp1));
340 g_assert(memcmp(msg, exp1, msglen) == 0);
341 msglen = sizeof(msg);
342 get_emu_msg(msg, &msglen);
343 g_assert(msglen == sizeof(exp2));
344 g_assert(memcmp(msg, exp2, msglen) == 0);
348 * Send a get_device_id to do a basic test.
350 static void test_bt_base(void)
352 uint8_t rsp[20];
353 unsigned int rsplen = sizeof(rsp);
355 bt_cmd(get_dev_id_cmd, sizeof(get_dev_id_cmd), rsp, &rsplen);
356 g_assert(rsplen == sizeof(get_dev_id_rsp));
357 g_assert(memcmp(get_dev_id_rsp, rsp, rsplen) == 0);
361 * Enable IRQs for the interface.
363 static void test_enable_irq(void)
365 uint8_t rsp[20];
366 unsigned int rsplen = sizeof(rsp);
368 bt_cmd(set_bmc_globals_cmd, sizeof(set_bmc_globals_cmd), rsp, &rsplen);
369 g_assert(rsplen == sizeof(set_bmc_globals_rsp));
370 g_assert(memcmp(set_bmc_globals_rsp, rsp, rsplen) == 0);
371 bt_write_irqreg(0x01);
372 bt_ints_enabled = 1;
376 * Create a local TCP socket with any port, then save off the port we got.
378 static void open_socket(void)
380 struct sockaddr_in myaddr = {};
381 socklen_t addrlen;
383 myaddr.sin_family = AF_INET;
384 myaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
385 myaddr.sin_port = 0;
386 emu_lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
387 if (emu_lfd == -1) {
388 perror("socket");
389 exit(1);
391 if (bind(emu_lfd, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
392 perror("bind");
393 exit(1);
395 addrlen = sizeof(myaddr);
396 if (getsockname(emu_lfd, (struct sockaddr *) &myaddr , &addrlen) == -1) {
397 perror("getsockname");
398 exit(1);
400 emu_port = ntohs(myaddr.sin_port);
401 assert(listen(emu_lfd, 1) != -1);
404 int main(int argc, char **argv)
406 int ret;
408 open_socket();
410 /* Run the tests */
411 g_test_init(&argc, &argv, NULL);
413 global_qtest = qtest_initf(
414 " -chardev socket,id=ipmi0,host=127.0.0.1,port=%d,reconnect=10"
415 " -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0"
416 " -device isa-ipmi-bt,bmc=bmc0", emu_port);
417 qtest_irq_intercept_in(global_qtest, "ioapic");
418 qtest_add_func("/ipmi/extern/connect", test_connect);
419 qtest_add_func("/ipmi/extern/bt_base", test_bt_base);
420 qtest_add_func("/ipmi/extern/bt_enable_irq", test_enable_irq);
421 qtest_add_func("/ipmi/extern/bt_base_irq", test_bt_base);
422 ret = g_test_run();
423 qtest_quit(global_qtest);
425 return ret;