virtio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <virtio_types.h>
  8. #include <virtio.h>
  9. #include <virtio_ring.h>
  10. #include <dm/device-internal.h>
  11. #include <dm/root.h>
  12. #include <dm/test.h>
  13. #include <dm/uclass-internal.h>
  14. #include <test/test.h>
  15. #include <test/ut.h>
  16. /* Basic test of the virtio uclass */
  17. static int dm_test_virtio_base(struct unit_test_state *uts)
  18. {
  19. struct udevice *bus, *dev;
  20. u8 status;
  21. /* check probe success */
  22. ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
  23. ut_assertnonnull(bus);
  24. /* check the child virtio-blk device is bound */
  25. ut_assertok(device_find_first_child(bus, &dev));
  26. ut_assertnonnull(dev);
  27. ut_assertok(strcmp(dev->name, "virtio-blk#0"));
  28. /* check driver status */
  29. ut_assertok(virtio_get_status(dev, &status));
  30. ut_asserteq(VIRTIO_CONFIG_S_ACKNOWLEDGE, status);
  31. return 0;
  32. }
  33. DM_TEST(dm_test_virtio_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  34. /* Test all of the virtio uclass ops */
  35. static int dm_test_virtio_all_ops(struct unit_test_state *uts)
  36. {
  37. struct udevice *bus, *dev;
  38. struct virtio_dev_priv *uc_priv;
  39. uint offset = 0, len = 0, nvqs = 1;
  40. void *buffer = NULL;
  41. u8 status;
  42. u32 counter;
  43. u64 features;
  44. struct virtqueue *vqs[2];
  45. /* check probe success */
  46. ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
  47. ut_assertnonnull(bus);
  48. /* check the child virtio-blk device is bound */
  49. ut_assertok(device_find_first_child(bus, &dev));
  50. ut_assertnonnull(dev);
  51. /*
  52. * fake the virtio device probe by filling in uc_priv->vdev
  53. * which is used by virtio_find_vqs/virtio_del_vqs.
  54. */
  55. uc_priv = dev_get_uclass_priv(bus);
  56. ut_assertnonnull(uc_priv);
  57. uc_priv->vdev = dev;
  58. /* test virtio_xxx APIs */
  59. ut_assertok(virtio_get_config(dev, offset, buffer, len));
  60. ut_assertok(virtio_set_config(dev, offset, buffer, len));
  61. ut_asserteq(-ENOSYS, virtio_generation(dev, &counter));
  62. ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
  63. ut_assertok(virtio_get_status(dev, &status));
  64. ut_asserteq(VIRTIO_CONFIG_S_DRIVER_OK, status);
  65. ut_assertok(virtio_reset(dev));
  66. ut_assertok(virtio_get_status(dev, &status));
  67. ut_asserteq(0, status);
  68. ut_assertok(virtio_get_features(dev, &features));
  69. ut_asserteq(VIRTIO_F_VERSION_1, features);
  70. ut_assertok(virtio_set_features(dev));
  71. ut_assertok(virtio_find_vqs(dev, nvqs, vqs));
  72. ut_assertok(virtio_del_vqs(dev));
  73. ut_assertok(virtio_notify(dev, vqs[0]));
  74. return 0;
  75. }
  76. DM_TEST(dm_test_virtio_all_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  77. /* Test of the virtio driver that does not have required driver ops */
  78. static int dm_test_virtio_missing_ops(struct unit_test_state *uts)
  79. {
  80. struct udevice *bus;
  81. /* find the virtio device */
  82. ut_assertok(uclass_find_device(UCLASS_VIRTIO, 1, &bus));
  83. /*
  84. * Probe the device should fail with error -ENOENT.
  85. * See ops check in virtio_uclass_pre_probe().
  86. */
  87. ut_asserteq(-ENOENT, device_probe(bus));
  88. return 0;
  89. }
  90. DM_TEST(dm_test_virtio_missing_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  91. /* Test removal of virtio device driver */
  92. static int dm_test_virtio_remove(struct unit_test_state *uts)
  93. {
  94. struct udevice *bus, *dev;
  95. /* check probe success */
  96. ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
  97. ut_assertnonnull(bus);
  98. /* check the child virtio-blk device is bound */
  99. ut_assertok(device_find_first_child(bus, &dev));
  100. ut_assertnonnull(dev);
  101. /* set driver status to VIRTIO_CONFIG_S_DRIVER_OK */
  102. ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));
  103. /* check the device can be successfully removed */
  104. dev_or_flags(dev, DM_FLAG_ACTIVATED);
  105. ut_asserteq(-EKEYREJECTED, device_remove(bus, DM_REMOVE_ACTIVE_ALL));
  106. ut_asserteq(false, device_active(dev));
  107. return 0;
  108. }
  109. DM_TEST(dm_test_virtio_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);