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