aboutsummaryrefslogtreecommitdiff
path: root/tests/qtest/ipmi-bt-test.c
blob: 19612e9405aed708a8d1b87907050a231e363fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * IPMI BT test cases, using the external interface for checking
 *
 * Copyright (c) 2012 Corey Minyard <cminyard@mvista.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "qemu/osdep.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>


#include "libqtest-single.h"
#include "qemu-common.h"

#define IPMI_IRQ        5

#define IPMI_BT_BASE    0xe4

#define IPMI_BT_CTLREG_CLR_WR_PTR  0
#define IPMI_BT_CTLREG_CLR_RD_PTR  1
#define IPMI_BT_CTLREG_H2B_ATN     2
#define IPMI_BT_CTLREG_B2H_ATN     3
#define IPMI_BT_CTLREG_SMS_ATN     4
#define IPMI_BT_CTLREG_H_BUSY      6
#define IPMI_BT_CTLREG_B_BUSY      7

#define IPMI_BT_CTLREG_GET(b) ((bt_get_ctrlreg() >> (b)) & 1)
#define IPMI_BT_CTLREG_GET_H2B_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_H2B_ATN)
#define IPMI_BT_CTLREG_GET_B2H_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_B2H_ATN)
#define IPMI_BT_CTLREG_GET_SMS_ATN() IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_SMS_ATN)
#define IPMI_BT_CTLREG_GET_H_BUSY()  IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_H_BUSY)
#define IPMI_BT_CTLREG_GET_B_BUSY()  IPMI_BT_CTLREG_GET(IPMI_BT_CTLREG_B_BUSY)

#define IPMI_BT_CTLREG_SET(b) bt_write_ctrlreg(1 << (b))
#define IPMI_BT_CTLREG_SET_CLR_WR_PTR() IPMI_BT_CTLREG_SET( \
                                                IPMI_BT_CTLREG_CLR_WR_PTR)
#define IPMI_BT_CTLREG_SET_CLR_RD_PTR() IPMI_BT_CTLREG_SET( \
                                                IPMI_BT_CTLREG_CLR_RD_PTR)
#define IPMI_BT_CTLREG_SET_H2B_ATN()  IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_H2B_ATN)
#define IPMI_BT_CTLREG_SET_B2H_ATN()  IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_B2H_ATN)
#define IPMI_BT_CTLREG_SET_SMS_ATN()  IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_SMS_ATN)
#define IPMI_BT_CTLREG_SET_H_BUSY()   IPMI_BT_CTLREG_SET(IPMI_BT_CTLREG_H_BUSY)

static int bt_ints_enabled;

static uint8_t bt_get_ctrlreg(void)
{
    return inb(IPMI_BT_BASE);
}

static void bt_write_ctrlreg(uint8_t val)
{
    outb(IPMI_BT_BASE, val);
}

static uint8_t bt_get_buf(void)
{
    return inb(IPMI_BT_BASE + 1);
}

static void bt_write_buf(uint8_t val)
{
    outb(IPMI_BT_BASE + 1, val);
}

static uint8_t bt_get_irqreg(void)
{
    return inb(IPMI_BT_BASE + 2);
}

static void bt_write_irqreg(uint8_t val)
{
    outb(IPMI_BT_BASE + 2, val);
}

static void bt_wait_b_busy(void)
{
    unsigned int count = 1000;
    while (IPMI_BT_CTLREG_GET_B_BUSY() != 0) {
        --count;
        g_assert(count != 0);
        usleep(100);
    }
}

static void bt_wait_b2h_atn(void)
{
    unsigned int count = 1000;
    while (IPMI_BT_CTLREG_GET_B2H_ATN() == 0) {
        --count;
        g_assert(count != 0);
        usleep(100);
    }
}


static int emu_lfd;
static int emu_fd;
static in_port_t emu_port;
static uint8_t inbuf[100];
static unsigned int inbuf_len;
static unsigned int inbuf_pos;
static int last_was_aa;

static void read_emu_data(void)
{
    fd_set readfds;
    int rv;
    struct timeval tv;

    FD_ZERO(&readfds);
    FD_SET(emu_fd, &readfds);
    tv.tv_sec = 10;
    tv.tv_usec = 0;
    rv = select(emu_fd + 1, &readfds, NULL, NULL, &tv);
    if (rv == -1) {
        perror("select");
    }
    g_assert(rv == 1);
    rv = read(emu_fd, inbuf, sizeof(inbuf));
    if (rv == -1) {
        perror("read");
    }
    g_assert(rv > 0);
    inbuf_len = rv;
    inbuf_pos = 0;
}

