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.
13 #include "libqos/pci-pc.h"
14 #include "qemu/osdep.h"
15 #include "qemu/timer.h"
16 #include "qemu-common.h"
18 /* Tests only initialization so far. TODO: Replace with functional tests */
25 static QPCIBus *pcibus;
26 static QPCIDevice *dev;
27 static void *dev_base;
29 static void save_fn(QPCIDevice *dev, int devfn, void *data)
31 QPCIDevice **pdev = (QPCIDevice **) data;
36 static QPCIDevice *get_device(void)
40 pcibus = qpci_init_pc();
41 qpci_device_foreach(pcibus, 0x10ec, 0x8139, save_fn, &dev);
42 g_assert(dev != NULL);
47 #define PORT(name, len, val) \
48 static unsigned __attribute__((unused)) in_##name(void) \
50 unsigned res = qpci_io_read##len(dev, dev_base+(val)); \
51 g_test_message("*%s -> %x\n", #name, res); \
54 static void out_##name(unsigned v) \
56 g_test_message("%x -> *%s\n", v, #name); \
57 qpci_io_write##len(dev, dev_base+(val), v); \
61 PORT(IntrMask, w, 0x3c)
62 PORT(IntrStatus, w, 0x3E)
63 PORT(TimerInt, l, 0x54)
65 #define fatal(...) do { g_test_message(__VA_ARGS__); g_assert(0); } while (0)
67 static void test_timer(void)
69 const unsigned from = 0.95 * CLK;
70 const unsigned to = 1.6 * CLK;
71 unsigned prev, curr, next;
80 /* Test 1. test counter continue and continue */
81 out_TimerInt(0); /* disable timer */
82 out_IntrStatus(0x4000);
83 out_Timer(12345); /* reset timer to 0 */
85 if (curr > 0.1 * CLK) {
86 fatal("time too big %u\n", curr);
89 clock_step(1 * NANOSECONDS_PER_SECOND);
93 /* test skip is in a specific range */
94 diff = (curr-prev) & 0xffffffffu;
95 if (diff < from || diff > to) {
96 fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
98 if (curr < prev && ++cnt == 3) {
103 /* Test 2. Check we didn't get an interrupt with TimerInt == 0 */
104 if (in_IntrStatus() & 0x4000) {
105 fatal("got an interrupt\n");
108 /* Test 3. Setting TimerInt to 1 and Timer to 0 get interrupt */
112 if ((in_IntrStatus() & 0x4000) == 0) {
113 fatal("we should have an interrupt here!\n");
116 /* Test 3. Check acknowledge */
117 out_IntrStatus(0x4000);
118 if (in_IntrStatus() & 0x4000) {
119 fatal("got an interrupt\n");
122 /* Test. Status set after Timer reset */
125 out_IntrStatus(0x4000);
127 out_TimerInt(curr + 0.5 * CLK);
128 clock_step(1 * NANOSECONDS_PER_SECOND);
130 if ((in_IntrStatus() & 0x4000) == 0) {
131 fatal("we should have an interrupt here!\n");
134 /* Test. Status set after TimerInt reset */
137 out_IntrStatus(0x4000);
139 out_TimerInt(curr + 0.5 * CLK);
140 clock_step(1 * NANOSECONDS_PER_SECOND);
142 if ((in_IntrStatus() & 0x4000) == 0) {
143 fatal("we should have an interrupt here!\n");
146 /* Test 4. Increment TimerInt we should see an interrupt */
148 next = curr + 5.0 * CLK;
151 clock_step(1 * NANOSECONDS_PER_SECOND);
154 diff = (curr-prev) & 0xffffffffu;
155 if (diff < from || diff > to) {
156 fatal("Invalid diff %u (%u-%u)\n", diff, from, to);
158 if (cnt < 3 && curr > next) {
159 if ((in_IntrStatus() & 0x4000) == 0) {
160 fatal("we should have an interrupt here!\n");
162 out_IntrStatus(0x4000);
163 next = curr + 5.0 * CLK;
168 /* Test 5. Second time we pass from 0 should see an interrupt */
169 } else if (cnt >= 3 && curr < prev) {
170 /* here we should have an interrupt */
171 if ((in_IntrStatus() & 0x4000) == 0) {
172 fatal("we should have an interrupt here!\n");
174 out_IntrStatus(0x4000);
181 g_test_message("Everythink is ok!\n");
185 static void test_init(void)
191 dev_base = qpci_iomap(dev, 0, &barsize);
193 g_assert(dev_base != NULL);
195 qpci_device_enable(dev);
200 int main(int argc, char **argv)
204 g_test_init(&argc, &argv, NULL);
205 qtest_add_func("/rtl8139/nop", nop);
206 qtest_add_func("/rtl8139/timer", test_init);
208 qtest_start("-device rtl8139");