11fde5734STony Krowiak // SPDX-License-Identifier: GPL-2.0+ 21fde5734STony Krowiak /* 31fde5734STony Krowiak * VFIO based AP device driver 41fde5734STony Krowiak * 51fde5734STony Krowiak * Copyright IBM Corp. 2018 61fde5734STony Krowiak * 71fde5734STony Krowiak * Author(s): Tony Krowiak <akrowiak@linux.ibm.com> 8ec89b55eSPierre Morel * Pierre Morel <pmorel@linux.ibm.com> 91fde5734STony Krowiak */ 101fde5734STony Krowiak 111fde5734STony Krowiak #include <linux/module.h> 121fde5734STony Krowiak #include <linux/mod_devicetable.h> 131fde5734STony Krowiak #include <linux/slab.h> 141fde5734STony Krowiak #include <linux/string.h> 15e45a6497SPetr Tesarik #include <asm/facility.h> 161fde5734STony Krowiak #include "vfio_ap_private.h" 17a084c44eSTony Krowiak #include "vfio_ap_debug.h" 181fde5734STony Krowiak 191fde5734STony Krowiak #define VFIO_AP_ROOT_NAME "vfio_ap" 201fde5734STony Krowiak #define VFIO_AP_DEV_NAME "matrix" 211fde5734STony Krowiak 221fde5734STony Krowiak MODULE_AUTHOR("IBM Corporation"); 231fde5734STony Krowiak MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018"); 241fde5734STony Krowiak MODULE_LICENSE("GPL v2"); 251fde5734STony Krowiak 261fde5734STony Krowiak struct ap_matrix_dev *matrix_dev; 27a084c44eSTony Krowiak debug_info_t *vfio_ap_dbf_info; 281fde5734STony Krowiak 291fde5734STony Krowiak /* Only type 10 adapters (CEX4 and later) are supported 301fde5734STony Krowiak * by the AP matrix device driver 311fde5734STony Krowiak */ 321fde5734STony Krowiak static struct ap_device_id ap_queue_ids[] = { 331fde5734STony Krowiak { .dev_type = AP_DEVICE_TYPE_CEX4, 341fde5734STony Krowiak .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, 351fde5734STony Krowiak { .dev_type = AP_DEVICE_TYPE_CEX5, 361fde5734STony Krowiak .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, 371fde5734STony Krowiak { .dev_type = AP_DEVICE_TYPE_CEX6, 381fde5734STony Krowiak .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, 39cf2957f3SHarald Freudenberger { .dev_type = AP_DEVICE_TYPE_CEX7, 40cf2957f3SHarald Freudenberger .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, 41985214afSHarald Freudenberger { .dev_type = AP_DEVICE_TYPE_CEX8, 42985214afSHarald Freudenberger .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, 431fde5734STony Krowiak { /* end of sibling */ }, 441fde5734STony Krowiak }; 451fde5734STony Krowiak 4631aae32cSJulian Wiedmann static struct ap_driver vfio_ap_drv = { 47260f3ea1STony Krowiak .probe = vfio_ap_mdev_probe_queue, 48260f3ea1STony Krowiak .remove = vfio_ap_mdev_remove_queue, 493f85d1dfSTony Krowiak .in_use = vfio_ap_mdev_resource_in_use, 50*eeb386aeSTony Krowiak .on_config_changed = vfio_ap_on_cfg_changed, 51*eeb386aeSTony Krowiak .on_scan_complete = vfio_ap_on_scan_complete, 5231aae32cSJulian Wiedmann .ids = ap_queue_ids, 5331aae32cSJulian Wiedmann }; 5431aae32cSJulian Wiedmann 551fde5734STony Krowiak static void vfio_ap_matrix_dev_release(struct device *dev) 561fde5734STony Krowiak { 571fde5734STony Krowiak struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev); 581fde5734STony Krowiak 591fde5734STony Krowiak kfree(matrix_dev); 601fde5734STony Krowiak } 611fde5734STony Krowiak 6236360658SPierre Morel static int matrix_bus_match(struct device *dev, struct device_driver *drv) 6336360658SPierre Morel { 6436360658SPierre Morel return 1; 6536360658SPierre Morel } 6636360658SPierre Morel 6736360658SPierre Morel static struct bus_type matrix_bus = { 6836360658SPierre Morel .name = "matrix", 6936360658SPierre Morel .match = &matrix_bus_match, 7036360658SPierre Morel }; 7136360658SPierre Morel 7236360658SPierre Morel static struct device_driver matrix_driver = { 7336360658SPierre Morel .name = "vfio_ap", 7436360658SPierre Morel .bus = &matrix_bus, 7536360658SPierre Morel .suppress_bind_attrs = true, 7636360658SPierre Morel }; 7736360658SPierre Morel 781fde5734STony Krowiak static int vfio_ap_matrix_dev_create(void) 791fde5734STony Krowiak { 801fde5734STony Krowiak int ret; 811fde5734STony Krowiak struct device *root_device; 821fde5734STony Krowiak 831fde5734STony Krowiak root_device = root_device_register(VFIO_AP_ROOT_NAME); 841fde5734STony Krowiak if (IS_ERR(root_device)) 851fde5734STony Krowiak return PTR_ERR(root_device); 861fde5734STony Krowiak 8736360658SPierre Morel ret = bus_register(&matrix_bus); 8836360658SPierre Morel if (ret) 8936360658SPierre Morel goto bus_register_err; 9036360658SPierre Morel 911fde5734STony Krowiak matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL); 921fde5734STony Krowiak if (!matrix_dev) { 931fde5734STony Krowiak ret = -ENOMEM; 941fde5734STony Krowiak goto matrix_alloc_err; 951fde5734STony Krowiak } 961fde5734STony Krowiak 9765f06713STony Krowiak /* Fill in config info via PQAP(QCI), if available */ 9865f06713STony Krowiak if (test_facility(12)) { 9965f06713STony Krowiak ret = ap_qci(&matrix_dev->info); 10065f06713STony Krowiak if (ret) 10165f06713STony Krowiak goto matrix_alloc_err; 10265f06713STony Krowiak } 10365f06713STony Krowiak 104d0786556STony Krowiak mutex_init(&matrix_dev->mdevs_lock); 10565f06713STony Krowiak INIT_LIST_HEAD(&matrix_dev->mdev_list); 10621195eb0STony Krowiak mutex_init(&matrix_dev->guests_lock); 10765f06713STony Krowiak 1081fde5734STony Krowiak dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME); 1091fde5734STony Krowiak matrix_dev->device.parent = root_device; 11036360658SPierre Morel matrix_dev->device.bus = &matrix_bus; 1111fde5734STony Krowiak matrix_dev->device.release = vfio_ap_matrix_dev_release; 11236360658SPierre Morel matrix_dev->vfio_ap_drv = &vfio_ap_drv; 1131fde5734STony Krowiak 1141fde5734STony Krowiak ret = device_register(&matrix_dev->device); 1151fde5734STony Krowiak if (ret) 1161fde5734STony Krowiak goto matrix_reg_err; 1171fde5734STony Krowiak 11836360658SPierre Morel ret = driver_register(&matrix_driver); 11936360658SPierre Morel if (ret) 12036360658SPierre Morel goto matrix_drv_err; 12136360658SPierre Morel 1221fde5734STony Krowiak return 0; 1231fde5734STony Krowiak 12436360658SPierre Morel matrix_drv_err: 12536360658SPierre Morel device_unregister(&matrix_dev->device); 1261fde5734STony Krowiak matrix_reg_err: 1271fde5734STony Krowiak put_device(&matrix_dev->device); 1281fde5734STony Krowiak matrix_alloc_err: 12936360658SPierre Morel bus_unregister(&matrix_bus); 13036360658SPierre Morel bus_register_err: 1311fde5734STony Krowiak root_device_unregister(root_device); 1321fde5734STony Krowiak return ret; 1331fde5734STony Krowiak } 1341fde5734STony Krowiak 1351fde5734STony Krowiak static void vfio_ap_matrix_dev_destroy(void) 1361fde5734STony Krowiak { 13736360658SPierre Morel struct device *root_device = matrix_dev->device.parent; 13836360658SPierre Morel 13936360658SPierre Morel driver_unregister(&matrix_driver); 1401fde5734STony Krowiak device_unregister(&matrix_dev->device); 14136360658SPierre Morel bus_unregister(&matrix_bus); 14236360658SPierre Morel root_device_unregister(root_device); 1431fde5734STony Krowiak } 1441fde5734STony Krowiak 145a084c44eSTony Krowiak static int __init vfio_ap_dbf_info_init(void) 146a084c44eSTony Krowiak { 147a084c44eSTony Krowiak vfio_ap_dbf_info = debug_register("vfio_ap", 1, 1, 148a084c44eSTony Krowiak DBF_MAX_SPRINTF_ARGS * sizeof(long)); 149a084c44eSTony Krowiak 150a084c44eSTony Krowiak if (!vfio_ap_dbf_info) 151a084c44eSTony Krowiak return -ENOENT; 152a084c44eSTony Krowiak 153a084c44eSTony Krowiak debug_register_view(vfio_ap_dbf_info, &debug_sprintf_view); 154a084c44eSTony Krowiak debug_set_level(vfio_ap_dbf_info, DBF_WARN); 155a084c44eSTony Krowiak 156a084c44eSTony Krowiak return 0; 157a084c44eSTony Krowiak } 158a084c44eSTony Krowiak 15946623ab3SChristian Borntraeger static int __init vfio_ap_init(void) 1601fde5734STony Krowiak { 1611fde5734STony Krowiak int ret; 1621fde5734STony Krowiak 163a084c44eSTony Krowiak ret = vfio_ap_dbf_info_init(); 164a084c44eSTony Krowiak if (ret) 165a084c44eSTony Krowiak return ret; 166a084c44eSTony Krowiak 1671fde5734STony Krowiak /* If there are no AP instructions, there is nothing to pass through. */ 1681fde5734STony Krowiak if (!ap_instructions_available()) 1691fde5734STony Krowiak return -ENODEV; 1701fde5734STony Krowiak 1711fde5734STony Krowiak ret = vfio_ap_matrix_dev_create(); 1721fde5734STony Krowiak if (ret) 1731fde5734STony Krowiak return ret; 1741fde5734STony Krowiak 1751fde5734STony Krowiak ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME); 1761fde5734STony Krowiak if (ret) { 1771fde5734STony Krowiak vfio_ap_matrix_dev_destroy(); 1781fde5734STony Krowiak return ret; 1791fde5734STony Krowiak } 1801fde5734STony Krowiak 18165f06713STony Krowiak ret = vfio_ap_mdev_register(); 18265f06713STony Krowiak if (ret) { 18365f06713STony Krowiak ap_driver_unregister(&vfio_ap_drv); 18465f06713STony Krowiak vfio_ap_matrix_dev_destroy(); 18565f06713STony Krowiak 18665f06713STony Krowiak return ret; 18765f06713STony Krowiak } 18865f06713STony Krowiak 1891fde5734STony Krowiak return 0; 1901fde5734STony Krowiak } 1911fde5734STony Krowiak 19246623ab3SChristian Borntraeger static void __exit vfio_ap_exit(void) 1931fde5734STony Krowiak { 19465f06713STony Krowiak vfio_ap_mdev_unregister(); 1951fde5734STony Krowiak ap_driver_unregister(&vfio_ap_drv); 1961fde5734STony Krowiak vfio_ap_matrix_dev_destroy(); 197a084c44eSTony Krowiak debug_unregister(vfio_ap_dbf_info); 1981fde5734STony Krowiak } 1991fde5734STony Krowiak 2001fde5734STony Krowiak module_init(vfio_ap_init); 2011fde5734STony Krowiak module_exit(vfio_ap_exit); 202