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, 4931aae32cSJulian Wiedmann .ids = ap_queue_ids, 5031aae32cSJulian Wiedmann }; 5131aae32cSJulian Wiedmann 521fde5734STony Krowiak static void vfio_ap_matrix_dev_release(struct device *dev) 531fde5734STony Krowiak { 541fde5734STony Krowiak struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev); 551fde5734STony Krowiak 561fde5734STony Krowiak kfree(matrix_dev); 571fde5734STony Krowiak } 581fde5734STony Krowiak 5936360658SPierre Morel static int matrix_bus_match(struct device *dev, struct device_driver *drv) 6036360658SPierre Morel { 6136360658SPierre Morel return 1; 6236360658SPierre Morel } 6336360658SPierre Morel 6436360658SPierre Morel static struct bus_type matrix_bus = { 6536360658SPierre Morel .name = "matrix", 6636360658SPierre Morel .match = &matrix_bus_match, 6736360658SPierre Morel }; 6836360658SPierre Morel 6936360658SPierre Morel static struct device_driver matrix_driver = { 7036360658SPierre Morel .name = "vfio_ap", 7136360658SPierre Morel .bus = &matrix_bus, 7236360658SPierre Morel .suppress_bind_attrs = true, 7336360658SPierre Morel }; 7436360658SPierre Morel 751fde5734STony Krowiak static int vfio_ap_matrix_dev_create(void) 761fde5734STony Krowiak { 771fde5734STony Krowiak int ret; 781fde5734STony Krowiak struct device *root_device; 791fde5734STony Krowiak 801fde5734STony Krowiak root_device = root_device_register(VFIO_AP_ROOT_NAME); 811fde5734STony Krowiak if (IS_ERR(root_device)) 821fde5734STony Krowiak return PTR_ERR(root_device); 831fde5734STony Krowiak 8436360658SPierre Morel ret = bus_register(&matrix_bus); 8536360658SPierre Morel if (ret) 8636360658SPierre Morel goto bus_register_err; 8736360658SPierre Morel 881fde5734STony Krowiak matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL); 891fde5734STony Krowiak if (!matrix_dev) { 901fde5734STony Krowiak ret = -ENOMEM; 911fde5734STony Krowiak goto matrix_alloc_err; 921fde5734STony Krowiak } 931fde5734STony Krowiak 9465f06713STony Krowiak /* Fill in config info via PQAP(QCI), if available */ 9565f06713STony Krowiak if (test_facility(12)) { 9665f06713STony Krowiak ret = ap_qci(&matrix_dev->info); 9765f06713STony Krowiak if (ret) 9865f06713STony Krowiak goto matrix_alloc_err; 9965f06713STony Krowiak } 10065f06713STony Krowiak 101*d0786556STony Krowiak mutex_init(&matrix_dev->mdevs_lock); 10265f06713STony Krowiak INIT_LIST_HEAD(&matrix_dev->mdev_list); 10365f06713STony Krowiak 1041fde5734STony Krowiak dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME); 1051fde5734STony Krowiak matrix_dev->device.parent = root_device; 10636360658SPierre Morel matrix_dev->device.bus = &matrix_bus; 1071fde5734STony Krowiak matrix_dev->device.release = vfio_ap_matrix_dev_release; 10836360658SPierre Morel matrix_dev->vfio_ap_drv = &vfio_ap_drv; 1091fde5734STony Krowiak 1101fde5734STony Krowiak ret = device_register(&matrix_dev->device); 1111fde5734STony Krowiak if (ret) 1121fde5734STony Krowiak goto matrix_reg_err; 1131fde5734STony Krowiak 11436360658SPierre Morel ret = driver_register(&matrix_driver); 11536360658SPierre Morel if (ret) 11636360658SPierre Morel goto matrix_drv_err; 11736360658SPierre Morel 1181fde5734STony Krowiak return 0; 1191fde5734STony Krowiak 12036360658SPierre Morel matrix_drv_err: 12136360658SPierre Morel device_unregister(&matrix_dev->device); 1221fde5734STony Krowiak matrix_reg_err: 1231fde5734STony Krowiak put_device(&matrix_dev->device); 1241fde5734STony Krowiak matrix_alloc_err: 12536360658SPierre Morel bus_unregister(&matrix_bus); 12636360658SPierre Morel bus_register_err: 1271fde5734STony Krowiak root_device_unregister(root_device); 1281fde5734STony Krowiak return ret; 1291fde5734STony Krowiak } 1301fde5734STony Krowiak 1311fde5734STony Krowiak static void vfio_ap_matrix_dev_destroy(void) 1321fde5734STony Krowiak { 13336360658SPierre Morel struct device *root_device = matrix_dev->device.parent; 13436360658SPierre Morel 13536360658SPierre Morel driver_unregister(&matrix_driver); 1361fde5734STony Krowiak device_unregister(&matrix_dev->device); 13736360658SPierre Morel bus_unregister(&matrix_bus); 13836360658SPierre Morel root_device_unregister(root_device); 1391fde5734STony Krowiak } 1401fde5734STony Krowiak 141a084c44eSTony Krowiak static int __init vfio_ap_dbf_info_init(void) 142a084c44eSTony Krowiak { 143a084c44eSTony Krowiak vfio_ap_dbf_info = debug_register("vfio_ap", 1, 1, 144a084c44eSTony Krowiak DBF_MAX_SPRINTF_ARGS * sizeof(long)); 145a084c44eSTony Krowiak 146a084c44eSTony Krowiak if (!vfio_ap_dbf_info) 147a084c44eSTony Krowiak return -ENOENT; 148a084c44eSTony Krowiak 149a084c44eSTony Krowiak debug_register_view(vfio_ap_dbf_info, &debug_sprintf_view); 150a084c44eSTony Krowiak debug_set_level(vfio_ap_dbf_info, DBF_WARN); 151a084c44eSTony Krowiak 152a084c44eSTony Krowiak return 0; 153a084c44eSTony Krowiak } 154a084c44eSTony Krowiak 15546623ab3SChristian Borntraeger static int __init vfio_ap_init(void) 1561fde5734STony Krowiak { 1571fde5734STony Krowiak int ret; 1581fde5734STony Krowiak 159a084c44eSTony Krowiak ret = vfio_ap_dbf_info_init(); 160a084c44eSTony Krowiak if (ret) 161a084c44eSTony Krowiak return ret; 162a084c44eSTony Krowiak 1631fde5734STony Krowiak /* If there are no AP instructions, there is nothing to pass through. */ 1641fde5734STony Krowiak if (!ap_instructions_available()) 1651fde5734STony Krowiak return -ENODEV; 1661fde5734STony Krowiak 1671fde5734STony Krowiak ret = vfio_ap_matrix_dev_create(); 1681fde5734STony Krowiak if (ret) 1691fde5734STony Krowiak return ret; 1701fde5734STony Krowiak 1711fde5734STony Krowiak ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME); 1721fde5734STony Krowiak if (ret) { 1731fde5734STony Krowiak vfio_ap_matrix_dev_destroy(); 1741fde5734STony Krowiak return ret; 1751fde5734STony Krowiak } 1761fde5734STony Krowiak 17765f06713STony Krowiak ret = vfio_ap_mdev_register(); 17865f06713STony Krowiak if (ret) { 17965f06713STony Krowiak ap_driver_unregister(&vfio_ap_drv); 18065f06713STony Krowiak vfio_ap_matrix_dev_destroy(); 18165f06713STony Krowiak 18265f06713STony Krowiak return ret; 18365f06713STony Krowiak } 18465f06713STony Krowiak 1851fde5734STony Krowiak return 0; 1861fde5734STony Krowiak } 1871fde5734STony Krowiak 18846623ab3SChristian Borntraeger static void __exit vfio_ap_exit(void) 1891fde5734STony Krowiak { 19065f06713STony Krowiak vfio_ap_mdev_unregister(); 1911fde5734STony Krowiak ap_driver_unregister(&vfio_ap_drv); 1921fde5734STony Krowiak vfio_ap_matrix_dev_destroy(); 193a084c44eSTony Krowiak debug_unregister(vfio_ap_dbf_info); 1941fde5734STony Krowiak } 1951fde5734STony Krowiak 1961fde5734STony Krowiak module_init(vfio_ap_init); 1971fde5734STony Krowiak module_exit(vfio_ap_exit); 198