xref: /openbmc/qemu/tests/qtest/ipmi-kcs-test.c (revision 4e6b1384)
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         g_assert(--count != 0);
77     }
78 }
79 
80 static void kcs_wait_obf(void)
81 {
82     unsigned int count = 1000;
83     while (IPMI_KCS_CMDREG_GET_OBF() == 0) {
84         g_assert(--count != 0);
85     }
86 }
87 
88 static void kcs_clear_obf(void)
89 {
90     if (kcs_ints_enabled) {
91         g_assert(get_irq(IPMI_IRQ));
92     } else {
93         g_assert(!get_irq(IPMI_IRQ));
94     }
95     g_assert(IPMI_KCS_CMDREG_GET_OBF() == 1);
96     kcs_get_datareg();
97     g_assert(IPMI_KCS_CMDREG_GET_OBF() == 0);
98     g_assert(!get_irq(IPMI_IRQ));
99 }
100 
101 static void kcs_check_state(uint8_t state)
102 {
103     g_assert(IPMI_KCS_CMDREG_GET_STATE() == state);
104 }
105 
106 static void kcs_cmd(uint8_t *cmd, unsigned int cmd_len,
107                     uint8_t *rsp, unsigned int *rsp_len)
108 {
109     unsigned int i, j = 0;
110 
111     /* Should be idle */
112     g_assert(kcs_get_cmdreg() == 0);
113 
114     kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_START);
115     kcs_wait_ibf();
116     kcs_check_state(IPMI_KCS_STATE_WRITE);
117     kcs_clear_obf();
118     for (i = 0; i < cmd_len; i++) {
119         kcs_write_datareg(cmd[i]);
120         kcs_wait_ibf();
121         kcs_check_state(IPMI_KCS_STATE_WRITE);
122         kcs_clear_obf();
123     }
124     kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_END);
125     kcs_wait_ibf();
126     kcs_check_state(IPMI_KCS_STATE_WRITE);
127     kcs_clear_obf();
128     kcs_write_datareg(0);
129  next_read_byte:
130     kcs_wait_ibf();
131     switch (IPMI_KCS_CMDREG_GET_STATE()) {
132     case IPMI_KCS_STATE_READ:
133         kcs_wait_obf();
134         g_assert(j < *rsp_len);
135         rsp[j++] = kcs_get_datareg();
136         kcs_write_datareg(IPMI_KCS_CMD_READ);
137         goto next_read_byte;
138         break;
139 
140     case IPMI_KCS_STATE_IDLE:
141         kcs_wait_obf();
142         kcs_get_datareg();
143         break;
144 
145     default:
146         g_assert(0);
147     }
148     *rsp_len = j;
149 }
150 
151 static void kcs_abort(uint8_t *cmd, unsigned int cmd_len,
152                       uint8_t *rsp, unsigned int *rsp_len)
153 {
154     unsigned int i, j = 0;
155     unsigned int retries = 4;
156 
157     /* Should be idle */
158     g_assert(kcs_get_cmdreg() == 0);
159 
160     kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_START);
161     kcs_wait_ibf();
162     kcs_check_state(IPMI_KCS_STATE_WRITE);
163     kcs_clear_obf();
164     for (i = 0; i < cmd_len; i++) {
165         kcs_write_datareg(cmd[i]);
166         kcs_wait_ibf();
167         kcs_check_state(IPMI_KCS_STATE_WRITE);
168         kcs_clear_obf();
169     }
170     kcs_write_cmdreg(IPMI_KCS_CMD_WRITE_END);
171     kcs_wait_ibf();
172     kcs_check_state(IPMI_KCS_STATE_WRITE);
173     kcs_clear_obf();
174     kcs_write_datareg(0);
175     kcs_wait_ibf();
176     switch (IPMI_KCS_CMDREG_GET_STATE()) {
177     case IPMI_KCS_STATE_READ:
178         kcs_wait_obf();
179         g_assert(j < *rsp_len);
180         rsp[j++] = kcs_get_datareg();
181         kcs_write_datareg(IPMI_KCS_CMD_READ);
182         break;
183 
184     default:
185         g_assert(0);
186     }
187 
188     /* Start the abort here */
189  retry_abort:
190     g_assert(retries > 0);
191 
192     kcs_wait_ibf();
193     kcs_write_cmdreg(IPMI_KCS_STATUS_ABORT);
194     kcs_wait_ibf();
195     kcs_clear_obf();
196     kcs_write_datareg(0);
197     kcs_wait_ibf();
198     if (IPMI_KCS_CMDREG_GET_STATE() != IPMI_KCS_STATE_READ) {
199         retries--;
200         goto retry_abort;
201     }
202     kcs_wait_obf();
203     rsp[0] = kcs_get_datareg();
204     kcs_write_datareg(IPMI_KCS_CMD_READ);
205     kcs_wait_ibf();
206     if (IPMI_KCS_CMDREG_GET_STATE() != IPMI_KCS_STATE_IDLE) {
207         retries--;
208         goto retry_abort;
209     }
210     kcs_wait_obf();
211     kcs_clear_obf();
212 
213     *rsp_len = j;
214 }
215 
216 
217 static uint8_t get_dev_id_cmd[] = { 0x18, 0x01 };
218 static uint8_t get_dev_id_rsp[] = { 0x1c, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00,
219                                     0x02, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00 };
220 
221 /*
222  * Send a get_device_id to do a basic test.
223  */
224 static void test_kcs_base(void)
225 {
226     uint8_t rsp[20];
227     unsigned int rsplen = sizeof(rsp);
228 
229     kcs_cmd(get_dev_id_cmd, sizeof(get_dev_id_cmd), rsp, &rsplen);
230     g_assert(rsplen == sizeof(get_dev_id_rsp));
231     g_assert(memcmp(get_dev_id_rsp, rsp, rsplen) == 0);
232 }
233 
234 /*
235  * Abort a kcs operation while reading
236  */
237 static void test_kcs_abort(void)
238 {
239     uint8_t rsp[20];
240     unsigned int rsplen = sizeof(rsp);
241 
242     kcs_abort(get_dev_id_cmd, sizeof(get_dev_id_cmd), rsp, &rsplen);
243     g_assert(rsp[0] == IPMI_KCS_ABORTED_BY_CMD);
244 }
245 
246 static uint8_t set_bmc_globals_cmd[] = { 0x18, 0x2e, 0x0f };
247 static uint8_t set_bmc_globals_rsp[] = { 0x1c, 0x2e, 0x00 };
248 
249 /*
250  * Enable interrupts
251  */
252 static void test_enable_irq(void)
253 {
254     uint8_t rsp[20];
255     unsigned int rsplen = sizeof(rsp);
256 
257     kcs_cmd(set_bmc_globals_cmd, sizeof(set_bmc_globals_cmd), rsp, &rsplen);
258     g_assert(rsplen == sizeof(set_bmc_globals_rsp));
259     g_assert(memcmp(set_bmc_globals_rsp, rsp, rsplen) == 0);
260     kcs_ints_enabled = 1;
261 }
262 
263 int main(int argc, char **argv)
264 {
265     char *cmdline;
266     int ret;
267 
268     /* Run the tests */
269     g_test_init(&argc, &argv, NULL);
270 
271     cmdline = g_strdup_printf("-device ipmi-bmc-sim,id=bmc0"
272                               " -device isa-ipmi-kcs,bmc=bmc0");
273     qtest_start(cmdline);
274     g_free(cmdline);
275     qtest_irq_intercept_in(global_qtest, "ioapic");
276     qtest_add_func("/ipmi/local/kcs_base", test_kcs_base);
277     qtest_add_func("/ipmi/local/kcs_abort", test_kcs_abort);
278     qtest_add_func("/ipmi/local/kcs_enable_irq", test_enable_irq);
279     qtest_add_func("/ipmi/local/kcs_base_irq", test_kcs_base);
280     qtest_add_func("/ipmi/local/kcs_abort_irq", test_kcs_abort);
281     ret = g_test_run();
282     qtest_quit(global_qtest);
283 
284     return ret;
285 }
286