vfio_ap_drv.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * VFIO based AP device driver
  4. *
  5. * Copyright IBM Corp. 2018
  6. *
  7. * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
  8. * Pierre Morel <pmorel@linux.ibm.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <asm/facility.h>
  15. #include "vfio_ap_private.h"
  16. #include "vfio_ap_debug.h"
  17. #define VFIO_AP_ROOT_NAME "vfio_ap"
  18. #define VFIO_AP_DEV_NAME "matrix"
  19. MODULE_AUTHOR("IBM Corporation");
  20. MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
  21. MODULE_LICENSE("GPL v2");
  22. struct ap_matrix_dev *matrix_dev;
  23. debug_info_t *vfio_ap_dbf_info;
  24. static ssize_t features_show(struct device *dev, struct device_attribute *attr, char *buf)
  25. {
  26. return sysfs_emit(buf, "guest_matrix hotplug ap_config\n");
  27. }
  28. static DEVICE_ATTR_RO(features);
  29. static struct attribute *matrix_dev_attrs[] = {
  30. &dev_attr_features.attr,
  31. NULL,
  32. };
  33. ATTRIBUTE_GROUPS(matrix_dev);
  34. /* Only type 10 adapters (CEX4 and later) are supported
  35. * by the AP matrix device driver
  36. */
  37. static struct ap_device_id ap_queue_ids[] = {
  38. { .dev_type = AP_DEVICE_TYPE_CEX4,
  39. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  40. { .dev_type = AP_DEVICE_TYPE_CEX5,
  41. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  42. { .dev_type = AP_DEVICE_TYPE_CEX6,
  43. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  44. { .dev_type = AP_DEVICE_TYPE_CEX7,
  45. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  46. { .dev_type = AP_DEVICE_TYPE_CEX8,
  47. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  48. { /* end of sibling */ },
  49. };
  50. static struct ap_driver vfio_ap_drv = {
  51. .probe = vfio_ap_mdev_probe_queue,
  52. .remove = vfio_ap_mdev_remove_queue,
  53. .in_use = vfio_ap_mdev_resource_in_use,
  54. .on_config_changed = vfio_ap_on_cfg_changed,
  55. .on_scan_complete = vfio_ap_on_scan_complete,
  56. .ids = ap_queue_ids,
  57. };
  58. static void vfio_ap_matrix_dev_release(struct device *dev)
  59. {
  60. struct ap_matrix_dev *matrix_dev;
  61. matrix_dev = container_of(dev, struct ap_matrix_dev, device);
  62. kfree(matrix_dev);
  63. }
  64. static const struct bus_type matrix_bus = {
  65. .name = "matrix",
  66. };
  67. static struct device_driver matrix_driver = {
  68. .name = "vfio_ap",
  69. .bus = &matrix_bus,
  70. .suppress_bind_attrs = true,
  71. .dev_groups = matrix_dev_groups,
  72. };
  73. static int vfio_ap_matrix_dev_create(void)
  74. {
  75. int ret;
  76. struct device *root_device;
  77. root_device = root_device_register(VFIO_AP_ROOT_NAME);
  78. if (IS_ERR(root_device))
  79. return PTR_ERR(root_device);
  80. ret = bus_register(&matrix_bus);
  81. if (ret)
  82. goto bus_register_err;
  83. matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
  84. if (!matrix_dev) {
  85. ret = -ENOMEM;
  86. goto matrix_alloc_err;
  87. }
  88. /* Fill in config info via PQAP(QCI), if available */
  89. if (test_facility(12)) {
  90. ret = ap_qci(&matrix_dev->info);
  91. if (ret)
  92. goto matrix_alloc_err;
  93. }
  94. mutex_init(&matrix_dev->mdevs_lock);
  95. INIT_LIST_HEAD(&matrix_dev->mdev_list);
  96. mutex_init(&matrix_dev->guests_lock);
  97. dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
  98. matrix_dev->device.parent = root_device;
  99. matrix_dev->device.bus = &matrix_bus;
  100. matrix_dev->device.release = vfio_ap_matrix_dev_release;
  101. matrix_dev->vfio_ap_drv = &vfio_ap_drv;
  102. ret = device_register(&matrix_dev->device);
  103. if (ret)
  104. goto matrix_reg_err;
  105. ret = driver_register(&matrix_driver);
  106. if (ret)
  107. goto matrix_drv_err;
  108. return 0;
  109. matrix_drv_err:
  110. device_del(&matrix_dev->device);
  111. matrix_reg_err:
  112. put_device(&matrix_dev->device);
  113. matrix_alloc_err:
  114. bus_unregister(&matrix_bus);
  115. bus_register_err:
  116. root_device_unregister(root_device);
  117. return ret;
  118. }
  119. static void vfio_ap_matrix_dev_destroy(void)
  120. {
  121. struct device *root_device = matrix_dev->device.parent;
  122. driver_unregister(&matrix_driver);
  123. device_unregister(&matrix_dev->device);
  124. bus_unregister(&matrix_bus);
  125. root_device_unregister(root_device);
  126. }
  127. static int __init vfio_ap_dbf_info_init(void)
  128. {
  129. vfio_ap_dbf_info = debug_register("vfio_ap", 1, 1,
  130. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  131. if (!vfio_ap_dbf_info)
  132. return -ENOENT;
  133. debug_register_view(vfio_ap_dbf_info, &debug_sprintf_view);
  134. debug_set_level(vfio_ap_dbf_info, DBF_WARN);
  135. return 0;
  136. }
  137. static int __init vfio_ap_init(void)
  138. {
  139. int ret;
  140. ret = vfio_ap_dbf_info_init();
  141. if (ret)
  142. return ret;
  143. /* If there are no AP instructions, there is nothing to pass through. */
  144. if (!ap_instructions_available())
  145. return -ENODEV;
  146. ret = vfio_ap_matrix_dev_create();
  147. if (ret)
  148. return ret;
  149. ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
  150. if (ret) {
  151. vfio_ap_matrix_dev_destroy();
  152. return ret;
  153. }
  154. ret = vfio_ap_mdev_register();
  155. if (ret) {
  156. ap_driver_unregister(&vfio_ap_drv);
  157. vfio_ap_matrix_dev_destroy();
  158. return ret;
  159. }
  160. return 0;
  161. }
  162. static void __exit vfio_ap_exit(void)
  163. {
  164. vfio_ap_mdev_unregister();
  165. ap_driver_unregister(&vfio_ap_drv);
  166. vfio_ap_matrix_dev_destroy();
  167. debug_unregister(vfio_ap_dbf_info);
  168. }
  169. module_init(vfio_ap_init);
  170. module_exit(vfio_ap_exit);