xref: /linux/drivers/s390/crypto/vfio_ap_drv.c (revision e08d227840bb9366c6321ae1e480b37ba5eec29b)
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 
features_show(struct device * dev,struct device_attribute * attr,char * buf)29*2d872136SJason J. Herne static ssize_t features_show(struct device *dev, struct device_attribute *attr, char *buf)
30*2d872136SJason J. Herne {
31*2d872136SJason J. Herne 	return sysfs_emit(buf, "guest_matrix hotplug ap_config\n");
32*2d872136SJason J. Herne }
33*2d872136SJason J. Herne static DEVICE_ATTR_RO(features);
34*2d872136SJason J. Herne 
35*2d872136SJason J. Herne static struct attribute *matrix_dev_attrs[] = {
36*2d872136SJason J. Herne 	&dev_attr_features.attr,
37*2d872136SJason J. Herne 	NULL,
38*2d872136SJason J. Herne };
39*2d872136SJason J. Herne ATTRIBUTE_GROUPS(matrix_dev);
40*2d872136SJason J. Herne 
411fde5734STony Krowiak /* Only type 10 adapters (CEX4 and later) are supported
421fde5734STony Krowiak  * by the AP matrix device driver
431fde5734STony Krowiak  */
441fde5734STony Krowiak static struct ap_device_id ap_queue_ids[] = {
451fde5734STony Krowiak 	{ .dev_type = AP_DEVICE_TYPE_CEX4,
461fde5734STony Krowiak 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
471fde5734STony Krowiak 	{ .dev_type = AP_DEVICE_TYPE_CEX5,
481fde5734STony Krowiak 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
491fde5734STony Krowiak 	{ .dev_type = AP_DEVICE_TYPE_CEX6,
501fde5734STony Krowiak 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
51cf2957f3SHarald Freudenberger 	{ .dev_type = AP_DEVICE_TYPE_CEX7,
52cf2957f3SHarald Freudenberger 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
53985214afSHarald Freudenberger 	{ .dev_type = AP_DEVICE_TYPE_CEX8,
54985214afSHarald Freudenberger 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
551fde5734STony Krowiak 	{ /* end of sibling */ },
561fde5734STony Krowiak };
571fde5734STony Krowiak 
5831aae32cSJulian Wiedmann static struct ap_driver vfio_ap_drv = {
59260f3ea1STony Krowiak 	.probe = vfio_ap_mdev_probe_queue,
60260f3ea1STony Krowiak 	.remove = vfio_ap_mdev_remove_queue,
613f85d1dfSTony Krowiak 	.in_use = vfio_ap_mdev_resource_in_use,
62eeb386aeSTony Krowiak 	.on_config_changed = vfio_ap_on_cfg_changed,
63eeb386aeSTony Krowiak 	.on_scan_complete = vfio_ap_on_scan_complete,
6431aae32cSJulian Wiedmann 	.ids = ap_queue_ids,
6531aae32cSJulian Wiedmann };
6631aae32cSJulian Wiedmann 
vfio_ap_matrix_dev_release(struct device * dev)671fde5734STony Krowiak static void vfio_ap_matrix_dev_release(struct device *dev)
681fde5734STony Krowiak {
698f8cf767STony Krowiak 	struct ap_matrix_dev *matrix_dev;
701fde5734STony Krowiak 
718f8cf767STony Krowiak 	matrix_dev = container_of(dev, struct ap_matrix_dev, device);
721fde5734STony Krowiak 	kfree(matrix_dev);
731fde5734STony Krowiak }
741fde5734STony Krowiak 
759e99049aSRicardo B. Marliere static const struct bus_type matrix_bus = {
7636360658SPierre Morel 	.name = "matrix",
7736360658SPierre Morel };
7836360658SPierre Morel 
7936360658SPierre Morel static struct device_driver matrix_driver = {
8036360658SPierre Morel 	.name = "vfio_ap",
8136360658SPierre Morel 	.bus = &matrix_bus,
8236360658SPierre Morel 	.suppress_bind_attrs = true,
83*2d872136SJason J. Herne 	.dev_groups = matrix_dev_groups,
8436360658SPierre Morel };
8536360658SPierre Morel 
vfio_ap_matrix_dev_create(void)861fde5734STony Krowiak static int vfio_ap_matrix_dev_create(void)
871fde5734STony Krowiak {
881fde5734STony Krowiak 	int ret;
891fde5734STony Krowiak 	struct device *root_device;
901fde5734STony Krowiak 
911fde5734STony Krowiak 	root_device = root_device_register(VFIO_AP_ROOT_NAME);
921fde5734STony Krowiak 	if (IS_ERR(root_device))
931fde5734STony Krowiak 		return PTR_ERR(root_device);
941fde5734STony Krowiak 
9536360658SPierre Morel 	ret = bus_register(&matrix_bus);
9636360658SPierre Morel 	if (ret)
9736360658SPierre Morel 		goto bus_register_err;
9836360658SPierre Morel 
991fde5734STony Krowiak 	matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
1001fde5734STony Krowiak 	if (!matrix_dev) {
1011fde5734STony Krowiak 		ret = -ENOMEM;
1021fde5734STony Krowiak 		goto matrix_alloc_err;
1031fde5734STony Krowiak 	}
1041fde5734STony Krowiak 
10565f06713STony Krowiak 	/* Fill in config info via PQAP(QCI), if available */
10665f06713STony Krowiak 	if (test_facility(12)) {
10765f06713STony Krowiak 		ret = ap_qci(&matrix_dev->info);
10865f06713STony Krowiak 		if (ret)
10965f06713STony Krowiak 			goto matrix_alloc_err;
11065f06713STony Krowiak 	}
11165f06713STony Krowiak 
112d0786556STony Krowiak 	mutex_init(&matrix_dev->mdevs_lock);
11365f06713STony Krowiak 	INIT_LIST_HEAD(&matrix_dev->mdev_list);
11421195eb0STony Krowiak 	mutex_init(&matrix_dev->guests_lock);
11565f06713STony Krowiak 
1161fde5734STony Krowiak 	dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
1171fde5734STony Krowiak 	matrix_dev->device.parent = root_device;
11836360658SPierre Morel 	matrix_dev->device.bus = &matrix_bus;
1191fde5734STony Krowiak 	matrix_dev->device.release = vfio_ap_matrix_dev_release;
12036360658SPierre Morel 	matrix_dev->vfio_ap_drv = &vfio_ap_drv;
1211fde5734STony Krowiak 
1221fde5734STony Krowiak 	ret = device_register(&matrix_dev->device);
1231fde5734STony Krowiak 	if (ret)
1241fde5734STony Krowiak 		goto matrix_reg_err;
1251fde5734STony Krowiak 
12636360658SPierre Morel 	ret = driver_register(&matrix_driver);
12736360658SPierre Morel 	if (ret)
12836360658SPierre Morel 		goto matrix_drv_err;
12936360658SPierre Morel 
1301fde5734STony Krowiak 	return 0;
1311fde5734STony Krowiak 
13236360658SPierre Morel matrix_drv_err:
133ce389573SAlex Williamson 	device_del(&matrix_dev->device);
1341fde5734STony Krowiak matrix_reg_err:
1351fde5734STony Krowiak 	put_device(&matrix_dev->device);
1361fde5734STony Krowiak matrix_alloc_err:
13736360658SPierre Morel 	bus_unregister(&matrix_bus);
13836360658SPierre Morel bus_register_err:
1391fde5734STony Krowiak 	root_device_unregister(root_device);
1401fde5734STony Krowiak 	return ret;
1411fde5734STony Krowiak }
1421fde5734STony Krowiak 
vfio_ap_matrix_dev_destroy(void)1431fde5734STony Krowiak static void vfio_ap_matrix_dev_destroy(void)
1441fde5734STony Krowiak {
14536360658SPierre Morel 	struct device *root_device = matrix_dev->device.parent;
14636360658SPierre Morel 
14736360658SPierre Morel 	driver_unregister(&matrix_driver);
1481fde5734STony Krowiak 	device_unregister(&matrix_dev->device);
14936360658SPierre Morel 	bus_unregister(&matrix_bus);
15036360658SPierre Morel 	root_device_unregister(root_device);
1511fde5734STony Krowiak }
1521fde5734STony Krowiak 
vfio_ap_dbf_info_init(void)153a084c44eSTony Krowiak static int __init vfio_ap_dbf_info_init(void)
154a084c44eSTony Krowiak {
155a084c44eSTony Krowiak 	vfio_ap_dbf_info = debug_register("vfio_ap", 1, 1,
156a084c44eSTony Krowiak 					  DBF_MAX_SPRINTF_ARGS * sizeof(long));
157a084c44eSTony Krowiak 
158a084c44eSTony Krowiak 	if (!vfio_ap_dbf_info)
159a084c44eSTony Krowiak 		return -ENOENT;
160a084c44eSTony Krowiak 
161a084c44eSTony Krowiak 	debug_register_view(vfio_ap_dbf_info, &debug_sprintf_view);
162a084c44eSTony Krowiak 	debug_set_level(vfio_ap_dbf_info, DBF_WARN);
163a084c44eSTony Krowiak 
164a084c44eSTony Krowiak 	return 0;
165a084c44eSTony Krowiak }
166a084c44eSTony Krowiak 
vfio_ap_init(void)16746623ab3SChristian Borntraeger static int __init vfio_ap_init(void)
1681fde5734STony Krowiak {
1691fde5734STony Krowiak 	int ret;
1701fde5734STony Krowiak 
171a084c44eSTony Krowiak 	ret = vfio_ap_dbf_info_init();
172a084c44eSTony Krowiak 	if (ret)
173a084c44eSTony Krowiak 		return ret;
174a084c44eSTony Krowiak 
1751fde5734STony Krowiak 	/* If there are no AP instructions, there is nothing to pass through. */
1761fde5734STony Krowiak 	if (!ap_instructions_available())
1771fde5734STony Krowiak 		return -ENODEV;
1781fde5734STony Krowiak 
1791fde5734STony Krowiak 	ret = vfio_ap_matrix_dev_create();
1801fde5734STony Krowiak 	if (ret)
1811fde5734STony Krowiak 		return ret;
1821fde5734STony Krowiak 
1831fde5734STony Krowiak 	ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
1841fde5734STony Krowiak 	if (ret) {
1851fde5734STony Krowiak 		vfio_ap_matrix_dev_destroy();
1861fde5734STony Krowiak 		return ret;
1871fde5734STony Krowiak 	}
1881fde5734STony Krowiak 
18965f06713STony Krowiak 	ret = vfio_ap_mdev_register();
19065f06713STony Krowiak 	if (ret) {
19165f06713STony Krowiak 		ap_driver_unregister(&vfio_ap_drv);
19265f06713STony Krowiak 		vfio_ap_matrix_dev_destroy();
19365f06713STony Krowiak 
19465f06713STony Krowiak 		return ret;
19565f06713STony Krowiak 	}
19665f06713STony Krowiak 
1971fde5734STony Krowiak 	return 0;
1981fde5734STony Krowiak }
1991fde5734STony Krowiak 
vfio_ap_exit(void)20046623ab3SChristian Borntraeger static void __exit vfio_ap_exit(void)
2011fde5734STony Krowiak {
20265f06713STony Krowiak 	vfio_ap_mdev_unregister();
2031fde5734STony Krowiak 	ap_driver_unregister(&vfio_ap_drv);
2041fde5734STony Krowiak 	vfio_ap_matrix_dev_destroy();
205a084c44eSTony Krowiak 	debug_unregister(vfio_ap_dbf_info);
2061fde5734STony Krowiak }
2071fde5734STony Krowiak 
2081fde5734STony Krowiak module_init(vfio_ap_init);
2091fde5734STony Krowiak module_exit(vfio_ap_exit);
210