static void write_emu_msg(uint8_t *msg, unsigned int len)
{
    int rv;

#ifdef DEBUG_TEST
    {
        unsigned int i;
        printf("sending:");
        for (i = 0; i < len; i++) {
            printf(" %2.2x", msg[i]);
        }
        printf("\n");
    }
#endif
    rv = write(emu_fd, msg, len);
    g_assert(rv == len);
}

static void get_emu_msg(uint8_t *msg, unsigned int *len)
{
    unsigned int outpos = 0;

    for (;;) {
        while (inbuf_pos < inbuf_len) {
            uint8_t ch = inbuf[inbuf_pos++];

            g_assert(outpos < *len);
            if (last_was_aa) {
                assert(ch & 0x10);
                msg[outpos++] = ch & ~0x10;
                last_was_aa = 0;
            } else if (ch == 0xaa) {
                last_was_aa = 1;
            } else {
                msg[outpos++] = ch;
                if ((ch == 0xa0) || (ch == 0xa1)) {
                    /* Message complete */
                    *len = outpos;
                    goto done;
                }
            }
        }
        read_emu_data();
    }
 done:
#ifdef DEBUG_TEST
    {
        unsigned int i;
        printf("Msg:");
        for (i = 0; i < outpos; i++) {
            printf(" %2.2x", msg[i]);
        }
        printf("\n");
    }
#endif
    return;
}

static uint8_t
ipmb_checksum(const unsigned char *data, int size, unsigned char start)
{
        unsigned char csum = start;

        for (; size > 0; size--, data++) {
                csum += *data;
        }
        return csum;
}

static uint8_t get_dev_id_cmd[] = { 0x18, 0x01 };
static uint8_t get_dev_id_rsp[] = { 0x1c, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00,
                                    0x02, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 };

static uint8_t set_bmc_globals_cmd[] = { 0x18, 0x2e, 0x0f };
static uint8_t set_bmc_globals_rsp[] = { 0x1c, 0x2e, 0x00 };
static uint8_t enable_irq_cmd[] = { 0x05, 0xa1 };

static void emu_msg_handler(void)
{
    uint8_t msg[100];
    unsigned int msg_len = sizeof(msg);

    get_emu_msg(msg, &msg_len);
    g_assert(msg_len >= 5);
    g_assert(msg[msg_len - 1] == 0xa0);
    msg_len--;
    g_assert(ipmb_checksum(msg, msg_len, 0) == 0);
    msg_len--;
    if ((msg[1] == get_dev_id_cmd[0]) && (msg[2] == get_dev_id_cmd[1])) {
        memcpy(msg + 1, get_dev_id_rsp, sizeof(get_dev_id_rsp));
        msg_len = sizeof(get_dev_id_rsp) + 1;
        msg[msg_len] = -ipmb_checksum(msg, msg_len, 0);
        msg_len++;
        msg[msg_len++] = 0xa0;
        write_emu_msg(msg, msg_len);
    } else if ((msg[1] == set_bmc_globals_cmd[0]) &&
               (msg[2] == set_bmc_globals_cmd[1])) {
        write_emu_msg(enable_irq_cmd, sizeof(enable_irq_cmd));
        memcpy(msg + 1, set_bmc_globals_rsp, sizeof(set_bmc_globals_rsp));
        msg_len = sizeof(set_bmc_globals_rsp) + 1;
        msg[msg_len] = -ipmb_checksum(msg, msg_len, 0);
        msg_len++;
        msg[msg_len++] = 0xa0;
        write_emu_msg(msg, msg_len);
    } else {
        g_assert(0);
    }
}

static void bt_cmd(uint8_t *cmd, unsigned int cmd_len,
                    uint8_t *rsp, unsigned int *rsp_len)
{
    unsigned int i, len, j = 0;
    uint8_t seq = 5;

    /* Should be idle */
    g_assert(bt_get_ctrlreg() == 0);

    bt_wait_b_busy();
    IPMI_BT_CTLREG_SET_CLR_WR_PTR();
    bt_write_buf(cmd_len + 1);
    bt_write_buf(cmd[0]);
    bt_write_buf(seq);
    for (i = 1; i < cmd_len; i++) {
        bt_write_buf(cmd[i]);
    }
    IPMI_BT_CTLREG_SET_H2B_ATN();

    emu_msg_handler(); /* We should get a message on the socket here. */

    bt_wait_b2h_atn();
    if (bt_ints_enabled) {
        g_assert((bt_get_irqreg() & 0x02) == 0x02);
        g_assert(get_irq(IPMI_IRQ));
        bt_write_irqreg(0x03);
    } else {
        g_assert(!get_irq(IPMI_IRQ));
    }
    IPMI_BT_CTLREG_SET_H_BUSY();
    IPMI_BT_CTLREG_SET_B2H_ATN();
    IPMI_BT_CTLREG_SET_CLR_RD_PTR();
    len = bt_get_buf();
    g_assert(len >= 4);
    rsp[0] = bt_get_buf();
    assert(bt_get_buf() == seq);
    len--;
    for (j = 1; j < len; j++) {
        rsp[j] = bt_get_buf();
    }
    IPMI_BT_CTLREG_SET_H_BUSY();
    *rsp_len = j;
}


