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