1 // 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 */
dm_test_virtio_base(struct unit_test_state * uts)19 static 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 }
39 DM_TEST(dm_test_virtio_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
40
41 /* Test all of the virtio uclass ops */
dm_test_virtio_all_ops(struct unit_test_state * uts)42 static 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-blk 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(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_del_vqs(dev));
84 ut_assertok(virtio_notify(dev, vqs[0]));
85
86 return 0;
87 }
88 DM_TEST(dm_test_virtio_all_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
89
90 /* Test of the virtio driver that does not have required driver ops */
dm_test_virtio_missing_ops(struct unit_test_state * uts)91 static int dm_test_virtio_missing_ops(struct unit_test_state *uts)
92 {
93 struct udevice *bus;
94
95 /* find the virtio device */
96 ut_assertok(uclass_find_device(UCLASS_VIRTIO, 1, &bus));
97
98 /*
99 * Probe the device should fail with error -ENOENT.
100 * See ops check in virtio_uclass_pre_probe().
101 */
102 ut_asserteq(-ENOENT, device_probe(bus));
103
104 return 0;
105 }
106 DM_TEST(dm_test_virtio_missing_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
107
108 /* Test removal of virtio device driver */
dm_test_virtio_remove(struct unit_test_state * uts)109 static int dm_test_virtio_remove(struct unit_test_state *uts)
110 {
111 struct udevice *bus, *dev;
112
113 /* check probe success */
114 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
115 ut_assertnonnull(bus);
116
117 /* check the child virtio-blk device is bound */
118 ut_assertok(device_find_first_child(bus, &dev));
119 ut_assertnonnull(dev);
120
121 /* set driver status to VIRTIO_CONFIG_S_DRIVER_OK */
122 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
123
124 /* check the device can be successfully removed */
125 dev_or_flags(dev, DM_FLAG_ACTIVATED);
126 ut_asserteq(-EKEYREJECTED, device_remove(bus, DM_REMOVE_ACTIVE_ALL));
127
128 ut_asserteq(false, device_active(dev));
129
130 return 0;
131 }
132 DM_TEST(dm_test_virtio_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
133