blob: 9a7e658ccebcefc83cbb2382d0ea65c1aa75c587 [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>
Bin Meng1bb290d2018-10-15 02:21:26 -070012#include <dm/root.h>
13#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060014#include <dm/uclass-internal.h>
15#include <test/test.h>
Bin Meng1bb290d2018-10-15 02:21:26 -070016#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));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020026 ut_assertnonnull(bus);
Bin Meng1bb290d2018-10-15 02:21:26 -070027
28 /* check the child virtio-blk device is bound */
29 ut_assertok(device_find_first_child(bus, &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020030 ut_assertnonnull(dev);
Bin Meng1bb290d2018-10-15 02:21:26 -070031 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}
Simon Glass974dccd2020-07-28 19:41:12 -060039DM_TEST(dm_test_virtio_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Bin Meng1bb290d2018-10-15 02:21:26 -070040
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));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020055 ut_assertnonnull(bus);
Bin Meng1bb290d2018-10-15 02:21:26 -070056
57 /* check the child virtio-blk device is bound */
58 ut_assertok(device_find_first_child(bus, &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020059 ut_assertnonnull(dev);
Bin Meng1bb290d2018-10-15 02:21:26 -070060
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);
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +020066 ut_assertnonnull(uc_priv);
Bin Meng1bb290d2018-10-15 02:21:26 -070067 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}
Simon Glass974dccd2020-07-28 19:41:12 -060088DM_TEST(dm_test_virtio_all_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Bin Meng1bb290d2018-10-15 02:21:26 -070089
90/* Test of the virtio driver that does not have required driver ops */
91static 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600106DM_TEST(dm_test_virtio_missing_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Bin Meng1bb290d2018-10-15 02:21:26 -0700107
108/* Test removal of virtio device driver */
109static 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));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200115 ut_assertnonnull(bus);
Bin Meng1bb290d2018-10-15 02:21:26 -0700116
117 /* check the child virtio-blk device is bound */
118 ut_assertok(device_find_first_child(bus, &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200119 ut_assertnonnull(dev);
Bin Meng1bb290d2018-10-15 02:21:26 -0700120
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 */
Simon Glass6211d762020-12-19 10:40:10 -0700125 dev_or_flags(dev, DM_FLAG_ACTIVATED);
Simon Glassdf34d0b2021-01-24 14:32:45 -0700126 ut_asserteq(-EKEYREJECTED, device_remove(bus, DM_REMOVE_ACTIVE_ALL));
127
128 ut_asserteq(false, device_active(dev));
Bin Meng1bb290d2018-10-15 02:21:26 -0700129
130 return 0;
131}
Simon Glass974dccd2020-07-28 19:41:12 -0600132DM_TEST(dm_test_virtio_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);