blob: 4a0c0b23b88ecf6fae1a60469335c2edad945468 [file] [log] [blame]
Bin Meng1bb290d2018-10-15 02:21:26 -07001// 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/uclass-internal.h>
13#include <dm/root.h>
14#include <dm/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 */
24 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020025 ut_assertnonnull(bus);
Bin Meng1bb290d2018-10-15 02:21:26 -070026
27 /* check the child virtio-blk device is bound */
28 ut_assertok(device_find_first_child(bus, &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020029 ut_assertnonnull(dev);
Bin Meng1bb290d2018-10-15 02:21:26 -070030 ut_assertok(strcmp(dev->name, "virtio-blk#0"));
31
32 /* check driver status */
33 ut_assertok(virtio_get_status(dev, &status));
34 ut_asserteq(VIRTIO_CONFIG_S_ACKNOWLEDGE, status);
35
36 return 0;
37}
38DM_TEST(dm_test_virtio_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
39
40/* Test all of the virtio uclass ops */
41static int dm_test_virtio_all_ops(struct unit_test_state *uts)
42{
43 struct udevice *bus, *dev;
44 struct virtio_dev_priv *uc_priv;
45 uint offset = 0, len = 0, nvqs = 1;
46 void *buffer = NULL;
47 u8 status;
48 u32 counter;
49 u64 features;
50 struct virtqueue *vqs[2];
51
52 /* check probe success */
53 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020054 ut_assertnonnull(bus);
Bin Meng1bb290d2018-10-15 02:21:26 -070055
56 /* check the child virtio-blk device is bound */
57 ut_assertok(device_find_first_child(bus, &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020058 ut_assertnonnull(dev);
Bin Meng1bb290d2018-10-15 02:21:26 -070059
60 /*
61 * fake the virtio device probe by filling in uc_priv->vdev
62 * which is used by virtio_find_vqs/virtio_del_vqs.
63 */
64 uc_priv = dev_get_uclass_priv(bus);
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020065 ut_assertnonnull(uc_priv);
Bin Meng1bb290d2018-10-15 02:21:26 -070066 uc_priv->vdev = dev;
67
68 /* test virtio_xxx APIs */
69 ut_assertok(virtio_get_config(dev, offset, buffer, len));
70 ut_assertok(virtio_set_config(dev, offset, buffer, len));
71 ut_asserteq(-ENOSYS, virtio_generation(dev, &counter));
72 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
73 ut_assertok(virtio_get_status(dev, &status));
74 ut_asserteq(VIRTIO_CONFIG_S_DRIVER_OK, status);
75 ut_assertok(virtio_reset(dev));
76 ut_assertok(virtio_get_status(dev, &status));
77 ut_asserteq(0, status);
78 ut_assertok(virtio_get_features(dev, &features));
79 ut_asserteq(VIRTIO_F_VERSION_1, features);
80 ut_assertok(virtio_set_features(dev));
81 ut_assertok(virtio_find_vqs(dev, nvqs, vqs));
82 ut_assertok(virtio_del_vqs(dev));
83 ut_assertok(virtio_notify(dev, vqs[0]));
84
85 return 0;
86}
87DM_TEST(dm_test_virtio_all_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
88
89/* Test of the virtio driver that does not have required driver ops */
90static int dm_test_virtio_missing_ops(struct unit_test_state *uts)
91{
92 struct udevice *bus;
93
94 /* find the virtio device */
95 ut_assertok(uclass_find_device(UCLASS_VIRTIO, 1, &bus));
96
97 /*
98 * Probe the device should fail with error -ENOENT.
99 * See ops check in virtio_uclass_pre_probe().
100 */
101 ut_asserteq(-ENOENT, device_probe(bus));
102
103 return 0;
104}
105DM_TEST(dm_test_virtio_missing_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
106
107/* Test removal of virtio device driver */
108static int dm_test_virtio_remove(struct unit_test_state *uts)
109{
110 struct udevice *bus, *dev;
111
112 /* check probe success */
113 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200114 ut_assertnonnull(bus);
Bin Meng1bb290d2018-10-15 02:21:26 -0700115
116 /* check the child virtio-blk device is bound */
117 ut_assertok(device_find_first_child(bus, &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200118 ut_assertnonnull(dev);
Bin Meng1bb290d2018-10-15 02:21:26 -0700119
120 /* set driver status to VIRTIO_CONFIG_S_DRIVER_OK */
121 ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
122
123 /* check the device can be successfully removed */
124 dev->flags |= DM_FLAG_ACTIVATED;
125 ut_assertok(device_remove(bus, DM_REMOVE_ACTIVE_ALL));
126
127 return 0;
128}
129DM_TEST(dm_test_virtio_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);