vfio_ap_drv.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. */
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. #include "vfio_ap_private.h"
  14. #define VFIO_AP_ROOT_NAME "vfio_ap"
  15. #define VFIO_AP_DEV_TYPE_NAME "ap_matrix"
  16. #define VFIO_AP_DEV_NAME "matrix"
  17. MODULE_AUTHOR("IBM Corporation");
  18. MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
  19. MODULE_LICENSE("GPL v2");
  20. static struct ap_driver vfio_ap_drv;
  21. static struct device_type vfio_ap_dev_type = {
  22. .name = VFIO_AP_DEV_TYPE_NAME,
  23. };
  24. struct ap_matrix_dev *matrix_dev;
  25. /* Only type 10 adapters (CEX4 and later) are supported
  26. * by the AP matrix device driver
  27. */
  28. static struct ap_device_id ap_queue_ids[] = {
  29. { .dev_type = AP_DEVICE_TYPE_CEX4,
  30. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  31. { .dev_type = AP_DEVICE_TYPE_CEX5,
  32. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  33. { .dev_type = AP_DEVICE_TYPE_CEX6,
  34. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  35. { /* end of sibling */ },
  36. };
  37. MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
  38. static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
  39. {
  40. return 0;
  41. }
  42. static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
  43. {
  44. /* Nothing to do yet */
  45. }
  46. static void vfio_ap_matrix_dev_release(struct device *dev)
  47. {
  48. struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
  49. kfree(matrix_dev);
  50. }
  51. static int vfio_ap_matrix_dev_create(void)
  52. {
  53. int ret;
  54. struct device *root_device;
  55. root_device = root_device_register(VFIO_AP_ROOT_NAME);
  56. if (IS_ERR(root_device))
  57. return PTR_ERR(root_device);
  58. matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
  59. if (!matrix_dev) {
  60. ret = -ENOMEM;
  61. goto matrix_alloc_err;
  62. }
  63. /* Fill in config info via PQAP(QCI), if available */
  64. if (test_facility(12)) {
  65. ret = ap_qci(&matrix_dev->info);
  66. if (ret)
  67. goto matrix_alloc_err;
  68. }
  69. mutex_init(&matrix_dev->lock);
  70. INIT_LIST_HEAD(&matrix_dev->mdev_list);
  71. matrix_dev->device.type = &vfio_ap_dev_type;
  72. dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
  73. matrix_dev->device.parent = root_device;
  74. matrix_dev->device.release = vfio_ap_matrix_dev_release;
  75. matrix_dev->device.driver = &vfio_ap_drv.driver;
  76. ret = device_register(&matrix_dev->device);
  77. if (ret)
  78. goto matrix_reg_err;
  79. return 0;
  80. matrix_reg_err:
  81. put_device(&matrix_dev->device);
  82. matrix_alloc_err:
  83. root_device_unregister(root_device);
  84. return ret;
  85. }
  86. static void vfio_ap_matrix_dev_destroy(void)
  87. {
  88. device_unregister(&matrix_dev->device);
  89. root_device_unregister(matrix_dev->device.parent);
  90. }
  91. int __init vfio_ap_init(void)
  92. {
  93. int ret;
  94. /* If there are no AP instructions, there is nothing to pass through. */
  95. if (!ap_instructions_available())
  96. return -ENODEV;
  97. ret = vfio_ap_matrix_dev_create();
  98. if (ret)
  99. return ret;
  100. memset(&vfio_ap_drv, 0, sizeof(vfio_ap_drv));
  101. vfio_ap_drv.probe = vfio_ap_queue_dev_probe;
  102. vfio_ap_drv.remove = vfio_ap_queue_dev_remove;
  103. vfio_ap_drv.ids = ap_queue_ids;
  104. ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
  105. if (ret) {
  106. vfio_ap_matrix_dev_destroy();
  107. return ret;
  108. }
  109. ret = vfio_ap_mdev_register();
  110. if (ret) {
  111. ap_driver_unregister(&vfio_ap_drv);
  112. vfio_ap_matrix_dev_destroy();
  113. return ret;
  114. }
  115. return 0;
  116. }
  117. void __exit vfio_ap_exit(void)
  118. {
  119. vfio_ap_mdev_unregister();
  120. ap_driver_unregister(&vfio_ap_drv);
  121. vfio_ap_matrix_dev_destroy();
  122. }
  123. module_init(vfio_ap_init);
  124. module_exit(vfio_ap_exit);