]>
Commit | Line | Data |
---|---|---|
24f976d3 CM |
1 | /* |
2 | * IPMI BT test cases, using the external interface for checking | |
3 | * | |
4 | * Copyright (c) 2012 Corey Minyard <[email protected]> | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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. | |
23 | */ | |
24 | ||
681c28a3 | 25 | #include "qemu/osdep.h" |
24f976d3 CM |
26 | |
27 | #include <sys/socket.h> | |
28 | #include <netinet/in.h> | |
29 | #include <netinet/ip.h> | |
30 | #include <netinet/tcp.h> | |
31 | ||
32 | #include <glib.h> | |
33 | ||
34 | #include "libqtest.h" | |
35 | #include "qemu-common.h" | |
36 | ||
37 | #define IPMI_IRQ 5 | |
38 | ||
39 | #define IPMI_BT_BASE 0xe4 | |
40 | ||
41 | #define IPMI_BT_CTLREG_CLR_WR_PTR 0 | |
42 | #define IPMI_BT_CTLREG_CLR_RD_PTR 1 | |
43 | #define IPMI_BT_CTLREG_H2B_ATN 2 | |
44 | #define IPMI_BT_CTLREG_B2H_ATN 3 | |
45 | #define IPMI_BT_CTLREG_SMS_ATN 4 | |
46 | #define IPMI_BT_CTLREG_H_BUSY 6 | |
47 | #define IPMI_BT_CTLREG_B_BUSY 7 | |
48 | ||
49 | #define IPMI_BT_CTLREG_GET(b) ((bt_get_ctrlreg() >> (b)) & 1) | |
50 | #define IPMI_BT_CTLREG_GET_H2B_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_H2B_ATN) | |
51 | #define IPMI_BT_CTLREG_GET_B2H_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_B2H_ATN) | |
52 | #define IPMI_BT_CTLREG_GET_SMS_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_SMS_ATN) | |
53 | #define IPMI_BT_CTLREG_GET_H_BUSY() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_H_BUSY) | |
54 | #define IPMI_BT_CTLREG_GET_B_BUSY() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_B_BUSY) | |
55 | ||
56 | #define IPMI_BT_CTLREG_SET(b) bt_write_ctrlreg(1 << (b)) | |
57 | #define IPMI_BT_CTLREG_SET_CLR_WR_PTR() IPMI_BT_CTLREG_SET( \ | |
58 | IPMI_BT_CTLREG_CLR_WR_PTR) | |
59 | #define IPMI_BT_CTLREG_SET_CLR_RD_PTR() IPMI_BT_CTLREG_SET( \ | |
60 | IPMI_BT_CTLREG_CLR_RD_PTR) | |
61 | #define IPMI_BT_CTLREG_SET_H2B_ATN() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_H2B_ATN) | |
62 | #define IPMI_BT_CTLREG_SET_B2H_ATN() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_B2H_ATN) | |
63 | #define IPMI_BT_CTLREG_SET_SMS_ATN() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_SMS_ATN) | |
64 | #define IPMI_BT_CTLREG_SET_H_BUSY() IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_H_BUSY) | |
65 | ||
66 | static int bt_ints_enabled; | |
67 | ||
68 | static uint8_t bt_get_ctrlreg(void) | |
69 | { | |
70 | return inb(IPMI_BT_BASE); | |
71 | } | |
72 | ||
73 | static void bt_write_ctrlreg(uint8_t val) | |
74 | { | |
75 | outb(IPMI_BT_BASE, val); | |
76 | } | |
77 | ||
78 | static uint8_t bt_get_buf(void) | |
79 | { | |
80 | return inb(IPMI_BT_BASE + 1); | |
81 | } | |
82 | ||
83 | static void bt_write_buf(uint8_t val) | |
84 | { | |
85 | outb(IPMI_BT_BASE + 1, val); | |
86 | } | |
87 | ||
88 | static uint8_t bt_get_irqreg(void) | |
89 | { | |
90 | return inb(IPMI_BT_BASE + 2); | |
91 | } | |
92 | ||
93 | static void bt_write_irqreg(uint8_t val) | |
94 | { | |
95 | outb(IPMI_BT_BASE + 2, val); | |
96 | } | |
97 | ||
98 | static void bt_wait_b_busy(void) | |
99 | { | |
100 | unsigned int count = 1000; | |
101 | while (IPMI_BT_CTLREG_GET_B_BUSY() != 0) { | |
102 | g_assert(--count != 0); | |
103 | } | |
104 | } | |
105 | ||
106 | static void bt_wait_b2h_atn(void) | |
107 | { | |
108 | unsigned int count = 1000; | |
109 | while (IPMI_BT_CTLREG_GET_B2H_ATN() == 0) { | |
110 | g_assert(--count != 0); | |
111 | } | |
112 | } | |
113 | ||
114 | ||
115 | static int emu_lfd; | |
116 | static int emu_fd; | |
117 | static in_port_t emu_port; | |
118 | static uint8_t inbuf[100]; | |
119 | static unsigned int inbuf_len; | |
120 | static unsigned int inbuf_pos; | |
121 | static int last_was_aa; | |
122 | ||
123 | static void read_emu_data(void) | |
124 | { | |
125 | fd_set readfds; | |
126 | int rv; | |
127 | struct timeval tv; | |
128 | ||
129 | FD_ZERO(&readfds); | |
130 | FD_SET(emu_fd, &readfds); | |
131 | tv.tv_sec = 10; | |
132 | tv.tv_usec = 0; | |
133 | rv = select(emu_fd + 1, &readfds, NULL, NULL, &tv); | |
134 | if (rv == -1) { | |
135 | perror("select"); | |
136 | } | |
137 | g_assert(rv == 1); | |
138 | rv = read(emu_fd, inbuf, sizeof(inbuf)); | |
139 | if (rv == -1) { | |
140 | perror("read"); | |
141 | } | |
142 | g_assert(rv > 0); | |
143 | inbuf_len = rv; | |
144 | inbuf_pos = 0; | |
145 | } | |
146 | ||
147 | static void write_emu_msg(uint8_t *msg, unsigned int len) | |
148 | { | |
149 | int rv; | |
150 | ||
151 | #ifdef DEBUG_TEST | |
152 | { | |
153 | unsigned int i; | |
154 | printf("sending:"); | |
155 | for (i = 0; i < len; i++) { | |
156 | printf(" %2.2x", msg[i]); | |
157 | } | |
158 | printf("\n"); | |
159 | } | |
160 | #endif | |
161 | rv = write(emu_fd, msg, len); | |
162 | g_assert(rv == len); | |
163 | } | |
164 | ||
165 | static void get_emu_msg(uint8_t *msg, unsigned int *len) | |
166 | { | |
167 | unsigned int outpos = 0; | |
168 | ||
169 | for (;;) { | |
170 | while (inbuf_pos < inbuf_len) { | |
171 | uint8_t ch = inbuf[inbuf_pos++]; | |
172 | ||
173 | g_assert(outpos < *len); | |
174 | if (last_was_aa) { | |
175 | assert(ch & 0x10); | |
176 | msg[outpos++] = ch & ~0x10; | |
177 | last_was_aa = 0; | |
178 | } else if (ch == 0xaa) { | |
179 | last_was_aa = 1; | |
180 | } else { | |
181 | msg[outpos++] = ch; | |
182 | if ((ch == 0xa0) || (ch == 0xa1)) { | |
183 | /* Message complete */ | |
184 | *len = outpos; | |
185 | goto done; | |
186 | } | |
187 | } | |
188 | } | |
189 | read_emu_data(); | |
190 | } | |
191 | done: | |
192 | #ifdef DEBUG_TEST | |
193 | { | |
194 | unsigned int i; | |
195 | printf("Msg:"); | |
196 | for (i = 0; i < outpos; i++) { | |
197 | printf(" %2.2x", msg[i]); | |
198 | } | |
199 | printf("\n"); | |
200 | } | |
201 | #endif | |
202 | return; | |
203 | } | |
204 | ||
205 | static uint8_t | |
206 | ipmb_checksum(const unsigned char *data, int size, unsigned char start) | |
207 | { | |
208 | unsigned char csum = start; | |
209 | ||
210 | for (; size > 0; size--, data++) { | |
211 | csum += *data; | |
212 | } | |
213 | return csum; | |
214 | } | |
215 | ||
216 | static uint8_t get_dev_id_cmd[] = { 0x18, 0x01 }; | |
217 | static uint8_t get_dev_id_rsp[] = { 0x1c, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, | |
218 | 0x02, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
219 | ||
220 | static uint8_t set_bmc_globals_cmd[] = { 0x18, 0x2e, 0x0f }; | |
221 | static uint8_t set_bmc_globals_rsp[] = { 0x1c, 0x2e, 0x00 }; | |
222 | static uint8_t enable_irq_cmd[] = { 0x05, 0xa1 }; | |
223 | ||
224 | static void emu_msg_handler(void) | |
225 | { | |
226 | uint8_t msg[100]; | |
227 | unsigned int msg_len = sizeof(msg); | |
228 | ||
229 | get_emu_msg(msg, &msg_len); | |
230 | g_assert(msg_len >= 5); | |
231 | g_assert(msg[msg_len - 1] == 0xa0); | |
232 | msg_len--; | |
233 | g_assert(ipmb_checksum(msg, msg_len, 0) == 0); | |
234 | msg_len--; | |
235 | if ((msg[1] == get_dev_id_cmd[0]) && (msg[2] == get_dev_id_cmd[1])) { | |
236 | memcpy(msg + 1, get_dev_id_rsp, sizeof(get_dev_id_rsp)); | |
237 | msg_len = sizeof(get_dev_id_rsp) + 1; | |
238 | msg[msg_len] = -ipmb_checksum(msg, msg_len, 0); | |
239 | msg_len++; | |
240 | msg[msg_len++] = 0xa0; | |
241 | write_emu_msg(msg, msg_len); | |
242 | } else if ((msg[1] == set_bmc_globals_cmd[0]) && | |
243 | (msg[2] == set_bmc_globals_cmd[1])) { | |
244 | memcpy(msg + 1, set_bmc_globals_rsp, sizeof(set_bmc_globals_rsp)); | |
245 | msg_len = sizeof(set_bmc_globals_rsp) + 1; | |
246 | msg[msg_len] = -ipmb_checksum(msg, msg_len, 0); | |
247 | msg_len++; | |
248 | msg[msg_len++] = 0xa0; | |
249 | write_emu_msg(msg, msg_len); | |
250 | write_emu_msg(enable_irq_cmd, sizeof(enable_irq_cmd)); | |
251 | } else { | |
252 | g_assert(0); | |
253 | } | |
254 | } | |
255 | ||
256 | static void bt_cmd(uint8_t *cmd, unsigned int cmd_len, | |
257 | uint8_t *rsp, unsigned int *rsp_len) | |
258 | { | |
259 | unsigned int i, len, j = 0; | |
260 | uint8_t seq = 5; | |
261 | ||
262 | /* Should be idle */ | |
263 | g_assert(bt_get_ctrlreg() == 0); | |
264 | ||
265 | bt_wait_b_busy(); | |
266 | IPMI_BT_CTLREG_SET_CLR_WR_PTR(); | |
267 | bt_write_buf(cmd_len + 1); | |
268 | bt_write_buf(cmd[0]); | |
269 | bt_write_buf(seq); | |
270 | for (i = 1; i < cmd_len; i++) { | |
271 | bt_write_buf(cmd[i]); | |
272 | } | |
273 | IPMI_BT_CTLREG_SET_H2B_ATN(); | |
274 | ||
275 | emu_msg_handler(); /* We should get a message on the socket here. */ | |
276 | ||
277 | bt_wait_b2h_atn(); | |
278 | if (bt_ints_enabled) { | |
279 | g_assert((bt_get_irqreg() & 0x02) == 0x02); | |
280 | g_assert(get_irq(IPMI_IRQ)); | |
281 | bt_write_irqreg(0x03); | |
282 | } else { | |
283 | g_assert(!get_irq(IPMI_IRQ)); | |
284 | } | |
285 | IPMI_BT_CTLREG_SET_H_BUSY(); | |
286 | IPMI_BT_CTLREG_SET_B2H_ATN(); | |
287 | IPMI_BT_CTLREG_SET_CLR_RD_PTR(); | |
288 | len = bt_get_buf(); | |
289 | g_assert(len >= 4); | |
290 | rsp[0] = bt_get_buf(); | |
291 | assert(bt_get_buf() == seq); | |
292 | len--; | |
293 | for (j = 1; j < len; j++) { | |
294 | rsp[j] = bt_get_buf(); | |
295 | } | |
296 | IPMI_BT_CTLREG_SET_H_BUSY(); | |
297 | *rsp_len = j; | |
298 | } | |
299 | ||
300 | ||
301 | /* | |
302 | * We should get a connect request and a short message with capabilities. | |
303 | */ | |
304 | static void test_connect(void) | |
305 | { | |
306 | fd_set readfds; | |
307 | int rv; | |
308 | int val; | |
309 | struct timeval tv; | |
310 | uint8_t msg[100]; | |
311 | unsigned int msglen; | |
312 | static uint8_t exp1[] = { 0xff, 0x01, 0xa1 }; /* A protocol version */ | |
313 | static uint8_t exp2[] = { 0x08, 0x1f, 0xa1 }; /* A capabilities cmd */ | |
314 | ||
315 | FD_ZERO(&readfds); | |
316 | FD_SET(emu_lfd, &readfds); | |
317 | tv.tv_sec = 10; | |
318 | tv.tv_usec = 0; | |
319 | rv = select(emu_lfd + 1, &readfds, NULL, NULL, &tv); | |
320 | g_assert(rv == 1); | |
321 | emu_fd = accept(emu_lfd, NULL, 0); | |
322 | if (emu_fd < 0) { | |
323 | perror("accept"); | |
324 | } | |
325 | g_assert(emu_fd >= 0); | |
326 | ||
327 | val = 1; | |
328 | rv = setsockopt(emu_fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); | |
329 | g_assert(rv != -1); | |
330 | ||
331 | /* Report our version */ | |
332 | write_emu_msg(exp1, sizeof(exp1)); | |
333 | ||
334 | /* Validate that we get the info we expect. */ | |
335 | msglen = sizeof(msg); | |
336 | get_emu_msg(msg, &msglen); | |
337 | g_assert(msglen == sizeof(exp1)); | |
338 | g_assert(memcmp(msg, exp1, msglen) == 0); | |
339 | msglen = sizeof(msg); | |
340 | get_emu_msg(msg, &msglen); | |
341 | g_assert(msglen == sizeof(exp2)); | |
342 | g_assert(memcmp(msg, exp2, msglen) == 0); | |
343 | } | |
344 | ||
345 | /* | |
346 | * Send a get_device_id to do a basic test. | |
347 | */ | |
348 | static void test_bt_base(void) | |
349 | { | |
350 | uint8_t rsp[20]; | |
351 | unsigned int rsplen = sizeof(rsp); | |
352 | ||
353 | bt_cmd(get_dev_id_cmd, sizeof(get_dev_id_cmd), rsp, &rsplen); | |
354 | g_assert(rsplen == sizeof(get_dev_id_rsp)); | |
355 | g_assert(memcmp(get_dev_id_rsp, rsp, rsplen) == 0); | |
356 | } | |
357 | ||
358 | /* | |
359 | * Enable IRQs for the interface. | |
360 | */ | |
361 | static void test_enable_irq(void) | |
362 | { | |
363 | uint8_t rsp[20]; | |
364 | unsigned int rsplen = sizeof(rsp); | |
365 | ||
366 | bt_cmd(set_bmc_globals_cmd, sizeof(set_bmc_globals_cmd), rsp, &rsplen); | |
367 | g_assert(rsplen == sizeof(set_bmc_globals_rsp)); | |
368 | g_assert(memcmp(set_bmc_globals_rsp, rsp, rsplen) == 0); | |
369 | bt_write_irqreg(0x01); | |
370 | bt_ints_enabled = 1; | |
371 | } | |
372 | ||
373 | /* | |
374 | * Create a local TCP socket with any port, then save off the port we got. | |
375 | */ | |
376 | static void open_socket(void) | |
377 | { | |
378 | struct sockaddr_in myaddr; | |
379 | socklen_t addrlen; | |
380 | ||
381 | myaddr.sin_family = AF_INET; | |
382 | myaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
383 | myaddr.sin_port = 0; | |
384 | emu_lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
385 | if (emu_lfd == -1) { | |
386 | perror("socket"); | |
387 | exit(1); | |
388 | } | |
389 | if (bind(emu_lfd, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) { | |
390 | perror("bind"); | |
391 | exit(1); | |
392 | } | |
393 | addrlen = sizeof(myaddr); | |
394 | if (getsockname(emu_lfd, (struct sockaddr *) &myaddr , &addrlen) == -1) { | |
395 | perror("getsockname"); | |
396 | exit(1); | |
397 | } | |
398 | emu_port = ntohs(myaddr.sin_port); | |
399 | assert(listen(emu_lfd, 1) != -1); | |
400 | } | |
401 | ||
402 | int main(int argc, char **argv) | |
403 | { | |
404 | const char *arch = qtest_get_arch(); | |
405 | char *cmdline; | |
406 | int ret; | |
407 | ||
408 | /* Check architecture */ | |
409 | if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) { | |
410 | g_test_message("Skipping test for non-x86\n"); | |
411 | return 0; | |
412 | } | |
413 | ||
414 | open_socket(); | |
415 | ||
416 | /* Run the tests */ | |
417 | g_test_init(&argc, &argv, NULL); | |
418 | ||
419 | cmdline = g_strdup_printf("-vnc none" | |
420 | " -chardev socket,id=ipmi0,host=localhost,port=%d,reconnect=10" | |
421 | " -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0" | |
422 | " -device isa-ipmi-bt,bmc=bmc0", emu_port); | |
423 | qtest_start(cmdline); | |
424 | qtest_irq_intercept_in(global_qtest, "ioapic"); | |
425 | qtest_add_func("/ipmi/extern/connect", test_connect); | |
426 | qtest_add_func("/ipmi/extern/bt_base", test_bt_base); | |
427 | qtest_add_func("/ipmi/extern/bt_enable_irq", test_enable_irq); | |
428 | qtest_add_func("/ipmi/extern/bt_base_irq", test_bt_base); | |
429 | ret = g_test_run(); | |
430 | qtest_quit(global_qtest); | |
431 | ||
432 | return ret; | |
433 | } |