1 // SPDX-License-Identifier: GPL-2.0+
7 #include <virtio_types.h>
9 #include <virtio_ring.h>
10 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
14 #include <test/test.h>
17 /* Basic test of the virtio uclass */
18 static int dm_test_virtio_base(struct unit_test_state *uts)
20 struct udevice *bus, *dev;
23 /* check probe success */
24 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
25 ut_assertnonnull(bus);
27 /* check the child virtio-rng device is bound */
28 ut_assertok(device_find_first_child(bus, &dev));
29 ut_assertnonnull(dev);
30 ut_asserteq_str("virtio-rng#0", dev->name);
32 /* check driver status */
33 ut_assertok(virtio_get_status(dev, &status));
34 ut_asserteq(VIRTIO_CONFIG_S_ACKNOWLEDGE, status);
36 /* probe the virtio-rng driver */
37 ut_assertok(device_probe(dev));
39 /* check the device was reset and the driver picked up the device */
40 ut_assertok(virtio_get_status(dev, &status));
41 ut_asserteq(VIRTIO_CONFIG_S_DRIVER |
42 VIRTIO_CONFIG_S_DRIVER_OK |
43 VIRTIO_CONFIG_S_FEATURES_OK, status);
47 DM_TEST(dm_test_virtio_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
49 /* Test all of the virtio uclass ops */
50 static int dm_test_virtio_all_ops(struct unit_test_state *uts)
52 struct udevice *bus, *dev;
53 struct virtio_dev_priv *uc_priv;
54 uint offset = 0, len = 0, nvqs = 1;
59 struct virtqueue *vqs[2];
61 /* check probe success */
62 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
63 ut_assertnonnull(bus);
65 /* check the child virtio-rng device is bound */
66 ut_assertok(device_find_first_child(bus, &dev));
67 ut_assertnonnull(dev);
70 * fake the virtio device probe by filling in uc_priv->vdev
71 * which is used by virtio_find_vqs/virtio_del_vqs.
73 uc_priv = dev_get_uclass_priv(bus);
74 ut_assertnonnull(uc_priv);
77 /* test virtio_xxx APIs */
78 ut_assertok(virtio_get_config(dev, offset, buffer, len));
79 ut_assertok(virtio_set_config(dev, offset, buffer, len));
80 ut_asserteq(-ENOSYS, virtio_generation(dev, &counter));
81 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
82 ut_assertok(virtio_get_status(dev, &status));
83 ut_asserteq(VIRTIO_CONFIG_S_DRIVER_OK, status);
84 ut_assertok(virtio_reset(dev));
85 ut_assertok(virtio_get_status(dev, &status));
86 ut_asserteq(0, status);
87 ut_assertok(virtio_get_features(dev, &features));
88 ut_asserteq_64(BIT_ULL(VIRTIO_F_VERSION_1), features);
89 ut_assertok(virtio_set_features(dev));
90 ut_assertok(virtio_find_vqs(dev, nvqs, vqs));
91 ut_assertok(virtio_notify(dev, vqs[0]));
92 ut_assertok(virtio_del_vqs(dev));
96 DM_TEST(dm_test_virtio_all_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
98 /* Test removal of virtio device driver */
99 static int dm_test_virtio_remove(struct unit_test_state *uts)
101 struct udevice *bus, *dev;
104 /* check probe success */
105 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
106 ut_assertnonnull(bus);
108 /* check the child virtio-rng device is bound */
109 ut_assertok(device_find_first_child(bus, &dev));
110 ut_assertnonnull(dev);
112 /* set driver status to VIRTIO_CONFIG_S_DRIVER_OK */
113 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
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));
119 ut_asserteq(false, device_active(dev));
120 virtio_get_status(dev, &status);
125 DM_TEST(dm_test_virtio_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
127 /* Test all of the virtio ring */
128 static int dm_test_virtio_ring(struct unit_test_state *uts)
130 struct udevice *bus, *dev;
131 struct virtio_dev_priv *uc_priv;
132 struct virtqueue *vq;
133 struct virtio_sg sg[2];
134 struct virtio_sg *sgs[2];
138 /* check probe success */
139 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
140 ut_assertnonnull(bus);
142 /* check the child virtio-blk device is bound */
143 ut_assertok(device_find_first_child(bus, &dev));
144 ut_assertnonnull(dev);
147 * fake the virtio device probe by filling in uc_priv->vdev
148 * which is used by virtio_find_vqs/virtio_del_vqs.
150 uc_priv = dev_get_uclass_priv(bus);
151 ut_assertnonnull(uc_priv);
154 /* prepare the scatter-gather buffer */
155 sg[0].addr = buffer[0];
156 sg[0].length = sizeof(buffer[0]);
157 sg[1].addr = buffer[1];
158 sg[1].length = sizeof(buffer[1]);
162 /* read a buffer and report written size from device */
163 ut_assertok(virtio_find_vqs(dev, 1, &vq));
164 ut_assertok(virtqueue_add(vq, sgs, 0, 1));
165 vq->vring.used->idx = 1;
166 vq->vring.used->ring[0].id = 0;
167 vq->vring.used->ring[0].len = 0x53355885;
168 ut_asserteq_ptr(buffer, virtqueue_get_buf(vq, &len));
169 ut_asserteq(0x53355885, len);
170 ut_assertok(virtio_del_vqs(dev));
172 /* rejects used descriptors that aren't a chain head */
173 ut_assertok(virtio_find_vqs(dev, 1, &vq));
174 ut_assertok(virtqueue_add(vq, sgs, 0, 2));
175 vq->vring.used->idx = 1;
176 vq->vring.used->ring[0].id = 1;
177 vq->vring.used->ring[0].len = 0x53355885;
178 ut_assertnull(virtqueue_get_buf(vq, &len));
179 ut_assertok(virtio_del_vqs(dev));
181 /* device changes to descriptor are ignored */
182 ut_assertok(virtio_find_vqs(dev, 1, &vq));
183 ut_assertok(virtqueue_add(vq, sgs, 0, 1));
184 vq->vring.desc[0].addr = cpu_to_virtio64(dev, 0xbadbad11);
185 vq->vring.desc[0].len = cpu_to_virtio32(dev, 0x11badbad);
186 vq->vring.desc[0].flags = cpu_to_virtio16(dev, VRING_DESC_F_NEXT);
187 vq->vring.desc[0].next = cpu_to_virtio16(dev, U16_MAX);
188 vq->vring.used->idx = 1;
189 vq->vring.used->ring[0].id = 0;
190 vq->vring.used->ring[0].len = 6;
191 ut_asserteq_ptr(buffer, virtqueue_get_buf(vq, &len));
193 ut_assertok(virtio_del_vqs(dev));
197 DM_TEST(dm_test_virtio_ring, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);