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"
18 #define VFIO_AP_ROOT_NAME "vfio_ap"
19 #define VFIO_AP_DEV_NAME "matrix"
21 MODULE_AUTHOR("IBM Corporation");
22 MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
23 MODULE_LICENSE("GPL v2");
25 static struct ap_driver vfio_ap_drv;
27 struct ap_matrix_dev *matrix_dev;
29 /* Only type 10 adapters (CEX4 and later) are supported
30 * by the AP matrix device driver
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 */ },
42 MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
45 * vfio_ap_queue_dev_probe:
47 * Allocate a vfio_ap_queue structure and associate it
48 * with the device as driver_data.
50 static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
52 struct vfio_ap_queue *q;
54 q = kzalloc(sizeof(*q), GFP_KERNEL);
57 dev_set_drvdata(&apdev->device, q);
58 q->apqn = to_ap_queue(&apdev->device)->qid;
59 q->saved_isc = VFIO_AP_ISC_INVALID;
64 * vfio_ap_queue_dev_remove:
66 * Takes the matrix lock to avoid actions on this device while removing
67 * Free the associated vfio_ap_queue structure
69 static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
71 struct vfio_ap_queue *q;
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);
82 mutex_unlock(&matrix_dev->lock);
85 static void vfio_ap_matrix_dev_release(struct device *dev)
87 struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
92 static int matrix_bus_match(struct device *dev, struct device_driver *drv)
97 static struct bus_type matrix_bus = {
99 .match = &matrix_bus_match,
102 static struct device_driver matrix_driver = {
105 .suppress_bind_attrs = true,
108 static int vfio_ap_matrix_dev_create(void)
111 struct device *root_device;
113 root_device = root_device_register(VFIO_AP_ROOT_NAME);
114 if (IS_ERR(root_device))
115 return PTR_ERR(root_device);
117 ret = bus_register(&matrix_bus);
119 goto bus_register_err;
121 matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
124 goto matrix_alloc_err;
127 /* Fill in config info via PQAP(QCI), if available */
128 if (test_facility(12)) {
129 ret = ap_qci(&matrix_dev->info);
131 goto matrix_alloc_err;
134 mutex_init(&matrix_dev->lock);
135 INIT_LIST_HEAD(&matrix_dev->mdev_list);
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;
143 ret = device_register(&matrix_dev->device);
147 ret = driver_register(&matrix_driver);
154 device_unregister(&matrix_dev->device);
156 put_device(&matrix_dev->device);
158 bus_unregister(&matrix_bus);
160 root_device_unregister(root_device);
164 static void vfio_ap_matrix_dev_destroy(void)
166 struct device *root_device = matrix_dev->device.parent;
168 driver_unregister(&matrix_driver);
169 device_unregister(&matrix_dev->device);
170 bus_unregister(&matrix_bus);
171 root_device_unregister(root_device);
174 static int __init vfio_ap_init(void)
178 /* If there are no AP instructions, there is nothing to pass through. */
179 if (!ap_instructions_available())
182 ret = vfio_ap_matrix_dev_create();
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;
191 ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
193 vfio_ap_matrix_dev_destroy();
197 ret = vfio_ap_mdev_register();
199 ap_driver_unregister(&vfio_ap_drv);
200 vfio_ap_matrix_dev_destroy();
208 static void __exit vfio_ap_exit(void)
210 vfio_ap_mdev_unregister();
211 ap_driver_unregister(&vfio_ap_drv);
212 vfio_ap_matrix_dev_destroy();
215 module_init(vfio_ap_init);
216 module_exit(vfio_ap_exit);