block: Use returned *file in bdrv_co_get_block_status
[qemu.git] / tests / ipmi-kcs-test.c
blob564c470f55132d6dedb202184d6542041722b8b8
1 /*
2 * IPMI KCS test cases, using the local interface.
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 <stdint.h>
26 #include <string.h>
27 #include <stdio.h>
29 #include <glib.h>
31 #include "libqtest.h"
33 #define IPMI_IRQ 5
35 #define IPMI_KCS_BASE 0xca2
37 #define IPMI_KCS_STATUS_ABORT 0x60
38 #define IPMI_KCS_CMD_WRITE_START 0x61
39 #define IPMI_KCS_CMD_WRITE_END 0x62
40 #define IPMI_KCS_CMD_READ 0x68
42 #define IPMI_KCS_ABORTED_BY_CMD 0x01
44 #define IPMI_KCS_CMDREG_GET_STATE() ((kcs_get_cmdreg() >> 6) & 3)
45 #define IPMI_KCS_STATE_IDLE 0
46 #define IPMI_KCS_STATE_READ 1
47 #define IPMI_KCS_STATE_WRITE 2
48 #define IPMI_KCS_STATE_ERROR 3
49 #define IPMI_KCS_CMDREG_GET_CD() ((kcs_get_cmdreg() >> 3) & 1)
50 #define IPMI_KCS_CMDREG_GET_ATN() ((kcs_get_cmdreg() >> 2) & 1)
51 #define IPMI_KCS_CMDREG_GET_IBF() ((kcs_get_cmdreg() >> 1) & 1)
52 #define IPMI_KCS_CMDREG_GET_OBF() ((kcs_get_cmdreg() >> 0) & 1)
54 static int kcs_ints_enabled;
56 static uint8_t kcs_get_cmdreg(void)
58 return inb(IPMI_KCS_BASE + 1);
61 static void kcs_write_cmdreg(uint8_t val)
63 outb(IPMI_KCS_BASE + 1, val);
66 static uint8_t kcs_get_datareg(void)
68 return inb(IPMI_KCS_BASE);
71 static void kcs_write_datareg(uint8_t val)
73 outb(IPMI_KCS_BASE, val);
76 static void kcs_wait_ibf(void)
78 unsigned int count = 1000;
79 while (IPMI_KCS_CMDREG_GET_IBF() != 0) {
80 g_assert(--count != 0);
84 static void kcs_wait_obf(void)
86 unsigned int count = 1000;
87 while (IPMI_KCS_CMDREG_GET_OBF() == 0) {
88 g_assert(--count != 0);
92 static void kcs_clear_obf(void)
94 if (kcs_ints_enabled) {
95 g_assert(get_irq(IPMI_IRQ));
96 } else {
97 g_assert(!get_irq(IPMI_IRQ));
99 g_assert(IPMI_KCS_CMDREG_GET_OBF() == 1);
100 kcs_get_datareg();
101 g_assert(IPMI_KCS_CMDREG_GET_OBF() == 0);
102 g_assert(!get_irq(IPMI_IRQ));
105 static void kcs_check_state(uint8_t state)
107 g_assert(IPMI_KCS_CMDREG_GET_STATE() == state);
110 static void kcs_cmd(uint8_t *cmd, unsigned int cmd_len,
111 uint8_t *rsp, unsigned int *rsp_len)
113 unsigned int i, j = 0;
115 /* Should be idle */
116 g_assert(kcs_get_cmdreg() == 0);
118 kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_START);
119 kcs_wait_ibf();
120 kcs_check_state(IPMI_KCS_STATE_WRITE);
121 kcs_clear_obf();
122 for (i = 0; i < cmd_len; i++) {
123 kcs_write_datareg(cmd[i]);
124 kcs_wait_ibf();
125 kcs_check_state(IPMI_KCS_STATE_WRITE);
126 kcs_clear_obf();
128 kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_END);
129 kcs_wait_ibf();
130 kcs_check_state(IPMI_KCS_STATE_WRITE);
131 kcs_clear_obf();
132 kcs_write_datareg(0);
133 next_read_byte:
134 kcs_wait_ibf();
135 switch (IPMI_KCS_CMDREG_GET_STATE()) {
136 case IPMI_KCS_STATE_READ:
137 kcs_wait_obf();
138 g_assert(j < *rsp_len);
139 rsp[j++] = kcs_get_datareg();
140 kcs_write_datareg(IPMI_KCS_CMD_READ);
141 goto next_read_byte;
142 break;
144 case IPMI_KCS_STATE_IDLE:
145 kcs_wait_obf();
146 kcs_get_datareg();
147 break;
149 default:
150 g_assert(0);
152 *rsp_len = j;
155 static void kcs_abort(uint8_t *cmd, unsigned int cmd_len,
156 uint8_t *rsp, unsigned int *rsp_len)
158 unsigned int i, j = 0;
159 unsigned int retries = 4;
161 /* Should be idle */
162 g_assert(kcs_get_cmdreg() == 0);
164 kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_START);
165 kcs_wait_ibf();
166 kcs_check_state(IPMI_KCS_STATE_WRITE);
167 kcs_clear_obf();
168 for (i = 0; i < cmd_len; i++) {
169 kcs_write_datareg(cmd[i]);
170 kcs_wait_ibf();
171 kcs_check_state(IPMI_KCS_STATE_WRITE);
172 kcs_clear_obf();
174 kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_END);
175 kcs_wait_ibf();
176 kcs_check_state(IPMI_KCS_STATE_WRITE);
177 kcs_clear_obf();
178 kcs_write_datareg(0);
179 kcs_wait_ibf();
180 switch (IPMI_KCS_CMDREG_GET_STATE()) {
181 case IPMI_KCS_STATE_READ:
182 kcs_wait_obf();
183 g_assert(j < *rsp_len);
184 rsp[j++] = kcs_get_datareg();
185 kcs_write_datareg(IPMI_KCS_CMD_READ);
186 break;
188 default:
189 g_assert(0);
192 /* Start the abort here */
193 retry_abort:
194 g_assert(retries > 0);
196 kcs_wait_ibf();
197 kcs_write_cmdreg(IPMI_KCS_STATUS_ABORT);
198 kcs_wait_ibf();
199 kcs_clear_obf();
200 kcs_write_datareg(0);
201 kcs_wait_ibf();
202 if (IPMI_KCS_CMDREG_GET_STATE() != IPMI_KCS_STATE_READ) {
203 retries--;
204 goto retry_abort;
206 kcs_wait_obf();
207 rsp[0] = kcs_get_datareg();
208 kcs_write_datareg(IPMI_KCS_CMD_READ);
209 kcs_wait_ibf();
210 if (IPMI_KCS_CMDREG_GET_STATE() != IPMI_KCS_STATE_IDLE) {
211 retries--;
212 goto retry_abort;
214 kcs_wait_obf();
215 kcs_clear_obf();
217 *rsp_len = j;
221 static uint8_t get_dev_id_cmd[] = { 0x18, 0x01 };
222 static uint8_t get_dev_id_rsp[] = { 0x1c, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00,
223 0x02, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00 };
226 * Send a get_device_id to do a basic test.
228 static void test_kcs_base(void)
230 uint8_t rsp[20];
231 unsigned int rsplen = sizeof(rsp);
233 kcs_cmd(get_dev_id_cmd, sizeof(get_dev_id_cmd), rsp, &rsplen);
234 g_assert(rsplen == sizeof(get_dev_id_rsp));
235 g_assert(memcmp(get_dev_id_rsp, rsp, rsplen) == 0);
239 * Abort a kcs operation while reading
241 static void test_kcs_abort(void)
243 uint8_t rsp[20];
244 unsigned int rsplen = sizeof(rsp);
246 kcs_abort(get_dev_id_cmd, sizeof(get_dev_id_cmd), rsp, &rsplen);
247 g_assert(rsp[0] == IPMI_KCS_ABORTED_BY_CMD);
250 static uint8_t set_bmc_globals_cmd[] = { 0x18, 0x2e, 0x0f };
251 static uint8_t set_bmc_globals_rsp[] = { 0x1c, 0x2e, 0x00 };
254 * Enable interrupts
256 static void test_enable_irq(void)
258 uint8_t rsp[20];
259 unsigned int rsplen = sizeof(rsp);
261 kcs_cmd(set_bmc_globals_cmd, sizeof(set_bmc_globals_cmd), rsp, &rsplen);
262 g_assert(rsplen == sizeof(set_bmc_globals_rsp));
263 g_assert(memcmp(set_bmc_globals_rsp, rsp, rsplen) == 0);
264 kcs_ints_enabled = 1;
267 int main(int argc, char **argv)
269 const char *arch = qtest_get_arch();
270 char *cmdline;
271 int ret;
273 /* Check architecture */
274 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
275 g_test_message("Skipping test for non-x86\n");
276 return 0;
279 /* Run the tests */
280 g_test_init(&argc, &argv, NULL);
282 cmdline = g_strdup_printf("-vnc none -device ipmi-bmc-sim,id=bmc0"
283 " -device isa-ipmi-kcs,bmc=bmc0");
284 qtest_start(cmdline);
285 qtest_irq_intercept_in(global_qtest, "ioapic");
286 qtest_add_func("/ipmi/local/kcs_base", test_kcs_base);
287 qtest_add_func("/ipmi/local/kcs_abort", test_kcs_abort);
288 qtest_add_func("/ipmi/local/kcs_enable_irq", test_enable_irq);
289 qtest_add_func("/ipmi/local/kcs_base_irq", test_kcs_base);
290 qtest_add_func("/ipmi/local/kcs_abort_irq", test_kcs_abort);
291 ret = g_test_run();
292 qtest_quit(global_qtest);
294 return ret;