blob: fdda4da417813cc1740ffe5a73d1d75dbc5f414b [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 */
Michal Suchanekc726fc02022-10-12 21:57:59 +020025 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
Andrew Scull8df508f2022-05-16 10:41:36 +000026 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 */
Michal Suchanekc726fc02022-10-12 21:57:59 +020063 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
Andrew Scull8df508f2022-05-16 10:41:36 +000064 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;
Simon Glass90240022023-01-17 10:47:50 -0700103 u8 status;
Andrew Scull8df508f2022-05-16 10:41:36 +0000104
105 /* check probe success */
Michal Suchanekc726fc02022-10-12 21:57:59 +0200106 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
Andrew Scull8df508f2022-05-16 10:41:36 +0000107 ut_assertnonnull(bus);
108
109 /* check the child virtio-rng device is bound */
110 ut_assertok(device_find_first_child(bus, &dev));
111 ut_assertnonnull(dev);
112
113 /* set driver status to VIRTIO_CONFIG_S_DRIVER_OK */
114 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
115
116 /* check the device can be successfully removed */
117 dev_or_flags(dev, DM_FLAG_ACTIVATED);
118 ut_asserteq(-EKEYREJECTED, device_remove(bus, DM_REMOVE_ACTIVE_ALL));
119
120 ut_asserteq(false, device_active(dev));
Simon Glass90240022023-01-17 10:47:50 -0700121 virtio_get_status(dev, &status);
122 ut_assertok(status);
Andrew Scull8df508f2022-05-16 10:41:36 +0000123
124 return 0;
125}
126DM_TEST(dm_test_virtio_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
127
128/* Test all of the virtio ring */
129static int dm_test_virtio_ring(struct unit_test_state *uts)
130{
131 struct udevice *bus, *dev;
132 struct virtio_dev_priv *uc_priv;
133 struct virtqueue *vq;
134 struct virtio_sg sg[2];
135 struct virtio_sg *sgs[2];
136 unsigned int len;
137 u8 buffer[2][32];
138
139 /* check probe success */
Michal Suchanekc726fc02022-10-12 21:57:59 +0200140 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
Andrew Scull8df508f2022-05-16 10:41:36 +0000141 ut_assertnonnull(bus);
142
143 /* check the child virtio-blk device is bound */
144 ut_assertok(device_find_first_child(bus, &dev));
145 ut_assertnonnull(dev);
146
147 /*
148 * fake the virtio device probe by filling in uc_priv->vdev
149 * which is used by virtio_find_vqs/virtio_del_vqs.
150 */
151 uc_priv = dev_get_uclass_priv(bus);
152 ut_assertnonnull(uc_priv);
153 uc_priv->vdev = dev;
154
155 /* prepare the scatter-gather buffer */
156 sg[0].addr = buffer[0];
157 sg[0].length = sizeof(buffer[0]);
158 sg[1].addr = buffer[1];
159 sg[1].length = sizeof(buffer[1]);
160 sgs[0] = &sg[0];
161 sgs[1] = &sg[1];
162
163 /* read a buffer and report written size from device */
164 ut_assertok(virtio_find_vqs(dev, 1, &vq));
165 ut_assertok(virtqueue_add(vq, sgs, 0, 1));
166 vq->vring.used->idx = 1;
167 vq->vring.used->ring[0].id = 0;
168 vq->vring.used->ring[0].len = 0x53355885;
169 ut_asserteq_ptr(buffer, virtqueue_get_buf(vq, &len));
170 ut_asserteq(0x53355885, len);
171 ut_assertok(virtio_del_vqs(dev));
172
173 /* rejects used descriptors that aren't a chain head */
174 ut_assertok(virtio_find_vqs(dev, 1, &vq));
175 ut_assertok(virtqueue_add(vq, sgs, 0, 2));
176 vq->vring.used->idx = 1;
177 vq->vring.used->ring[0].id = 1;
178 vq->vring.used->ring[0].len = 0x53355885;
179 ut_assertnull(virtqueue_get_buf(vq, &len));
180 ut_assertok(virtio_del_vqs(dev));
181
182 /* device changes to descriptor are ignored */
183 ut_assertok(virtio_find_vqs(dev, 1, &vq));
184 ut_assertok(virtqueue_add(vq, sgs, 0, 1));
185 vq->vring.desc[0].addr = cpu_to_virtio64(dev, 0xbadbad11);
186 vq->vring.desc[0].len = cpu_to_virtio32(dev, 0x11badbad);
187 vq->vring.desc[0].flags = cpu_to_virtio16(dev, VRING_DESC_F_NEXT);
188 vq->vring.desc[0].next = cpu_to_virtio16(dev, U16_MAX);
189 vq->vring.used->idx = 1;
190 vq->vring.used->ring[0].id = 0;
191 vq->vring.used->ring[0].len = 6;
192 ut_asserteq_ptr(buffer, virtqueue_get_buf(vq, &len));
193 ut_asserteq(6, len);
194 ut_assertok(virtio_del_vqs(dev));
195
196 return 0;
197}
198DM_TEST(dm_test_virtio_ring, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);