/*
 * We should get a connect request and a short message with capabilities.
 */
static void test_connect(void)
{
    fd_set readfds;
    int rv;
    int val;
    struct timeval tv;
    uint8_t msg[100];
    unsigned int msglen;
    static uint8_t exp1[] = { 0xff, 0x01, 0xa1 }; /* A protocol version */
    static uint8_t exp2[] = { 0x08, 0x3f, 0xa1 }; /* A capabilities cmd */

    FD_ZERO(&readfds);
    FD_SET(emu_lfd, &readfds);
    tv.tv_sec = 10;
    tv.tv_usec = 0;
    rv = select(emu_lfd + 1, &readfds, NULL, NULL, &tv);
    g_assert(rv == 1);
    emu_fd = accept(emu_lfd, NULL, 0);
    if (emu_fd < 0) {
        perror("accept");
    }
    g_assert(emu_fd >= 0);

    val = 1;
    rv = setsockopt(emu_fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
    g_assert(rv != -1);

    /* Report our version */
    write_emu_msg(exp1, sizeof(exp1));

    /* Validate that we get the info we expect. */
    msglen = sizeof(msg);
    get_emu_msg(msg, &msglen);
    g_assert(msglen == sizeof(exp1));
    g_assert(memcmp(msg, exp1, msglen) == 0);
    msglen = sizeof(msg);
    get_emu_msg(msg, &msglen);
    g_assert(msglen == sizeof(exp2));
    g_assert(memcmp(msg, exp2, msglen) == 0);
}

/*
 * Send a get_device_id to do a basic test.
 */
static void test_bt_base(void)
{
    uint8_t rsp[20];
    unsigned int rsplen = sizeof(rsp);

    bt_cmd(get_dev_id_cmd, sizeof(get_dev_id_cmd), rsp, &rsplen);
    g_assert(rsplen == sizeof(get_dev_id_rsp));
    g_assert(memcmp(get_dev_id_rsp, rsp, rsplen) == 0);
}

/*
 * Enable IRQs for the interface.
 */
static void test_enable_irq(void)
{
    uint8_t rsp[20];
    unsigned int rsplen = sizeof(rsp);

    bt_cmd(set_bmc_globals_cmd, sizeof(set_bmc_globals_cmd), rsp, &rsplen);
    g_assert(rsplen == sizeof(set_bmc_globals_rsp));
    g_assert(memcmp(set_bmc_globals_rsp, rsp, rsplen) == 0);
    bt_write_irqreg(0x01);
    bt_ints_enabled = 1;
}

/*
 * Create a local TCP socket with any port, then save off the port we got.
 */
static void open_socket(void)
{
    struct sockaddr_in myaddr = {};
    socklen_t addrlen;

    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    myaddr.sin_port = 0;
    emu_lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (emu_lfd == -1) {
        perror("socket");
        exit(1);
    }
    if (bind(emu_lfd, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
        perror("bind");
        exit(1);
    }
    addrlen = sizeof(myaddr);
    if (getsockname(emu_lfd, (struct sockaddr *) &myaddr , &addrlen) == -1) {
        perror("getsockname");
        exit(1);
    }
    emu_port = ntohs(myaddr.sin_port);
    assert(listen(emu_lfd, 1) != -1);
}

int main(int argc, char **argv)
{
    int ret;

    open_socket();

    /* Run the tests */
    g_test_init(&argc, &argv, NULL);

    global_qtest = qtest_initf(
        " -chardev socket,id=ipmi0,host=localhost,port=%d,reconnect=10"
        " -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0"
        " -device isa-ipmi-bt,bmc=bmc0", emu_port);
    qtest_irq_intercept_in(global_qtest, "ioapic");
    qtest_add_func("/ipmi/extern/connect", test_connect);
    qtest_add_func("/ipmi/extern/bt_base", test_bt_base);
    qtest_add_func("/ipmi/extern/bt_enable_irq", test_enable_irq);
    qtest_add_func("/ipmi/extern/bt_base_irq", test_bt_base);
    ret = g_test_run();
    qtest_quit(global_qtest);

    return ret;
}