xref: /linux/drivers/s390/crypto/vfio_ap_drv.c (revision e45a64975b021b6fc9fdd0dd0b74539ae1b5aa86)
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>
81fde5734STony Krowiak  */
91fde5734STony Krowiak 
101fde5734STony Krowiak #include <linux/module.h>
111fde5734STony Krowiak #include <linux/mod_devicetable.h>
121fde5734STony Krowiak #include <linux/slab.h>
131fde5734STony Krowiak #include <linux/string.h>
14*e45a6497SPetr Tesarik #include <asm/facility.h>
151fde5734STony Krowiak #include "vfio_ap_private.h"
161fde5734STony Krowiak 
171fde5734STony Krowiak #define VFIO_AP_ROOT_NAME "vfio_ap"
181fde5734STony Krowiak #define VFIO_AP_DEV_TYPE_NAME "ap_matrix"
191fde5734STony Krowiak #define VFIO_AP_DEV_NAME "matrix"
201fde5734STony Krowiak 
211fde5734STony Krowiak MODULE_AUTHOR("IBM Corporation");
221fde5734STony Krowiak MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
231fde5734STony Krowiak MODULE_LICENSE("GPL v2");
241fde5734STony Krowiak 
251fde5734STony Krowiak static struct ap_driver vfio_ap_drv;
261fde5734STony Krowiak 
271fde5734STony Krowiak static struct device_type vfio_ap_dev_type = {
281fde5734STony Krowiak 	.name = VFIO_AP_DEV_TYPE_NAME,
291fde5734STony Krowiak };
301fde5734STony Krowiak 
311fde5734STony Krowiak struct ap_matrix_dev *matrix_dev;
321fde5734STony Krowiak 
331fde5734STony Krowiak /* Only type 10 adapters (CEX4 and later) are supported
341fde5734STony Krowiak  * by the AP matrix device driver
351fde5734STony Krowiak  */
361fde5734STony Krowiak static struct ap_device_id ap_queue_ids[] = {
371fde5734STony Krowiak 	{ .dev_type = AP_DEVICE_TYPE_CEX4,
381fde5734STony Krowiak 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
391fde5734STony Krowiak 	{ .dev_type = AP_DEVICE_TYPE_CEX5,
401fde5734STony Krowiak 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
411fde5734STony Krowiak 	{ .dev_type = AP_DEVICE_TYPE_CEX6,
421fde5734STony Krowiak 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
431fde5734STony Krowiak 	{ /* end of sibling */ },
441fde5734STony Krowiak };
451fde5734STony Krowiak 
461fde5734STony Krowiak MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
471fde5734STony Krowiak 
481fde5734STony Krowiak static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
491fde5734STony Krowiak {
501fde5734STony Krowiak 	return 0;
511fde5734STony Krowiak }
521fde5734STony Krowiak 
531fde5734STony Krowiak static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
541fde5734STony Krowiak {
551fde5734STony Krowiak 	/* Nothing to do yet */
561fde5734STony Krowiak }
571fde5734STony Krowiak 
581fde5734STony Krowiak static void vfio_ap_matrix_dev_release(struct device *dev)
591fde5734STony Krowiak {
601fde5734STony Krowiak 	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
611fde5734STony Krowiak 
621fde5734STony Krowiak 	kfree(matrix_dev);
631fde5734STony Krowiak }
641fde5734STony Krowiak 
651fde5734STony Krowiak static int vfio_ap_matrix_dev_create(void)
661fde5734STony Krowiak {
671fde5734STony Krowiak 	int ret;
681fde5734STony Krowiak 	struct device *root_device;
691fde5734STony Krowiak 
701fde5734STony Krowiak 	root_device = root_device_register(VFIO_AP_ROOT_NAME);
711fde5734STony Krowiak 	if (IS_ERR(root_device))
721fde5734STony Krowiak 		return PTR_ERR(root_device);
731fde5734STony Krowiak 
741fde5734STony Krowiak 	matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
751fde5734STony Krowiak 	if (!matrix_dev) {
761fde5734STony Krowiak 		ret = -ENOMEM;
771fde5734STony Krowiak 		goto matrix_alloc_err;
781fde5734STony Krowiak 	}
791fde5734STony Krowiak 
8065f06713STony Krowiak 	/* Fill in config info via PQAP(QCI), if available */
8165f06713STony Krowiak 	if (test_facility(12)) {
8265f06713STony Krowiak 		ret = ap_qci(&matrix_dev->info);
8365f06713STony Krowiak 		if (ret)
8465f06713STony Krowiak 			goto matrix_alloc_err;
8565f06713STony Krowiak 	}
8665f06713STony Krowiak 
8765f06713STony Krowiak 	mutex_init(&matrix_dev->lock);
8865f06713STony Krowiak 	INIT_LIST_HEAD(&matrix_dev->mdev_list);
8965f06713STony Krowiak 
901fde5734STony Krowiak 	matrix_dev->device.type = &vfio_ap_dev_type;
911fde5734STony Krowiak 	dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
921fde5734STony Krowiak 	matrix_dev->device.parent = root_device;
931fde5734STony Krowiak 	matrix_dev->device.release = vfio_ap_matrix_dev_release;
941fde5734STony Krowiak 	matrix_dev->device.driver = &vfio_ap_drv.driver;
951fde5734STony Krowiak 
961fde5734STony Krowiak 	ret = device_register(&matrix_dev->device);
971fde5734STony Krowiak 	if (ret)
981fde5734STony Krowiak 		goto matrix_reg_err;
991fde5734STony Krowiak 
1001fde5734STony Krowiak 	return 0;
1011fde5734STony Krowiak 
1021fde5734STony Krowiak matrix_reg_err:
1031fde5734STony Krowiak 	put_device(&matrix_dev->device);
1041fde5734STony Krowiak matrix_alloc_err:
1051fde5734STony Krowiak 	root_device_unregister(root_device);
1061fde5734STony Krowiak 
1071fde5734STony Krowiak 	return ret;
1081fde5734STony Krowiak }
1091fde5734STony Krowiak 
1101fde5734STony Krowiak static void vfio_ap_matrix_dev_destroy(void)
1111fde5734STony Krowiak {
1121fde5734STony Krowiak 	device_unregister(&matrix_dev->device);
1131fde5734STony Krowiak 	root_device_unregister(matrix_dev->device.parent);
1141fde5734STony Krowiak }
1151fde5734STony Krowiak 
11646623ab3SChristian Borntraeger static int __init vfio_ap_init(void)
1171fde5734STony Krowiak {
1181fde5734STony Krowiak 	int ret;
1191fde5734STony Krowiak 
1201fde5734STony Krowiak 	/* If there are no AP instructions, there is nothing to pass through. */
1211fde5734STony Krowiak 	if (!ap_instructions_available())
1221fde5734STony Krowiak 		return -ENODEV;
1231fde5734STony Krowiak 
1241fde5734STony Krowiak 	ret = vfio_ap_matrix_dev_create();
1251fde5734STony Krowiak 	if (ret)
1261fde5734STony Krowiak 		return ret;
1271fde5734STony Krowiak 
1281fde5734STony Krowiak 	memset(&vfio_ap_drv, 0, sizeof(vfio_ap_drv));
1291fde5734STony Krowiak 	vfio_ap_drv.probe = vfio_ap_queue_dev_probe;
1301fde5734STony Krowiak 	vfio_ap_drv.remove = vfio_ap_queue_dev_remove;
1311fde5734STony Krowiak 	vfio_ap_drv.ids = ap_queue_ids;
1321fde5734STony Krowiak 
1331fde5734STony Krowiak 	ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
1341fde5734STony Krowiak 	if (ret) {
1351fde5734STony Krowiak 		vfio_ap_matrix_dev_destroy();
1361fde5734STony Krowiak 		return ret;
1371fde5734STony Krowiak 	}
1381fde5734STony Krowiak 
13965f06713STony Krowiak 	ret = vfio_ap_mdev_register();
14065f06713STony Krowiak 	if (ret) {
14165f06713STony Krowiak 		ap_driver_unregister(&vfio_ap_drv);
14265f06713STony Krowiak 		vfio_ap_matrix_dev_destroy();
14365f06713STony Krowiak 
14465f06713STony Krowiak 		return ret;
14565f06713STony Krowiak 	}
14665f06713STony Krowiak 
1471fde5734STony Krowiak 	return 0;
1481fde5734STony Krowiak }
1491fde5734STony Krowiak 
15046623ab3SChristian Borntraeger static void __exit vfio_ap_exit(void)
1511fde5734STony Krowiak {
15265f06713STony Krowiak 	vfio_ap_mdev_unregister();
1531fde5734STony Krowiak 	ap_driver_unregister(&vfio_ap_drv);
1541fde5734STony Krowiak 	vfio_ap_matrix_dev_destroy();
1551fde5734STony Krowiak }
1561fde5734STony Krowiak 
1571fde5734STony Krowiak module_init(vfio_ap_init);
1581fde5734STony Krowiak module_exit(vfio_ap_exit);
159