blob: 63dc53415b73d06d356acc616137214c16fdd841 [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
Andrew Scull561ff8902022-05-16 10:41:36 +00006#include <dm.h>
7#include <virtio_types.h>
8#include <virtio.h>
9#include <virtio_ring.h>
10#include <dm/device-internal.h>
11#include <dm/root.h>
12#include <dm/test.h>
13#include <dm/uclass-internal.h>
14#include <test/test.h>
15#include <test/ut.h>
16
17/* Basic test of the virtio uclass */
18static int dm_test_virtio_base(struct unit_test_state *uts)
19{
20 struct udevice *bus, *dev;
21 u8 status;
22
23 /* check probe success */
Michal Suchanekac12a2f2022-10-12 21:57:59 +020024 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
Andrew Scull561ff8902022-05-16 10:41:36 +000025 ut_assertnonnull(bus);
26
Andrew Sculle4ea0fd2022-05-16 10:41:37 +000027 /* check the child virtio-rng device is bound */
Andrew Scull561ff8902022-05-16 10:41:36 +000028 ut_assertok(device_find_first_child(bus, &dev));
29 ut_assertnonnull(dev);
Andrew Sculle4ea0fd2022-05-16 10:41:37 +000030 ut_asserteq_str("virtio-rng#0", dev->name);
Andrew Scull561ff8902022-05-16 10:41:36 +000031
32 /* check driver status */
33 ut_assertok(virtio_get_status(dev, &status));
34 ut_asserteq(VIRTIO_CONFIG_S_ACKNOWLEDGE, status);
35
Andrew Scullc3f73132022-05-16 10:41:38 +000036 /* probe the virtio-rng driver */
37 ut_assertok(device_probe(dev));
38
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);
44
Andrew Scull561ff8902022-05-16 10:41:36 +000045 return 0;
46}
47DM_TEST(dm_test_virtio_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
48
49/* Test all of the virtio uclass ops */
50static int dm_test_virtio_all_ops(struct unit_test_state *uts)
51{
52 struct udevice *bus, *dev;
53 struct virtio_dev_priv *uc_priv;
54 uint offset = 0, len = 0, nvqs = 1;
55 void *buffer = NULL;
56 u8 status;
57 u32 counter;
58 u64 features;
59 struct virtqueue *vqs[2];
60
61 /* check probe success */
Michal Suchanekac12a2f2022-10-12 21:57:59 +020062 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
Andrew Scull561ff8902022-05-16 10:41:36 +000063 ut_assertnonnull(bus);
64
65 /* check the child virtio-rng device is bound */
66 ut_assertok(device_find_first_child(bus, &dev));
67 ut_assertnonnull(dev);
68
69 /*
70 * fake the virtio device probe by filling in uc_priv->vdev
71 * which is used by virtio_find_vqs/virtio_del_vqs.
72 */
73 uc_priv = dev_get_uclass_priv(bus);
74 ut_assertnonnull(uc_priv);
75 uc_priv->vdev = dev;
76
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));
93
94 return 0;
95}
96DM_TEST(dm_test_virtio_all_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
97
98/* Test removal of virtio device driver */
99static int dm_test_virtio_remove(struct unit_test_state *uts)
100{
101 struct udevice *bus, *dev;
Simon Glassa2cc4b72023-01-17 10:47:50 -0700102 u8 status;
Andrew Scull561ff8902022-05-16 10:41:36 +0000103
104 /* check probe success */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200105 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
Andrew Scull561ff8902022-05-16 10:41:36 +0000106 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));
Simon Glassa2cc4b72023-01-17 10:47:50 -0700120 virtio_get_status(dev, &status);
121 ut_assertok(status);
Andrew Scull561ff8902022-05-16 10:41:36 +0000122
123 return 0;
124}
125DM_TEST(dm_test_virtio_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
126
127/* Test all of the virtio ring */
128static int dm_test_virtio_ring(struct unit_test_state *uts)
129{
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];
135 unsigned int len;
136 u8 buffer[2][32];
137
138 /* check probe success */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200139 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
Andrew Scull561ff8902022-05-16 10:41:36 +0000140 ut_assertnonnull(bus);
141
142 /* check the child virtio-blk device is bound */
143 ut_assertok(device_find_first_child(bus, &dev));
144 ut_assertnonnull(dev);
145
146 /*
147 * fake the virtio device probe by filling in uc_priv->vdev
148 * which is used by virtio_find_vqs/virtio_del_vqs.
149 */
150 uc_priv = dev_get_uclass_priv(bus);
151 ut_assertnonnull(uc_priv);
152 uc_priv->vdev = dev;
153
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]);
159 sgs[0] = &sg[0];
160 sgs[1] = &sg[1];
161
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));
171
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));
180
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));
192 ut_asserteq(6, len);
193 ut_assertok(virtio_del_vqs(dev));
194
195 return 0;
196}
197DM_TEST(dm_test_virtio_ring, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);