1 // SPDX-License-Identifier: GPL-2.0+
3 * VFIO based AP device driver
5 * Copyright IBM Corp. 2018
7 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
8 * Pierre Morel <pmorel@linux.ibm.com>
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 #include "vfio_ap_debug.h"
19 #define VFIO_AP_ROOT_NAME "vfio_ap"
20 #define VFIO_AP_DEV_NAME "matrix"
21 #define AP_QUEUE_ASSIGNED "assigned"
22 #define AP_QUEUE_UNASSIGNED "unassigned"
23 #define AP_QUEUE_IN_USE "in use"
25 MODULE_AUTHOR("IBM Corporation");
26 MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
27 MODULE_LICENSE("GPL v2");
29 struct ap_matrix_dev *matrix_dev;
30 debug_info_t *vfio_ap_dbf_info;
32 /* Only type 10 adapters (CEX4 and later) are supported
33 * by the AP matrix device driver
35 static struct ap_device_id ap_queue_ids[] = {
36 { .dev_type = AP_DEVICE_TYPE_CEX4,
37 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
38 { .dev_type = AP_DEVICE_TYPE_CEX5,
39 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
40 { .dev_type = AP_DEVICE_TYPE_CEX6,
41 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
42 { .dev_type = AP_DEVICE_TYPE_CEX7,
43 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
44 { .dev_type = AP_DEVICE_TYPE_CEX8,
45 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
46 { /* end of sibling */ },
49 static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
51 struct ap_matrix_mdev *matrix_mdev;
52 unsigned long apid = AP_QID_CARD(q->apqn);
53 unsigned long apqi = AP_QID_QUEUE(q->apqn);
55 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
56 if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
57 test_bit_inv(apqi, matrix_mdev->matrix.aqm))
64 static ssize_t status_show(struct device *dev,
65 struct device_attribute *attr,
69 struct vfio_ap_queue *q;
70 struct ap_matrix_mdev *matrix_mdev;
71 struct ap_device *apdev = to_ap_dev(dev);
73 mutex_lock(&matrix_dev->lock);
74 q = dev_get_drvdata(&apdev->device);
75 matrix_mdev = vfio_ap_mdev_for_queue(q);
79 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
82 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
85 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
89 mutex_unlock(&matrix_dev->lock);
94 static DEVICE_ATTR_RO(status);
96 static struct attribute *vfio_queue_attrs[] = {
97 &dev_attr_status.attr,
101 static const struct attribute_group vfio_queue_attr_group = {
102 .attrs = vfio_queue_attrs,
106 * vfio_ap_queue_dev_probe: Allocate a vfio_ap_queue structure and associate it
107 * with the device as driver_data.
109 * @apdev: the AP device being probed
111 * Return: returns 0 if the probe succeeded; otherwise, returns an error if
112 * storage could not be allocated for a vfio_ap_queue object or the
113 * sysfs 'status' attribute could not be created for the queue device.
115 static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
118 struct vfio_ap_queue *q;
120 q = kzalloc(sizeof(*q), GFP_KERNEL);
124 mutex_lock(&matrix_dev->lock);
125 dev_set_drvdata(&apdev->device, q);
126 q->apqn = to_ap_queue(&apdev->device)->qid;
127 q->saved_isc = VFIO_AP_ISC_INVALID;
129 ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
131 dev_set_drvdata(&apdev->device, NULL);
135 mutex_unlock(&matrix_dev->lock);
141 * vfio_ap_queue_dev_remove: Free the associated vfio_ap_queue structure.
143 * @apdev: the AP device being removed
145 * Takes the matrix lock to avoid actions on this device while doing the remove.
147 static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
149 struct vfio_ap_queue *q;
151 mutex_lock(&matrix_dev->lock);
152 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
153 q = dev_get_drvdata(&apdev->device);
154 vfio_ap_mdev_reset_queue(q, 1);
155 dev_set_drvdata(&apdev->device, NULL);
157 mutex_unlock(&matrix_dev->lock);
160 static struct ap_driver vfio_ap_drv = {
161 .probe = vfio_ap_queue_dev_probe,
162 .remove = vfio_ap_queue_dev_remove,
166 static void vfio_ap_matrix_dev_release(struct device *dev)
168 struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
173 static int matrix_bus_match(struct device *dev, struct device_driver *drv)
178 static struct bus_type matrix_bus = {
180 .match = &matrix_bus_match,
183 static struct device_driver matrix_driver = {
186 .suppress_bind_attrs = true,
189 static int vfio_ap_matrix_dev_create(void)
192 struct device *root_device;
194 root_device = root_device_register(VFIO_AP_ROOT_NAME);
195 if (IS_ERR(root_device))
196 return PTR_ERR(root_device);
198 ret = bus_register(&matrix_bus);
200 goto bus_register_err;
202 matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
205 goto matrix_alloc_err;
208 /* Fill in config info via PQAP(QCI), if available */
209 if (test_facility(12)) {
210 ret = ap_qci(&matrix_dev->info);
212 goto matrix_alloc_err;
215 mutex_init(&matrix_dev->lock);
216 INIT_LIST_HEAD(&matrix_dev->mdev_list);
218 dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
219 matrix_dev->device.parent = root_device;
220 matrix_dev->device.bus = &matrix_bus;
221 matrix_dev->device.release = vfio_ap_matrix_dev_release;
222 matrix_dev->vfio_ap_drv = &vfio_ap_drv;
224 ret = device_register(&matrix_dev->device);
228 ret = driver_register(&matrix_driver);
235 device_unregister(&matrix_dev->device);
237 put_device(&matrix_dev->device);
239 bus_unregister(&matrix_bus);
241 root_device_unregister(root_device);
245 static void vfio_ap_matrix_dev_destroy(void)
247 struct device *root_device = matrix_dev->device.parent;
249 driver_unregister(&matrix_driver);
250 device_unregister(&matrix_dev->device);
251 bus_unregister(&matrix_bus);
252 root_device_unregister(root_device);
255 static int __init vfio_ap_dbf_info_init(void)
257 vfio_ap_dbf_info = debug_register("vfio_ap", 1, 1,
258 DBF_MAX_SPRINTF_ARGS * sizeof(long));
260 if (!vfio_ap_dbf_info)
263 debug_register_view(vfio_ap_dbf_info, &debug_sprintf_view);
264 debug_set_level(vfio_ap_dbf_info, DBF_WARN);
269 static int __init vfio_ap_init(void)
273 ret = vfio_ap_dbf_info_init();
277 /* If there are no AP instructions, there is nothing to pass through. */
278 if (!ap_instructions_available())
281 ret = vfio_ap_matrix_dev_create();
285 ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
287 vfio_ap_matrix_dev_destroy();
291 ret = vfio_ap_mdev_register();
293 ap_driver_unregister(&vfio_ap_drv);
294 vfio_ap_matrix_dev_destroy();
302 static void __exit vfio_ap_exit(void)
304 vfio_ap_mdev_unregister();
305 ap_driver_unregister(&vfio_ap_drv);
306 vfio_ap_matrix_dev_destroy();
307 debug_unregister(vfio_ap_dbf_info);
310 module_init(vfio_ap_init);
311 module_exit(vfio_ap_exit);