vfio_pci_zdev.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VFIO ZPCI devices support
  4. *
  5. * Copyright (C) IBM Corp. 2020. All rights reserved.
  6. * Author(s): Pierre Morel <[email protected]>
  7. * Matthew Rosato <[email protected]>
  8. */
  9. #include <linux/io.h>
  10. #include <linux/pci.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/vfio.h>
  13. #include <linux/vfio_zdev.h>
  14. #include <linux/kvm_host.h>
  15. #include <asm/pci_clp.h>
  16. #include <asm/pci_io.h>
  17. #include "vfio_pci_priv.h"
  18. /*
  19. * Add the Base PCI Function information to the device info region.
  20. */
  21. static int zpci_base_cap(struct zpci_dev *zdev, struct vfio_info_cap *caps)
  22. {
  23. struct vfio_device_info_cap_zpci_base cap = {
  24. .header.id = VFIO_DEVICE_INFO_CAP_ZPCI_BASE,
  25. .header.version = 2,
  26. .start_dma = zdev->start_dma,
  27. .end_dma = zdev->end_dma,
  28. .pchid = zdev->pchid,
  29. .vfn = zdev->vfn,
  30. .fmb_length = zdev->fmb_length,
  31. .pft = zdev->pft,
  32. .gid = zdev->pfgid,
  33. .fh = zdev->fh
  34. };
  35. return vfio_info_add_capability(caps, &cap.header, sizeof(cap));
  36. }
  37. /*
  38. * Add the Base PCI Function Group information to the device info region.
  39. */
  40. static int zpci_group_cap(struct zpci_dev *zdev, struct vfio_info_cap *caps)
  41. {
  42. struct vfio_device_info_cap_zpci_group cap = {
  43. .header.id = VFIO_DEVICE_INFO_CAP_ZPCI_GROUP,
  44. .header.version = 2,
  45. .dasm = zdev->dma_mask,
  46. .msi_addr = zdev->msi_addr,
  47. .flags = VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH,
  48. .mui = zdev->fmb_update,
  49. .noi = zdev->max_msi,
  50. .maxstbl = ZPCI_MAX_WRITE_SIZE,
  51. .version = zdev->version,
  52. .reserved = 0,
  53. .imaxstbl = zdev->maxstbl
  54. };
  55. return vfio_info_add_capability(caps, &cap.header, sizeof(cap));
  56. }
  57. /*
  58. * Add the device utility string to the device info region.
  59. */
  60. static int zpci_util_cap(struct zpci_dev *zdev, struct vfio_info_cap *caps)
  61. {
  62. struct vfio_device_info_cap_zpci_util *cap;
  63. int cap_size = sizeof(*cap) + CLP_UTIL_STR_LEN;
  64. int ret;
  65. cap = kmalloc(cap_size, GFP_KERNEL);
  66. if (!cap)
  67. return -ENOMEM;
  68. cap->header.id = VFIO_DEVICE_INFO_CAP_ZPCI_UTIL;
  69. cap->header.version = 1;
  70. cap->size = CLP_UTIL_STR_LEN;
  71. memcpy(cap->util_str, zdev->util_str, cap->size);
  72. ret = vfio_info_add_capability(caps, &cap->header, cap_size);
  73. kfree(cap);
  74. return ret;
  75. }
  76. /*
  77. * Add the function path string to the device info region.
  78. */
  79. static int zpci_pfip_cap(struct zpci_dev *zdev, struct vfio_info_cap *caps)
  80. {
  81. struct vfio_device_info_cap_zpci_pfip *cap;
  82. int cap_size = sizeof(*cap) + CLP_PFIP_NR_SEGMENTS;
  83. int ret;
  84. cap = kmalloc(cap_size, GFP_KERNEL);
  85. if (!cap)
  86. return -ENOMEM;
  87. cap->header.id = VFIO_DEVICE_INFO_CAP_ZPCI_PFIP;
  88. cap->header.version = 1;
  89. cap->size = CLP_PFIP_NR_SEGMENTS;
  90. memcpy(cap->pfip, zdev->pfip, cap->size);
  91. ret = vfio_info_add_capability(caps, &cap->header, cap_size);
  92. kfree(cap);
  93. return ret;
  94. }
  95. /*
  96. * Add all supported capabilities to the VFIO_DEVICE_GET_INFO capability chain.
  97. */
  98. int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
  99. struct vfio_info_cap *caps)
  100. {
  101. struct zpci_dev *zdev = to_zpci(vdev->pdev);
  102. int ret;
  103. if (!zdev)
  104. return -ENODEV;
  105. ret = zpci_base_cap(zdev, caps);
  106. if (ret)
  107. return ret;
  108. ret = zpci_group_cap(zdev, caps);
  109. if (ret)
  110. return ret;
  111. if (zdev->util_str_avail) {
  112. ret = zpci_util_cap(zdev, caps);
  113. if (ret)
  114. return ret;
  115. }
  116. ret = zpci_pfip_cap(zdev, caps);
  117. return ret;
  118. }
  119. int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
  120. {
  121. struct zpci_dev *zdev = to_zpci(vdev->pdev);
  122. if (!zdev)
  123. return -ENODEV;
  124. if (!vdev->vdev.kvm)
  125. return 0;
  126. if (zpci_kvm_hook.kvm_register)
  127. return zpci_kvm_hook.kvm_register(zdev, vdev->vdev.kvm);
  128. return -ENOENT;
  129. }
  130. void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev)
  131. {
  132. struct zpci_dev *zdev = to_zpci(vdev->pdev);
  133. if (!zdev || !vdev->vdev.kvm)
  134. return;
  135. if (zpci_kvm_hook.kvm_unregister)
  136. zpci_kvm_hook.kvm_unregister(zdev);
  137. }