blob: 46f4798fc295f59a2815b2fdf425d032dcf3a651 [file] [log] [blame]
Andrew Scull561ff8902022-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
28 /* check the child virtio-blk device is bound */
29 ut_assertok(device_find_first_child(bus, &dev));
30 ut_assertnonnull(dev);
31 ut_assertok(strcmp(dev->name, "virtio-blk#0"));
32
33 /* check driver status */
34 ut_assertok(virtio_get_status(dev, &status));
35 ut_asserteq(VIRTIO_CONFIG_S_ACKNOWLEDGE, status);
36
37 return 0;
38}
39DM_TEST(dm_test_virtio_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
40
41/* Test all of the virtio uclass ops */
42static int dm_test_virtio_all_ops(struct unit_test_state *uts)
43{
44 struct udevice *bus, *dev;
45 struct virtio_dev_priv *uc_priv;
46 uint offset = 0, len = 0, nvqs = 1;
47 void *buffer = NULL;
48 u8 status;
49 u32 counter;
50 u64 features;
51 struct virtqueue *vqs[2];
52
53 /* check probe success */
54 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
55 ut_assertnonnull(bus);
56
57 /* check the child virtio-rng device is bound */
58 ut_assertok(device_find_first_child(bus, &dev));
59 ut_assertnonnull(dev);
60
61 /*
62 * fake the virtio device probe by filling in uc_priv->vdev
63 * which is used by virtio_find_vqs/virtio_del_vqs.
64 */
65 uc_priv = dev_get_uclass_priv(bus);
66 ut_assertnonnull(uc_priv);
67 uc_priv->vdev = dev;
68
69 /* test virtio_xxx APIs */
70 ut_assertok(virtio_get_config(dev, offset, buffer, len));
71 ut_assertok(virtio_set_config(dev, offset, buffer, len));
72 ut_asserteq(-ENOSYS, virtio_generation(dev, &counter));
73 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
74 ut_assertok(virtio_get_status(dev, &status));
75 ut_asserteq(VIRTIO_CONFIG_S_DRIVER_OK, status);
76 ut_assertok(virtio_reset(dev));
77 ut_assertok(virtio_get_status(dev, &status));
78 ut_asserteq(0, status);
79 ut_assertok(virtio_get_features(dev, &features));
80 ut_asserteq_64(BIT_ULL(VIRTIO_F_VERSION_1), features);
81 ut_assertok(virtio_set_features(dev));
82 ut_assertok(virtio_find_vqs(dev, nvqs, vqs));
83 ut_assertok(virtio_notify(dev, vqs[0]));
84 ut_assertok(virtio_del_vqs(dev));
85
86 return 0;
87}
88DM_TEST(dm_test_virtio_all_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
89
90/* Test removal of virtio device driver */
91static int dm_test_virtio_remove(struct unit_test_state *uts)
92{
93 struct udevice *bus, *dev;
94
95 /* check probe success */
96 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
97 ut_assertnonnull(bus);
98
99 /* check the child virtio-rng device is bound */
100 ut_assertok(device_find_first_child(bus, &dev));
101 ut_assertnonnull(dev);
102
103 /* set driver status to VIRTIO_CONFIG_S_DRIVER_OK */
104 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
105
106 /* check the device can be successfully removed */
107 dev_or_flags(dev, DM_FLAG_ACTIVATED);
108 ut_asserteq(-EKEYREJECTED, device_remove(bus, DM_REMOVE_ACTIVE_ALL));
109
110 ut_asserteq(false, device_active(dev));
111
112 return 0;
113}
114DM_TEST(dm_test_virtio_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
115
116/* Test all of the virtio ring */
117static int dm_test_virtio_ring(struct unit_test_state *uts)
118{
119 struct udevice *bus, *dev;
120 struct virtio_dev_priv *uc_priv;
121 struct virtqueue *vq;
122 struct virtio_sg sg[2];
123 struct virtio_sg *sgs[2];
124 unsigned int len;
125 u8 buffer[2][32];
126
127 /* check probe success */
128 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
129 ut_assertnonnull(bus);
130
131 /* check the child virtio-blk device is bound */
132 ut_assertok(device_find_first_child(bus, &dev));
133 ut_assertnonnull(dev);
134
135 /*
136 * fake the virtio device probe by filling in uc_priv->vdev
137 * which is used by virtio_find_vqs/virtio_del_vqs.
138 */
139 uc_priv = dev_get_uclass_priv(bus);
140 ut_assertnonnull(uc_priv);
141 uc_priv->vdev = dev;
142
143 /* prepare the scatter-gather buffer */
144 sg[0].addr = buffer[0];
145 sg[0].length = sizeof(buffer[0]);
146 sg[1].addr = buffer[1];
147 sg[1].length = sizeof(buffer[1]);
148 sgs[0] = &sg[0];
149 sgs[1] = &sg[1];
150
151 /* read a buffer and report written size from device */
152 ut_assertok(virtio_find_vqs(dev, 1, &vq));
153 ut_assertok(virtqueue_add(vq, sgs, 0, 1));
154 vq->vring.used->idx = 1;
155 vq->vring.used->ring[0].id = 0;
156 vq->vring.used->ring[0].len = 0x53355885;
157 ut_asserteq_ptr(buffer, virtqueue_get_buf(vq, &len));
158 ut_asserteq(0x53355885, len);
159 ut_assertok(virtio_del_vqs(dev));
160
161 /* rejects used descriptors that aren't a chain head */
162 ut_assertok(virtio_find_vqs(dev, 1, &vq));
163 ut_assertok(virtqueue_add(vq, sgs, 0, 2));
164 vq->vring.used->idx = 1;
165 vq->vring.used->ring[0].id = 1;
166 vq->vring.used->ring[0].len = 0x53355885;
167 ut_assertnull(virtqueue_get_buf(vq, &len));
168 ut_assertok(virtio_del_vqs(dev));
169
170 /* device changes to descriptor are ignored */
171 ut_assertok(virtio_find_vqs(dev, 1, &vq));
172 ut_assertok(virtqueue_add(vq, sgs, 0, 1));
173 vq->vring.desc[0].addr = cpu_to_virtio64(dev, 0xbadbad11);
174 vq->vring.desc[0].len = cpu_to_virtio32(dev, 0x11badbad);
175 vq->vring.desc[0].flags = cpu_to_virtio16(dev, VRING_DESC_F_NEXT);
176 vq->vring.desc[0].next = cpu_to_virtio16(dev, U16_MAX);
177 vq->vring.used->idx = 1;
178 vq->vring.used->ring[0].id = 0;
179 vq->vring.used->ring[0].len = 6;
180 ut_asserteq_ptr(buffer, virtqueue_get_buf(vq, &len));
181 ut_asserteq(6, len);
182 ut_assertok(virtio_del_vqs(dev));
183
184 return 0;
185}
186DM_TEST(dm_test_virtio_ring, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);