blob: d0195e6bf096fb352eba4d04109bd65bedb77b28 [file] [log] [blame]
Andrew Scull8df508f2022-05-16 10:41:36 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <virtio_types.h>
9#include <virtio.h>
10#include <virtio_ring.h>
11#include <dm/device-internal.h>
12#include <dm/root.h>
13#include <dm/test.h>
14#include <dm/uclass-internal.h>
15#include <test/test.h>
16#include <test/ut.h>
17
18/* Basic test of the virtio uclass */
19static int dm_test_virtio_base(struct unit_test_state *uts)
20{
21 struct udevice *bus, *dev;
22 u8 status;
23
24 /* check probe success */
25 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
26 ut_assertnonnull(bus);
27
Andrew Scullacd3b272022-05-16 10:41:37 +000028 /* check the child virtio-rng device is bound */
Andrew Scull8df508f2022-05-16 10:41:36 +000029 ut_assertok(device_find_first_child(bus, &dev));
30 ut_assertnonnull(dev);
Andrew Scullacd3b272022-05-16 10:41:37 +000031 ut_asserteq_str("virtio-rng#0", dev->name);
Andrew Scull8df508f2022-05-16 10:41:36 +000032
33 /* check driver status */
34 ut_assertok(virtio_get_status(dev, &status));
35 ut_asserteq(VIRTIO_CONFIG_S_ACKNOWLEDGE, status);
36
Andrew Scull420b3e52022-05-16 10:41:38 +000037 /* probe the virtio-rng driver */
38 ut_assertok(device_probe(dev));
39
40 /* check the device was reset and the driver picked up the device */
41 ut_assertok(virtio_get_status(dev, &status));
42 ut_asserteq(VIRTIO_CONFIG_S_DRIVER |
43 VIRTIO_CONFIG_S_DRIVER_OK |
44 VIRTIO_CONFIG_S_FEATURES_OK, status);
45
Andrew Scull8df508f2022-05-16 10:41:36 +000046 return 0;
47}
48DM_TEST(dm_test_virtio_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
49
50/* Test all of the virtio uclass ops */
51static int dm_test_virtio_all_ops(struct unit_test_state *uts)
52{
53 struct udevice *bus, *dev;
54 struct virtio_dev_priv *uc_priv;
55 uint offset = 0, len = 0, nvqs = 1;
56 void *buffer = NULL;
57 u8 status;
58 u32 counter;
59 u64 features;
60 struct virtqueue *vqs[2];
61
62 /* check probe success */
63 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
64 ut_assertnonnull(bus);
65
66 /* check the child virtio-rng device is bound */
67 ut_assertok(device_find_first_child(bus, &dev));
68 ut_assertnonnull(dev);
69
70 /*
71 * fake the virtio device probe by filling in uc_priv->vdev
72 * which is used by virtio_find_vqs/virtio_del_vqs.
73 */
74 uc_priv = dev_get_uclass_priv(bus);
75 ut_assertnonnull(uc_priv);
76 uc_priv->vdev = dev;
77
78 /* test virtio_xxx APIs */
79 ut_assertok(virtio_get_config(dev, offset, buffer, len));
80 ut_assertok(virtio_set_config(dev, offset, buffer, len));
81 ut_asserteq(-ENOSYS, virtio_generation(dev, &counter));
82 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
83 ut_assertok(virtio_get_status(dev, &status));
84 ut_asserteq(VIRTIO_CONFIG_S_DRIVER_OK, status);
85 ut_assertok(virtio_reset(dev));
86 ut_assertok(virtio_get_status(dev, &status));
87 ut_asserteq(0, status);
88 ut_assertok(virtio_get_features(dev, &features));
89 ut_asserteq_64(BIT_ULL(VIRTIO_F_VERSION_1), features);
90 ut_assertok(virtio_set_features(dev));
91 ut_assertok(virtio_find_vqs(dev, nvqs, vqs));
92 ut_assertok(virtio_notify(dev, vqs[0]));
93 ut_assertok(virtio_del_vqs(dev));
94
95 return 0;
96}
97DM_TEST(dm_test_virtio_all_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
98
99/* Test removal of virtio device driver */
100static int dm_test_virtio_remove(struct unit_test_state *uts)
101{
102 struct udevice *bus, *dev;
103
104 /* check probe success */
105 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
106 ut_assertnonnull(bus);
107
108 /* check the child virtio-rng device is bound */
109 ut_assertok(device_find_first_child(bus, &dev));
110 ut_assertnonnull(dev);
111
112 /* set driver status to VIRTIO_CONFIG_S_DRIVER_OK */
113 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
114
115 /* check the device can be successfully removed */
116 dev_or_flags(dev, DM_FLAG_ACTIVATED);
117 ut_asserteq(-EKEYREJECTED, device_remove(bus, DM_REMOVE_ACTIVE_ALL));
118
119 ut_asserteq(false, device_active(dev));
120
121 return 0;
122}
123DM_TEST(dm_test_virtio_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
124
125/* Test all of the virtio ring */
126static int dm_test_virtio_ring(struct unit_test_state *uts)
127{
128 struct udevice *bus, *dev;
129 struct virtio_dev_priv *uc_priv;
130 struct virtqueue *vq;
131 struct virtio_sg sg[2];
132 struct virtio_sg *sgs[2];
133 unsigned int len;
134 u8 buffer[2][32];
135
136 /* check probe success */
137 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
138 ut_assertnonnull(bus);
139
140 /* check the child virtio-blk device is bound */
141 ut_assertok(device_find_first_child(bus, &dev));
142 ut_assertnonnull(dev);
143
144 /*
145 * fake the virtio device probe by filling in uc_priv->vdev
146 * which is used by virtio_find_vqs/virtio_del_vqs.
147 */
148 uc_priv = dev_get_uclass_priv(bus);
149 ut_assertnonnull(uc_priv);
150 uc_priv->vdev = dev;
151
152 /* prepare the scatter-gather buffer */
153 sg[0].addr = buffer[0];
154 sg[0].length = sizeof(buffer[0]);
155 sg[1].addr = buffer[1];
156 sg[1].length = sizeof(buffer[1]);
157 sgs[0] = &sg[0];
158 sgs[1] = &sg[1];
159
160 /* read a buffer and report written size from device */
161 ut_assertok(virtio_find_vqs(dev, 1, &vq));
162 ut_assertok(virtqueue_add(vq, sgs, 0, 1));
163 vq->vring.used->idx = 1;
164 vq->vring.used->ring[0].id = 0;
165 vq->vring.used->ring[0].len = 0x53355885;
166 ut_asserteq_ptr(buffer, virtqueue_get_buf(vq, &len));
167 ut_asserteq(0x53355885, len);
168 ut_assertok(virtio_del_vqs(dev));
169
170 /* rejects used descriptors that aren't a chain head */
171 ut_assertok(virtio_find_vqs(dev, 1, &vq));
172 ut_assertok(virtqueue_add(vq, sgs, 0, 2));
173 vq->vring.used->idx = 1;
174 vq->vring.used->ring[0].id = 1;
175 vq->vring.used->ring[0].len = 0x53355885;
176 ut_assertnull(virtqueue_get_buf(vq, &len));
177 ut_assertok(virtio_del_vqs(dev));
178
179 /* device changes to descriptor are ignored */
180 ut_assertok(virtio_find_vqs(dev, 1, &vq));
181 ut_assertok(virtqueue_add(vq, sgs, 0, 1));
182 vq->vring.desc[0].addr = cpu_to_virtio64(dev, 0xbadbad11);
183 vq->vring.desc[0].len = cpu_to_virtio32(dev, 0x11badbad);
184 vq->vring.desc[0].flags = cpu_to_virtio16(dev, VRING_DESC_F_NEXT);
185 vq->vring.desc[0].next = cpu_to_virtio16(dev, U16_MAX);
186 vq->vring.used->idx = 1;
187 vq->vring.used->ring[0].id = 0;
188 vq->vring.used->ring[0].len = 6;
189 ut_asserteq_ptr(buffer, virtqueue_get_buf(vq, &len));
190 ut_asserteq(6, len);
191 ut_assertok(virtio_del_vqs(dev));
192
193 return 0;
194}
195DM_TEST(dm_test_virtio_ring, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);