2 * QTest testcase for Realtek 8139 NIC
4 * Copyright (c) 2013-2014 SUSE LINUX Products GmbH
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "libqtest-single.h"
12 #include "libqos/pci-pc.h"
13 #include "qemu/timer.h"
15 static int verbosity_level;
17 /* Tests only initialization so far. TODO: Replace with functional tests */
24 static QPCIBus *pcibus;
25 static QPCIDevice *dev;
26 static QPCIBar dev_bar;
28 static void save_fn(QPCIDevice *dev, int devfn, void *data)
30 QPCIDevice **pdev = (QPCIDevice **) data;
35 static QPCIDevice *get_device(void)
39 pcibus = qpci_new_pc(global_qtest, NULL);
40 qpci_device_foreach(pcibus, 0x10ec, 0x8139, save_fn, &dev);
41 g_assert(dev != NULL);
46 #define PORT(name, len, val) \
47 static unsigned __attribute__((unused)) in_##name(void) \
49 unsigned res = qpci_io_read##len(dev, dev_bar, (val)); \
50 if (verbosity_level >= 2) { \
51 g_test_message("*%s -> %x", #name, res); \
55 static void out_##name(unsigned v) \
57 if (verbosity_level >= 2) { \
58 g_test_message("%x -> *%s", v, #name); \
60 qpci_io_write##len(dev, dev_bar, (val), v); \
64 PORT(IntrMask, w, 0x3c)
65 PORT(IntrStatus, w, 0x3E)
66 PORT(TimerInt, l, 0x54)
68 #define fatal(...) do { g_test_message(__VA_ARGS__); g_assert(0); } while (0)
70 static void test_timer(void)
72 const unsigned from = 0.95 * CLK;
73 const unsigned to = 1.6 * CLK;
74 unsigned prev, curr, next;
83 /* Test 1. test counter continue and continue */
84 out_TimerInt(0); /* disable timer */
85 out_IntrStatus(0x4000);
86 out_Timer(12345); /* reset timer to 0 */
88 if (curr > 0.1 * CLK) {
89 fatal("time too big %u\n", curr);
92 clock_step(1 * NANOSECONDS_PER_SECOND);
96 /* test skip is in a specific range */
97 diff = (curr-prev) & 0xffffffffu;
98 if (diff < from || diff > to) {
99 fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
101 if (curr < prev && ++cnt == 3) {
106 /* Test 2. Check we didn't get an interrupt with TimerInt == 0 */
107 if (in_IntrStatus() & 0x4000) {
108 fatal("got an interrupt\n");
111 /* Test 3. Setting TimerInt to 1 and Timer to 0 get interrupt */
115 if ((in_IntrStatus() & 0x4000) == 0) {
116 fatal("we should have an interrupt here!\n");
119 /* Test 3. Check acknowledge */
120 out_IntrStatus(0x4000);
121 if (in_IntrStatus() & 0x4000) {
122 fatal("got an interrupt\n");
125 /* Test. Status set after Timer reset */
128 out_IntrStatus(0x4000);
130 out_TimerInt(curr + 0.5 * CLK);
131 clock_step(1 * NANOSECONDS_PER_SECOND);
133 if ((in_IntrStatus() & 0x4000) == 0) {
134 fatal("we should have an interrupt here!\n");
137 /* Test. Status set after TimerInt reset */
140 out_IntrStatus(0x4000);
142 out_TimerInt(curr + 0.5 * CLK);
143 clock_step(1 * NANOSECONDS_PER_SECOND);
145 if ((in_IntrStatus() & 0x4000) == 0) {
146 fatal("we should have an interrupt here!\n");
149 /* Test 4. Increment TimerInt we should see an interrupt */
151 next = curr + 5.0 * CLK;
154 clock_step(1 * NANOSECONDS_PER_SECOND);
157 diff = (curr-prev) & 0xffffffffu;
158 if (diff < from || diff > to) {
159 fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
161 if (cnt < 3 && curr > next) {
162 if ((in_IntrStatus() & 0x4000) == 0) {
163 fatal("we should have an interrupt here!\n");
165 out_IntrStatus(0x4000);
166 next = curr + 5.0 * CLK;
171 /* Test 5. Second time we pass from 0 should see an interrupt */
172 } else if (cnt >= 3 && curr < prev) {
173 /* here we should have an interrupt */
174 if ((in_IntrStatus() & 0x4000) == 0) {
175 fatal("we should have an interrupt here!\n");
177 out_IntrStatus(0x4000);
184 g_test_message("Everythink is ok!");
188 static void test_init(void)
194 dev_bar = qpci_iomap(dev, 0, &barsize);
196 qpci_device_enable(dev);
201 int main(int argc, char **argv)
204 char *v_env = getenv("V");
207 verbosity_level = atoi(v_env);
210 g_test_init(&argc, &argv, NULL);
212 if (!qtest_has_device("rtl8139")) {
216 qtest_start("-device rtl8139");
218 qtest_add_func("/rtl8139/nop", nop);
219 qtest_add_func("/rtl8139/timer", test_init);