xref: /linux/drivers/s390/crypto/vfio_ap_drv.c (revision 088e88be5a380cc4e81963a9a02815da465d144f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * VFIO based AP device driver
4  *
5  * Copyright IBM Corp. 2018
6  *
7  * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
8  *	      Pierre Morel <pmorel@linux.ibm.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <asm/facility.h>
16 #include "vfio_ap_private.h"
17 
18 #define VFIO_AP_ROOT_NAME "vfio_ap"
19 #define VFIO_AP_DEV_NAME "matrix"
20 
21 MODULE_AUTHOR("IBM Corporation");
22 MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
23 MODULE_LICENSE("GPL v2");
24 
25 static struct ap_driver vfio_ap_drv;
26 
27 struct ap_matrix_dev *matrix_dev;
28 
29 /* Only type 10 adapters (CEX4 and later) are supported
30  * by the AP matrix device driver
31  */
32 static struct ap_device_id ap_queue_ids[] = {
33 	{ .dev_type = AP_DEVICE_TYPE_CEX4,
34 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
35 	{ .dev_type = AP_DEVICE_TYPE_CEX5,
36 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
37 	{ .dev_type = AP_DEVICE_TYPE_CEX6,
38 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
39 	{ /* end of sibling */ },
40 };
41 
42 MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
43 
44 /**
45  * vfio_ap_queue_dev_probe:
46  *
47  * Allocate a vfio_ap_queue structure and associate it
48  * with the device as driver_data.
49  */
50 static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
51 {
52 	struct vfio_ap_queue *q;
53 
54 	q = kzalloc(sizeof(*q), GFP_KERNEL);
55 	if (!q)
56 		return -ENOMEM;
57 	dev_set_drvdata(&apdev->device, q);
58 	q->apqn = to_ap_queue(&apdev->device)->qid;
59 	q->saved_isc = VFIO_AP_ISC_INVALID;
60 	return 0;
61 }
62 
63 /**
64  * vfio_ap_queue_dev_remove:
65  *
66  * Takes the matrix lock to avoid actions on this device while removing
67  * Free the associated vfio_ap_queue structure
68  */
69 static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
70 {
71 	struct vfio_ap_queue *q;
72 	int apid, apqi;
73 
74 	mutex_lock(&matrix_dev->lock);
75 	q = dev_get_drvdata(&apdev->device);
76 	dev_set_drvdata(&apdev->device, NULL);
77 	apid = AP_QID_CARD(q->apqn);
78 	apqi = AP_QID_QUEUE(q->apqn);
79 	vfio_ap_mdev_reset_queue(apid, apqi, 1);
80 	vfio_ap_irq_disable(q);
81 	kfree(q);
82 	mutex_unlock(&matrix_dev->lock);
83 }
84 
85 static void vfio_ap_matrix_dev_release(struct device *dev)
86 {
87 	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
88 
89 	kfree(matrix_dev);
90 }
91 
92 static int matrix_bus_match(struct device *dev, struct device_driver *drv)
93 {
94 	return 1;
95 }
96 
97 static struct bus_type matrix_bus = {
98 	.name = "matrix",
99 	.match = &matrix_bus_match,
100 };
101 
102 static struct device_driver matrix_driver = {
103 	.name = "vfio_ap",
104 	.bus = &matrix_bus,
105 	.suppress_bind_attrs = true,
106 };
107 
108 static int vfio_ap_matrix_dev_create(void)
109 {
110 	int ret;
111 	struct device *root_device;
112 
113 	root_device = root_device_register(VFIO_AP_ROOT_NAME);
114 	if (IS_ERR(root_device))
115 		return PTR_ERR(root_device);
116 
117 	ret = bus_register(&matrix_bus);
118 	if (ret)
119 		goto bus_register_err;
120 
121 	matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
122 	if (!matrix_dev) {
123 		ret = -ENOMEM;
124 		goto matrix_alloc_err;
125 	}
126 
127 	/* Fill in config info via PQAP(QCI), if available */
128 	if (test_facility(12)) {
129 		ret = ap_qci(&matrix_dev->info);
130 		if (ret)
131 			goto matrix_alloc_err;
132 	}
133 
134 	mutex_init(&matrix_dev->lock);
135 	INIT_LIST_HEAD(&matrix_dev->mdev_list);
136 
137 	dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
138 	matrix_dev->device.parent = root_device;
139 	matrix_dev->device.bus = &matrix_bus;
140 	matrix_dev->device.release = vfio_ap_matrix_dev_release;
141 	matrix_dev->vfio_ap_drv = &vfio_ap_drv;
142 
143 	ret = device_register(&matrix_dev->device);
144 	if (ret)
145 		goto matrix_reg_err;
146 
147 	ret = driver_register(&matrix_driver);
148 	if (ret)
149 		goto matrix_drv_err;
150 
151 	return 0;
152 
153 matrix_drv_err:
154 	device_unregister(&matrix_dev->device);
155 matrix_reg_err:
156 	put_device(&matrix_dev->device);
157 matrix_alloc_err:
158 	bus_unregister(&matrix_bus);
159 bus_register_err:
160 	root_device_unregister(root_device);
161 	return ret;
162 }
163 
164 static void vfio_ap_matrix_dev_destroy(void)
165 {
166 	struct device *root_device = matrix_dev->device.parent;
167 
168 	driver_unregister(&matrix_driver);
169 	device_unregister(&matrix_dev->device);
170 	bus_unregister(&matrix_bus);
171 	root_device_unregister(root_device);
172 }
173 
174 static int __init vfio_ap_init(void)
175 {
176 	int ret;
177 
178 	/* If there are no AP instructions, there is nothing to pass through. */
179 	if (!ap_instructions_available())
180 		return -ENODEV;
181 
182 	ret = vfio_ap_matrix_dev_create();
183 	if (ret)
184 		return ret;
185 
186 	memset(&vfio_ap_drv, 0, sizeof(vfio_ap_drv));
187 	vfio_ap_drv.probe = vfio_ap_queue_dev_probe;
188 	vfio_ap_drv.remove = vfio_ap_queue_dev_remove;
189 	vfio_ap_drv.ids = ap_queue_ids;
190 
191 	ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
192 	if (ret) {
193 		vfio_ap_matrix_dev_destroy();
194 		return ret;
195 	}
196 
197 	ret = vfio_ap_mdev_register();
198 	if (ret) {
199 		ap_driver_unregister(&vfio_ap_drv);
200 		vfio_ap_matrix_dev_destroy();
201 
202 		return ret;
203 	}
204 
205 	return 0;
206 }
207 
208 static void __exit vfio_ap_exit(void)
209 {
210 	vfio_ap_mdev_unregister();
211 	ap_driver_unregister(&vfio_ap_drv);
212 	vfio_ap_matrix_dev_destroy();
213 }
214 
215 module_init(vfio_ap_init);
216 module_exit(vfio_ap_exit);
217