| 1 | /* |
| 2 | * IPMI KCS test cases, using the local interface. |
| 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 "libqtest-single.h" |
| 28 | |
| 29 | #define IPMI_IRQ 5 |
| 30 | |
| 31 | #define IPMI_KCS_BASE 0xca2 |
| 32 | |
| 33 | #define IPMI_KCS_STATUS_ABORT 0x60 |
| 34 | #define IPMI_KCS_CMD_WRITE_START 0x61 |
| 35 | #define IPMI_KCS_CMD_WRITE_END 0x62 |
| 36 | #define IPMI_KCS_CMD_READ 0x68 |
| 37 | |
| 38 | #define IPMI_KCS_ABORTED_BY_CMD 0x01 |
| 39 | |
| 40 | #define IPMI_KCS_CMDREG_GET_STATE() ((kcs_get_cmdreg() >> 6) & 3) |
| 41 | #define IPMI_KCS_STATE_IDLE 0 |
| 42 | #define IPMI_KCS_STATE_READ 1 |
| 43 | #define IPMI_KCS_STATE_WRITE 2 |
| 44 | #define IPMI_KCS_STATE_ERROR 3 |
| 45 | #define IPMI_KCS_CMDREG_GET_CD() ((kcs_get_cmdreg() >> 3) & 1) |
| 46 | #define IPMI_KCS_CMDREG_GET_ATN() ((kcs_get_cmdreg() >> 2) & 1) |
| 47 | #define IPMI_KCS_CMDREG_GET_IBF() ((kcs_get_cmdreg() >> 1) & 1) |
| 48 | #define IPMI_KCS_CMDREG_GET_OBF() ((kcs_get_cmdreg() >> 0) & 1) |
| 49 | |
| 50 | static int kcs_ints_enabled; |
| 51 | |
| 52 | static uint8_t kcs_get_cmdreg(void) |
| 53 | { |
| 54 | return inb(IPMI_KCS_BASE + 1); |
| 55 | } |
| 56 | |
| 57 | static void kcs_write_cmdreg(uint8_t val) |
| 58 | { |
| 59 | outb(IPMI_KCS_BASE + 1, val); |
| 60 | } |
| 61 | |
| 62 | static uint8_t kcs_get_datareg(void) |
| 63 | { |
| 64 | return inb(IPMI_KCS_BASE); |
| 65 | } |
| 66 | |
| 67 | static void kcs_write_datareg(uint8_t val) |
| 68 | { |
| 69 | outb(IPMI_KCS_BASE, val); |
| 70 | } |
| 71 | |
| 72 | static void kcs_wait_ibf(void) |
| 73 | { |
| 74 | unsigned int count = 1000; |
| 75 | while (IPMI_KCS_CMDREG_GET_IBF() != 0) { |
| 76 | --count; |
| 77 | g_assert(count != 0); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void kcs_wait_obf(void) |
| 82 | { |
| 83 | unsigned int count = 1000; |
| 84 | while (IPMI_KCS_CMDREG_GET_OBF() == 0) { |
| 85 | --count; |
| 86 | g_assert(count != 0); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void kcs_clear_obf(void) |
| 91 | { |
| 92 | if (kcs_ints_enabled) { |
| 93 | g_assert(get_irq(IPMI_IRQ)); |
| 94 | } else { |
| 95 | g_assert(!get_irq(IPMI_IRQ)); |
| 96 | } |
| 97 | g_assert(IPMI_KCS_CMDREG_GET_OBF() == 1); |
| 98 | kcs_get_datareg(); |
| 99 | g_assert(IPMI_KCS_CMDREG_GET_OBF() == 0); |
| 100 | g_assert(!get_irq(IPMI_IRQ)); |
| 101 | } |
| 102 | |
| 103 | static void kcs_check_state(uint8_t state) |
| 104 | { |
| 105 | g_assert(IPMI_KCS_CMDREG_GET_STATE() == state); |
| 106 | } |
| 107 | |
| 108 | static void kcs_cmd(uint8_t *cmd, unsigned int cmd_len, |
| 109 | uint8_t *rsp, unsigned int *rsp_len) |
| 110 | { |
| 111 | unsigned int i, j = 0; |
| 112 | |
| 113 | /* Should be idle */ |
| 114 | g_assert(kcs_get_cmdreg() == 0); |
| 115 | |
| 116 | kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_START); |
| 117 | kcs_wait_ibf(); |
| 118 | kcs_check_state(IPMI_KCS_STATE_WRITE); |
| 119 | kcs_clear_obf(); |
| 120 | for (i = 0; i < cmd_len; i++) { |
| 121 | kcs_write_datareg(cmd[i]); |
| 122 | kcs_wait_ibf(); |
| 123 | kcs_check_state(IPMI_KCS_STATE_WRITE); |
| 124 | kcs_clear_obf(); |
| 125 | } |
| 126 | kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_END); |
| 127 | kcs_wait_ibf(); |
| 128 | kcs_check_state(IPMI_KCS_STATE_WRITE); |
| 129 | kcs_clear_obf(); |
| 130 | kcs_write_datareg(0); |
| 131 | next_read_byte: |
| 132 | kcs_wait_ibf(); |
| 133 | switch (IPMI_KCS_CMDREG_GET_STATE()) { |
| 134 | case IPMI_KCS_STATE_READ: |
| 135 | kcs_wait_obf(); |
| 136 | g_assert(j < *rsp_len); |
| 137 | rsp[j++] = kcs_get_datareg(); |
| 138 | kcs_write_datareg(IPMI_KCS_CMD_READ); |
| 139 | goto next_read_byte; |
| 140 | break; |
| 141 | |
| 142 | case IPMI_KCS_STATE_IDLE: |
| 143 | kcs_wait_obf(); |
| 144 | kcs_get_datareg(); |
| 145 | break; |
| 146 | |
| 147 | default: |
| 148 | g_assert_not_reached(); |
| 149 | } |
| 150 | *rsp_len = j; |
| 151 | } |
| 152 | |
| 153 | static void kcs_abort(uint8_t *cmd, unsigned int cmd_len, |
| 154 | uint8_t *rsp, unsigned int *rsp_len) |
| 155 | { |
| 156 | unsigned int i, j = 0; |
| 157 | unsigned int retries = 4; |
| 158 | |
| 159 | /* Should be idle */ |
| 160 | g_assert(kcs_get_cmdreg() == 0); |
| 161 | |
| 162 | kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_START); |
| 163 | kcs_wait_ibf(); |
| 164 | kcs_check_state(IPMI_KCS_STATE_WRITE); |
| 165 | kcs_clear_obf(); |
| 166 | for (i = 0; i < cmd_len; i++) { |
| 167 | kcs_write_datareg(cmd[i]); |
| 168 | kcs_wait_ibf(); |
| 169 | kcs_check_state(IPMI_KCS_STATE_WRITE); |
| 170 | kcs_clear_obf(); |
| 171 | } |
| 172 | kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_END); |
| 173 | kcs_wait_ibf(); |
| 174 | kcs_check_state(IPMI_KCS_STATE_WRITE); |
| 175 | kcs_clear_obf(); |
| 176 | kcs_write_datareg(0); |
| 177 | kcs_wait_ibf(); |
| 178 | switch (IPMI_KCS_CMDREG_GET_STATE()) { |
| 179 | case IPMI_KCS_STATE_READ: |
| 180 | kcs_wait_obf(); |
| 181 | g_assert(j < *rsp_len); |
| 182 | rsp[j++] = kcs_get_datareg(); |
| 183 | kcs_write_datareg(IPMI_KCS_CMD_READ); |
| 184 | break; |
| 185 | |
| 186 | default: |
| 187 | g_assert_not_reached(); |
| 188 | } |
| 189 | |
| 190 | /* Start the abort here */ |
| 191 | retry_abort: |
| 192 | g_assert(retries > 0); |
| 193 | |
| 194 | kcs_wait_ibf(); |
| 195 | kcs_write_cmdreg(IPMI_KCS_STATUS_ABORT); |
| 196 | kcs_wait_ibf(); |
| 197 | kcs_clear_obf(); |
| 198 | kcs_write_datareg(0); |
| 199 | kcs_wait_ibf(); |
| 200 | if (IPMI_KCS_CMDREG_GET_STATE() != IPMI_KCS_STATE_READ) { |
| 201 | retries--; |
| 202 | goto retry_abort; |
| 203 | } |
| 204 | kcs_wait_obf(); |
| 205 | rsp[0] = kcs_get_datareg(); |
| 206 | kcs_write_datareg(IPMI_KCS_CMD_READ); |
| 207 | kcs_wait_ibf(); |
| 208 | if (IPMI_KCS_CMDREG_GET_STATE() != IPMI_KCS_STATE_IDLE) { |
| 209 | retries--; |
| 210 | goto retry_abort; |
| 211 | } |
| 212 | kcs_wait_obf(); |
| 213 | kcs_clear_obf(); |
| 214 | |
| 215 | *rsp_len = j; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | static uint8_t get_dev_id_cmd[] = { 0x18, 0x01 }; |
| 220 | static uint8_t get_dev_id_rsp[] = { 0x1c, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, |
| 221 | 0x02, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 222 | |
| 223 | /* |
| 224 | * Send a get_device_id to do a basic test. |
| 225 | */ |
| 226 | static void test_kcs_base(void) |
| 227 | { |
| 228 | uint8_t rsp[20]; |
| 229 | unsigned int rsplen = sizeof(rsp); |
| 230 | |
| 231 | kcs_cmd(get_dev_id_cmd, sizeof(get_dev_id_cmd), rsp, &rsplen); |
| 232 | g_assert(rsplen == sizeof(get_dev_id_rsp)); |
| 233 | g_assert(memcmp(get_dev_id_rsp, rsp, rsplen) == 0); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Abort a kcs operation while reading |
| 238 | */ |
| 239 | static void test_kcs_abort(void) |
| 240 | { |
| 241 | uint8_t rsp[20]; |
| 242 | unsigned int rsplen = sizeof(rsp); |
| 243 | |
| 244 | kcs_abort(get_dev_id_cmd, sizeof(get_dev_id_cmd), rsp, &rsplen); |
| 245 | g_assert(rsp[0] == IPMI_KCS_ABORTED_BY_CMD); |
| 246 | } |
| 247 | |
| 248 | static uint8_t set_bmc_globals_cmd[] = { 0x18, 0x2e, 0x0f }; |
| 249 | static uint8_t set_bmc_globals_rsp[] = { 0x1c, 0x2e, 0x00 }; |
| 250 | |
| 251 | /* |
| 252 | * Enable interrupts |
| 253 | */ |
| 254 | static void test_enable_irq(void) |
| 255 | { |
| 256 | uint8_t rsp[20]; |
| 257 | unsigned int rsplen = sizeof(rsp); |
| 258 | |
| 259 | kcs_cmd(set_bmc_globals_cmd, sizeof(set_bmc_globals_cmd), rsp, &rsplen); |
| 260 | g_assert(rsplen == sizeof(set_bmc_globals_rsp)); |
| 261 | g_assert(memcmp(set_bmc_globals_rsp, rsp, rsplen) == 0); |
| 262 | kcs_ints_enabled = 1; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | static uint8_t get_channel_access_cmd[] = { 0x18, 0x41, 0x01, 0x40 }; |
| 267 | static uint8_t get_channel_access_rsp[] = { 0x1c, 0x41, 0x00, 0x3a, 0x04 }; |
| 268 | |
| 269 | /* |
| 270 | * Get channel access |
| 271 | */ |
| 272 | static void test_kcs_channel_access(void) |
| 273 | { |
| 274 | uint8_t rsp[20]; |
| 275 | unsigned int rsplen = sizeof(rsp); |
| 276 | |
| 277 | kcs_cmd(get_channel_access_cmd, sizeof(get_channel_access_cmd), |
| 278 | rsp, &rsplen); |
| 279 | g_assert(rsplen == sizeof(get_channel_access_rsp)); |
| 280 | g_assert(memcmp(get_channel_access_rsp, rsp, rsplen) == 0); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | static uint8_t get_channel_info_cmd[] = { 0x18, 0x42, 0x01 }; |
| 285 | static uint8_t get_channel_info_rsp[] = { 0x1c, 0x42, 0x00, 0x01, 0x04, 0x01, |
| 286 | 0x00, 0xf2, 0x1b, 0x00, 0x00, 0x00 }; |
| 287 | |
| 288 | /* |
| 289 | * Get channel info |
| 290 | */ |
| 291 | static void test_kcs_channel_info(void) |
| 292 | { |
| 293 | uint8_t rsp[20]; |
| 294 | unsigned int rsplen = sizeof(rsp); |
| 295 | |
| 296 | kcs_cmd(get_channel_info_cmd, sizeof(get_channel_info_cmd), rsp, &rsplen); |
| 297 | g_assert(rsplen == sizeof(get_channel_info_rsp)); |
| 298 | g_assert(memcmp(get_channel_info_rsp, rsp, rsplen) == 0); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /* get ip address (specified in cmdline): 10.0.0.2 */ |
| 303 | static uint8_t get_ipaddr_cmd[] = { 0x30, 0x02, 0x01, 0x03, 0x00, 0x00 }; |
| 304 | static uint8_t get_ipaddr_rsp[] = { 0x34, 0x02, 0x00, 0x11, |
| 305 | 0x0a, 0x00, 0x00, 0x02 }; |
| 306 | |
| 307 | /* |
| 308 | * Get LAN configurations |
| 309 | */ |
| 310 | static void test_kcs_lan_get(void) |
| 311 | { |
| 312 | uint8_t rsp[20]; |
| 313 | unsigned int rsplen = sizeof(rsp); |
| 314 | |
| 315 | kcs_cmd(get_ipaddr_cmd, sizeof(get_ipaddr_cmd), rsp, &rsplen); |
| 316 | g_assert(rsplen == sizeof(get_ipaddr_rsp)); |
| 317 | g_assert(memcmp(get_ipaddr_rsp, rsp, rsplen) == 0); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | /* set/get ip address: 192.0.2.2 */ |
| 322 | static uint8_t lan_set_ipaddr_cmd[] = { 0x30, 0x01, 0x01, 0x03, |
| 323 | 0xc0, 0x00, 0x02, 0x02 }; |
| 324 | static uint8_t lan_set_ipaddr_rsp[] = { 0x34, 0x01, 0x00 }; |
| 325 | static uint8_t lan_get_ipaddr_cmd[] = { 0x30, 0x02, 0x01, 0x03, 0x00, 0x00 }; |
| 326 | static uint8_t lan_get_ipaddr_rsp[] = { 0x34, 0x02, 0x00, 0x11, |
| 327 | 0xc0, 0x00, 0x02, 0x02 }; |
| 328 | /* set ip address source: static */ |
| 329 | static uint8_t lan_set_ipsrc_cmd[] = { 0x30, 0x01, 0x01, 0x04, 0x01 }; |
| 330 | static uint8_t lan_set_ipsrc_rsp[] = { 0x34, 0x01, 0x00 }; |
| 331 | |
| 332 | /* set/get subnet mask: 255.255.255.0 */ |
| 333 | static uint8_t lan_set_netmask_cmd[] = { 0x30, 0x01, 0x01, 0x06, |
| 334 | 0xff, 0xff, 0xff, 0x00 }; |
| 335 | static uint8_t lan_set_netmask_rsp[] = { 0x34, 0x01, 0x00 }; |
| 336 | static uint8_t lan_get_netmask_cmd[] = { 0x30, 0x02, 0x01, 0x06, 0x00, 0x00 }; |
| 337 | static uint8_t lan_get_netmask_rsp[] = { 0x34, 0x02, 0x00, 0x11, |
| 338 | 0xff, 0xff, 0xff, 0x00 }; |
| 339 | |
| 340 | /* set/get default gateway ip address: 192.0.2.1 */ |
| 341 | static uint8_t lan_set_defgw_ipaddr_cmd[] = { 0x30, 0x01, 0x01, 0x0c, |
| 342 | 0xc0, 0x00, 0x02, 0x01 }; |
| 343 | static uint8_t lan_set_defgw_ipaddr_rsp[] = { 0x34, 0x01, 0x00 }; |
| 344 | static uint8_t lan_get_defgw_ipaddr_cmd[] = { 0x30, 0x02, 0x01, 0x0c, |
| 345 | 0x00, 0x00 }; |
| 346 | static uint8_t lan_get_defgw_ipaddr_rsp[] = { 0x34, 0x02, 0x00, 0x11, |
| 347 | 0xc0, 0x00, 0x02, 0x01 }; |
| 348 | |
| 349 | /* |
| 350 | * Set and then get LAN configurations |
| 351 | */ |
| 352 | static void test_kcs_lan_set_get(void) |
| 353 | { |
| 354 | uint8_t rsp[20]; |
| 355 | unsigned int rsplen = 0; |
| 356 | |
| 357 | /* set ip address */ |
| 358 | rsplen = sizeof(rsp); |
| 359 | kcs_cmd(lan_set_ipaddr_cmd, sizeof(lan_set_ipaddr_cmd), rsp, &rsplen); |
| 360 | g_assert(rsplen == sizeof(lan_set_ipaddr_rsp)); |
| 361 | g_assert(memcmp(lan_set_ipaddr_rsp, rsp, rsplen) == 0); |
| 362 | |
| 363 | /* get ip address */ |
| 364 | rsplen = sizeof(rsp); |
| 365 | kcs_cmd(lan_get_ipaddr_cmd, sizeof(lan_get_ipaddr_cmd), rsp, &rsplen); |
| 366 | g_assert(rsplen == sizeof(lan_get_ipaddr_rsp)); |
| 367 | g_assert(memcmp(lan_get_ipaddr_rsp, rsp, rsplen) == 0); |
| 368 | |
| 369 | /* set ip address source */ |
| 370 | rsplen = sizeof(rsp); |
| 371 | kcs_cmd(lan_set_ipsrc_cmd, sizeof(lan_set_ipsrc_cmd), rsp, &rsplen); |
| 372 | g_assert(rsplen == sizeof(lan_set_ipsrc_rsp)); |
| 373 | g_assert(memcmp(lan_set_ipsrc_rsp, rsp, rsplen) == 0); |
| 374 | |
| 375 | /* set subnet mask */ |
| 376 | rsplen = sizeof(rsp); |
| 377 | kcs_cmd(lan_set_netmask_cmd, sizeof(lan_set_netmask_cmd), rsp, &rsplen); |
| 378 | g_assert(rsplen == sizeof(lan_set_netmask_rsp)); |
| 379 | g_assert(memcmp(lan_set_netmask_rsp, rsp, rsplen) == 0); |
| 380 | |
| 381 | /* get subnet mask */ |
| 382 | rsplen = sizeof(rsp); |
| 383 | kcs_cmd(lan_get_netmask_cmd, sizeof(lan_get_netmask_cmd), rsp, &rsplen); |
| 384 | g_assert(rsplen == sizeof(lan_get_netmask_rsp)); |
| 385 | g_assert(memcmp(lan_get_netmask_rsp, rsp, rsplen) == 0); |
| 386 | |
| 387 | /* set default gateway ip address */ |
| 388 | rsplen = sizeof(rsp); |
| 389 | kcs_cmd(lan_set_defgw_ipaddr_cmd, sizeof(lan_set_defgw_ipaddr_cmd), |
| 390 | rsp, &rsplen); |
| 391 | g_assert(rsplen == sizeof(lan_set_defgw_ipaddr_rsp)); |
| 392 | g_assert(memcmp(lan_set_defgw_ipaddr_rsp, rsp, rsplen) == 0); |
| 393 | |
| 394 | /* get default gateway ip address */ |
| 395 | rsplen = sizeof(rsp); |
| 396 | kcs_cmd(lan_get_defgw_ipaddr_cmd, sizeof(lan_get_defgw_ipaddr_cmd), |
| 397 | rsp, &rsplen); |
| 398 | g_assert(rsplen == sizeof(lan_get_defgw_ipaddr_rsp)); |
| 399 | g_assert(memcmp(lan_get_defgw_ipaddr_rsp, rsp, rsplen) == 0); |
| 400 | } |
| 401 | |
| 402 | |
| 403 | int main(int argc, char **argv) |
| 404 | { |
| 405 | char *cmdline; |
| 406 | int ret; |
| 407 | |
| 408 | /* Run the tests */ |
| 409 | g_test_init(&argc, &argv, NULL); |
| 410 | |
| 411 | cmdline = g_strdup_printf("-device ipmi-bmc-sim,id=bmc0" |
| 412 | ",lan.channel=1,lan.ipaddr=10.0.0.2" |
| 413 | " -device isa-ipmi-kcs,bmc=bmc0"); |
| 414 | qtest_start(cmdline); |
| 415 | g_free(cmdline); |
| 416 | qtest_irq_intercept_in(global_qtest, "ioapic"); |
| 417 | qtest_add_func("/ipmi/local/kcs_base", test_kcs_base); |
| 418 | qtest_add_func("/ipmi/local/kcs_abort", test_kcs_abort); |
| 419 | qtest_add_func("/ipmi/local/kcs_enable_irq", test_enable_irq); |
| 420 | qtest_add_func("/ipmi/local/kcs_base_irq", test_kcs_base); |
| 421 | qtest_add_func("/ipmi/local/kcs_abort_irq", test_kcs_abort); |
| 422 | qtest_add_func("/ipmi/local/kcs_channel_access", test_kcs_channel_access); |
| 423 | qtest_add_func("/ipmi/local/kcs_channel_info", test_kcs_channel_info); |
| 424 | qtest_add_func("/ipmi/local/kcs_lan_get", test_kcs_lan_get); |
| 425 | qtest_add_func("/ipmi/local/kcs_lan_set_get", test_kcs_lan_set_get); |
| 426 | ret = g_test_run(); |
| 427 | qtest_quit(global_qtest); |
| 428 | |
| 429 | return ret; |
| 430 | } |