Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 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> |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 12 | #include <dm/root.h> |
| 13 | #include <dm/test.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 14 | #include <dm/uclass-internal.h> |
| 15 | #include <test/test.h> |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 16 | #include <test/ut.h> |
| 17 | |
| 18 | /* Basic test of the virtio uclass */ |
| 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)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 26 | ut_assertnonnull(bus); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 27 | |
| 28 | /* check the child virtio-blk device is bound */ |
| 29 | ut_assertok(device_find_first_child(bus, &dev)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 30 | ut_assertnonnull(dev); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 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 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 39 | DM_TEST(dm_test_virtio_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 40 | |
| 41 | /* Test all of the virtio uclass ops */ |
| 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)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 55 | ut_assertnonnull(bus); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 56 | |
| 57 | /* check the child virtio-blk device is bound */ |
| 58 | ut_assertok(device_find_first_child(bus, &dev)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 59 | ut_assertnonnull(dev); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 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); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 66 | ut_assertnonnull(uc_priv); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 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 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 88 | DM_TEST(dm_test_virtio_all_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 89 | |
| 90 | /* Test of the virtio driver that does not have required driver ops */ |
| 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 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 106 | DM_TEST(dm_test_virtio_missing_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 107 | |
| 108 | /* Test removal of virtio device driver */ |
| 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)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 115 | ut_assertnonnull(bus); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 116 | |
| 117 | /* check the child virtio-blk device is bound */ |
| 118 | ut_assertok(device_find_first_child(bus, &dev)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 119 | ut_assertnonnull(dev); |
Bin Meng | 4f89d49 | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 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->flags |= DM_FLAG_ACTIVATED; |
| 126 | ut_assertok(device_remove(bus, DM_REMOVE_ACTIVE_ALL)); |
| 127 | |
| 128 | return 0; |
| 129 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 130 | DM_TEST(dm_test_virtio_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |