debugfs.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023, HiSilicon Ltd.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/debugfs.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/vfio.h>
  9. #include "vfio.h"
  10. static struct dentry *vfio_debugfs_root;
  11. static int vfio_device_state_read(struct seq_file *seq, void *data)
  12. {
  13. struct device *vf_dev = seq->private;
  14. struct vfio_device *vdev = container_of(vf_dev,
  15. struct vfio_device, device);
  16. enum vfio_device_mig_state state;
  17. int ret;
  18. BUILD_BUG_ON(VFIO_DEVICE_STATE_NR !=
  19. VFIO_DEVICE_STATE_PRE_COPY_P2P + 1);
  20. ret = vdev->mig_ops->migration_get_state(vdev, &state);
  21. if (ret)
  22. return -EINVAL;
  23. switch (state) {
  24. case VFIO_DEVICE_STATE_ERROR:
  25. seq_puts(seq, "ERROR\n");
  26. break;
  27. case VFIO_DEVICE_STATE_STOP:
  28. seq_puts(seq, "STOP\n");
  29. break;
  30. case VFIO_DEVICE_STATE_RUNNING:
  31. seq_puts(seq, "RUNNING\n");
  32. break;
  33. case VFIO_DEVICE_STATE_STOP_COPY:
  34. seq_puts(seq, "STOP_COPY\n");
  35. break;
  36. case VFIO_DEVICE_STATE_RESUMING:
  37. seq_puts(seq, "RESUMING\n");
  38. break;
  39. case VFIO_DEVICE_STATE_RUNNING_P2P:
  40. seq_puts(seq, "RUNNING_P2P\n");
  41. break;
  42. case VFIO_DEVICE_STATE_PRE_COPY:
  43. seq_puts(seq, "PRE_COPY\n");
  44. break;
  45. case VFIO_DEVICE_STATE_PRE_COPY_P2P:
  46. seq_puts(seq, "PRE_COPY_P2P\n");
  47. break;
  48. default:
  49. seq_puts(seq, "Invalid\n");
  50. }
  51. return 0;
  52. }
  53. void vfio_device_debugfs_init(struct vfio_device *vdev)
  54. {
  55. struct device *dev = &vdev->device;
  56. vdev->debug_root = debugfs_create_dir(dev_name(vdev->dev),
  57. vfio_debugfs_root);
  58. if (vdev->mig_ops) {
  59. struct dentry *vfio_dev_migration = NULL;
  60. vfio_dev_migration = debugfs_create_dir("migration",
  61. vdev->debug_root);
  62. debugfs_create_devm_seqfile(dev, "state", vfio_dev_migration,
  63. vfio_device_state_read);
  64. }
  65. }
  66. void vfio_device_debugfs_exit(struct vfio_device *vdev)
  67. {
  68. debugfs_remove_recursive(vdev->debug_root);
  69. }
  70. void vfio_debugfs_create_root(void)
  71. {
  72. vfio_debugfs_root = debugfs_create_dir("vfio", NULL);
  73. }
  74. void vfio_debugfs_remove_root(void)
  75. {
  76. debugfs_remove_recursive(vfio_debugfs_root);
  77. vfio_debugfs_root = NULL;
  78. }