vfio_ap_drv.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <[email protected]>
  8. * Pierre Morel <[email protected]>
  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. /* Only type 10 adapters (CEX4 and later) are supported
  25. * by the AP matrix device driver
  26. */
  27. static struct ap_device_id ap_queue_ids[] = {
  28. { .dev_type = AP_DEVICE_TYPE_CEX4,
  29. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  30. { .dev_type = AP_DEVICE_TYPE_CEX5,
  31. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  32. { .dev_type = AP_DEVICE_TYPE_CEX6,
  33. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  34. { .dev_type = AP_DEVICE_TYPE_CEX7,
  35. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  36. { .dev_type = AP_DEVICE_TYPE_CEX8,
  37. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  38. { /* end of sibling */ },
  39. };
  40. static struct ap_driver vfio_ap_drv = {
  41. .probe = vfio_ap_mdev_probe_queue,
  42. .remove = vfio_ap_mdev_remove_queue,
  43. .in_use = vfio_ap_mdev_resource_in_use,
  44. .on_config_changed = vfio_ap_on_cfg_changed,
  45. .on_scan_complete = vfio_ap_on_scan_complete,
  46. .ids = ap_queue_ids,
  47. };
  48. static void vfio_ap_matrix_dev_release(struct device *dev)
  49. {
  50. struct ap_matrix_dev *matrix_dev;
  51. matrix_dev = container_of(dev, struct ap_matrix_dev, device);
  52. kfree(matrix_dev);
  53. }
  54. static int matrix_bus_match(struct device *dev, struct device_driver *drv)
  55. {
  56. return 1;
  57. }
  58. static struct bus_type matrix_bus = {
  59. .name = "matrix",
  60. .match = &matrix_bus_match,
  61. };
  62. static struct device_driver matrix_driver = {
  63. .name = "vfio_ap",
  64. .bus = &matrix_bus,
  65. .suppress_bind_attrs = true,
  66. };
  67. static int vfio_ap_matrix_dev_create(void)
  68. {
  69. int ret;
  70. struct device *root_device;
  71. root_device = root_device_register(VFIO_AP_ROOT_NAME);
  72. if (IS_ERR(root_device))
  73. return PTR_ERR(root_device);
  74. ret = bus_register(&matrix_bus);
  75. if (ret)
  76. goto bus_register_err;
  77. matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
  78. if (!matrix_dev) {
  79. ret = -ENOMEM;
  80. goto matrix_alloc_err;
  81. }
  82. /* Fill in config info via PQAP(QCI), if available */
  83. if (test_facility(12)) {
  84. ret = ap_qci(&matrix_dev->info);
  85. if (ret)
  86. goto matrix_alloc_err;
  87. }
  88. mutex_init(&matrix_dev->mdevs_lock);
  89. INIT_LIST_HEAD(&matrix_dev->mdev_list);
  90. mutex_init(&matrix_dev->guests_lock);
  91. dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
  92. matrix_dev->device.parent = root_device;
  93. matrix_dev->device.bus = &matrix_bus;
  94. matrix_dev->device.release = vfio_ap_matrix_dev_release;
  95. matrix_dev->vfio_ap_drv = &vfio_ap_drv;
  96. ret = device_register(&matrix_dev->device);
  97. if (ret)
  98. goto matrix_reg_err;
  99. ret = driver_register(&matrix_driver);
  100. if (ret)
  101. goto matrix_drv_err;
  102. return 0;
  103. matrix_drv_err:
  104. device_unregister(&matrix_dev->device);
  105. matrix_reg_err:
  106. put_device(&matrix_dev->device);
  107. matrix_alloc_err:
  108. bus_unregister(&matrix_bus);
  109. bus_register_err:
  110. root_device_unregister(root_device);
  111. return ret;
  112. }
  113. static void vfio_ap_matrix_dev_destroy(void)
  114. {
  115. struct device *root_device = matrix_dev->device.parent;
  116. driver_unregister(&matrix_driver);
  117. device_unregister(&matrix_dev->device);
  118. bus_unregister(&matrix_bus);
  119. root_device_unregister(root_device);
  120. }
  121. static int __init vfio_ap_dbf_info_init(void)
  122. {
  123. vfio_ap_dbf_info = debug_register("vfio_ap", 1, 1,
  124. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  125. if (!vfio_ap_dbf_info)
  126. return -ENOENT;
  127. debug_register_view(vfio_ap_dbf_info, &debug_sprintf_view);
  128. debug_set_level(vfio_ap_dbf_info, DBF_WARN);
  129. return 0;
  130. }
  131. static int __init vfio_ap_init(void)
  132. {
  133. int ret;
  134. ret = vfio_ap_dbf_info_init();
  135. if (ret)
  136. return ret;
  137. /* If there are no AP instructions, there is nothing to pass through. */
  138. if (!ap_instructions_available())
  139. return -ENODEV;
  140. ret = vfio_ap_matrix_dev_create();
  141. if (ret)
  142. return ret;
  143. ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
  144. if (ret) {
  145. vfio_ap_matrix_dev_destroy();
  146. return ret;
  147. }
  148. ret = vfio_ap_mdev_register();
  149. if (ret) {
  150. ap_driver_unregister(&vfio_ap_drv);
  151. vfio_ap_matrix_dev_destroy();
  152. return ret;
  153. }
  154. return 0;
  155. }
  156. static void __exit vfio_ap_exit(void)
  157. {
  158. vfio_ap_mdev_unregister();
  159. ap_driver_unregister(&vfio_ap_drv);
  160. vfio_ap_matrix_dev_destroy();
  161. debug_unregister(vfio_ap_dbf_info);
  162. }
  163. module_init(vfio_ap_init);
  164. module_exit(vfio_ap_exit);