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