Bin Meng | 1bb290d | 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> |
| 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 */ |
| 18 | static 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 Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 25 | ut_assertnonnull(bus); |
Bin Meng | 1bb290d | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 26 | |
| 27 | /* check the child virtio-blk device is bound */ |
| 28 | ut_assertok(device_find_first_child(bus, &dev)); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 29 | ut_assertnonnull(dev); |
Bin Meng | 1bb290d | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 30 | 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 | } |
| 38 | DM_TEST(dm_test_virtio_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 39 | |
| 40 | /* Test all of the virtio uclass ops */ |
| 41 | static 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 Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 54 | ut_assertnonnull(bus); |
Bin Meng | 1bb290d | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 55 | |
| 56 | /* check the child virtio-blk device is bound */ |
| 57 | ut_assertok(device_find_first_child(bus, &dev)); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 58 | ut_assertnonnull(dev); |
Bin Meng | 1bb290d | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 59 | |
| 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 Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 65 | ut_assertnonnull(uc_priv); |
Bin Meng | 1bb290d | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 66 | 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 | } |
| 87 | DM_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 */ |
| 90 | static 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 | } |
| 105 | DM_TEST(dm_test_virtio_missing_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 106 | |
| 107 | /* Test removal of virtio device driver */ |
| 108 | static 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 Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 114 | ut_assertnonnull(bus); |
Bin Meng | 1bb290d | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 115 | |
| 116 | /* check the child virtio-blk device is bound */ |
| 117 | ut_assertok(device_find_first_child(bus, &dev)); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 118 | ut_assertnonnull(dev); |
Bin Meng | 1bb290d | 2018-10-15 02:21:26 -0700 | [diff] [blame] | 119 | |
| 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 | } |
| 129 | DM_TEST(dm_test_virtio_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |