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"
13 #include "libqos/pci-pc.h"
14 #include "qemu/timer.h"
15 #include "qemu-common.h"
17 /* Tests only initialization so far. TODO: Replace with functional tests */
24 static QPCIBus *pcibus;
25 static QPCIDevice *dev;
26 static void *dev_base;
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_init_pc();
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_base+(val)); \
50 g_test_message("*%s -> %x\n", #name, res); \
53 static void out_##name(unsigned v) \
55 g_test_message("%x -> *%s\n", v, #name); \
56 qpci_io_write##len(dev, dev_base+(val), v); \
60 PORT(IntrMask, w, 0x3c)
61 PORT(IntrStatus, w, 0x3E)
62 PORT(TimerInt, l, 0x54)
64 #define fatal(...) do { g_test_message(__VA_ARGS__); g_assert(0); } while (0)
66 static void test_timer(void)
68 const unsigned from = 0.95 * CLK;
69 const unsigned to = 1.6 * CLK;
70 unsigned prev, curr, next;
79 /* Test 1. test counter continue and continue */
80 out_TimerInt(0); /* disable timer */
81 out_IntrStatus(0x4000);
82 out_Timer(12345); /* reset timer to 0 */
84 if (curr > 0.1 * CLK) {
85 fatal("time too big %u\n", curr);
88 clock_step(1 * NANOSECONDS_PER_SECOND);
92 /* test skip is in a specific range */
93 diff = (curr-prev) & 0xffffffffu;
94 if (diff < from || diff > to) {
95 fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
97 if (curr < prev && ++cnt == 3) {
102 /* Test 2. Check we didn't get an interrupt with TimerInt == 0 */
103 if (in_IntrStatus() & 0x4000) {
104 fatal("got an interrupt\n");
107 /* Test 3. Setting TimerInt to 1 and Timer to 0 get interrupt */
111 if ((in_IntrStatus() & 0x4000) == 0) {
112 fatal("we should have an interrupt here!\n");
115 /* Test 3. Check acknowledge */
116 out_IntrStatus(0x4000);
117 if (in_IntrStatus() & 0x4000) {
118 fatal("got an interrupt\n");
121 /* Test. Status set after Timer reset */
124 out_IntrStatus(0x4000);
126 out_TimerInt(curr + 0.5 * CLK);
127 clock_step(1 * NANOSECONDS_PER_SECOND);
129 if ((in_IntrStatus() & 0x4000) == 0) {
130 fatal("we should have an interrupt here!\n");
133 /* Test. Status set after TimerInt reset */
136 out_IntrStatus(0x4000);
138 out_TimerInt(curr + 0.5 * CLK);
139 clock_step(1 * NANOSECONDS_PER_SECOND);
141 if ((in_IntrStatus() & 0x4000) == 0) {
142 fatal("we should have an interrupt here!\n");
145 /* Test 4. Increment TimerInt we should see an interrupt */
147 next = curr + 5.0 * CLK;
150 clock_step(1 * NANOSECONDS_PER_SECOND);
153 diff = (curr-prev) & 0xffffffffu;
154 if (diff < from || diff > to) {
155 fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
157 if (cnt < 3 && curr > next) {
158 if ((in_IntrStatus() & 0x4000) == 0) {
159 fatal("we should have an interrupt here!\n");
161 out_IntrStatus(0x4000);
162 next = curr + 5.0 * CLK;
167 /* Test 5. Second time we pass from 0 should see an interrupt */
168 } else if (cnt >= 3 && curr < prev) {
169 /* here we should have an interrupt */
170 if ((in_IntrStatus() & 0x4000) == 0) {
171 fatal("we should have an interrupt here!\n");
173 out_IntrStatus(0x4000);
180 g_test_message("Everythink is ok!\n");
184 static void test_init(void)
190 dev_base = qpci_iomap(dev, 0, &barsize);
192 g_assert(dev_base != NULL);
194 qpci_device_enable(dev);
199 int main(int argc, char **argv)
203 g_test_init(&argc, &argv, NULL);
204 qtest_add_func("/rtl8139/nop", nop);
205 qtest_add_func("/rtl8139/timer", test_init);
207 qtest_start("-device rtl8139");