a48c6938ae68f256744ceecce22852c8bb586e57
[linux-block.git] / drivers / s390 / crypto / vfio_ap_drv.c
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 #include "vfio_ap_debug.h"
18
19 #define VFIO_AP_ROOT_NAME "vfio_ap"
20 #define VFIO_AP_DEV_NAME "matrix"
21
22 MODULE_AUTHOR("IBM Corporation");
23 MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
24 MODULE_LICENSE("GPL v2");
25
26 struct ap_matrix_dev *matrix_dev;
27 debug_info_t *vfio_ap_dbf_info;
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         { .dev_type = AP_DEVICE_TYPE_CEX7,
40           .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
41         { .dev_type = AP_DEVICE_TYPE_CEX8,
42           .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
43         { /* end of sibling */ },
44 };
45
46 static struct ap_driver vfio_ap_drv = {
47         .probe = vfio_ap_mdev_probe_queue,
48         .remove = vfio_ap_mdev_remove_queue,
49         .in_use = vfio_ap_mdev_resource_in_use,
50         .on_config_changed = vfio_ap_on_cfg_changed,
51         .on_scan_complete = vfio_ap_on_scan_complete,
52         .ids = ap_queue_ids,
53 };
54
55 static void vfio_ap_matrix_dev_release(struct device *dev)
56 {
57         struct ap_matrix_dev *matrix_dev;
58
59         matrix_dev = container_of(dev, struct ap_matrix_dev, device);
60         kfree(matrix_dev);
61 }
62
63 static int matrix_bus_match(struct device *dev, struct device_driver *drv)
64 {
65         return 1;
66 }
67
68 static struct bus_type matrix_bus = {
69         .name = "matrix",
70         .match = &matrix_bus_match,
71 };
72
73 static struct device_driver matrix_driver = {
74         .name = "vfio_ap",
75         .bus = &matrix_bus,
76         .suppress_bind_attrs = true,
77 };
78
79 static int vfio_ap_matrix_dev_create(void)
80 {
81         int ret;
82         struct device *root_device;
83
84         root_device = root_device_register(VFIO_AP_ROOT_NAME);
85         if (IS_ERR(root_device))
86                 return PTR_ERR(root_device);
87
88         ret = bus_register(&matrix_bus);
89         if (ret)
90                 goto bus_register_err;
91
92         matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
93         if (!matrix_dev) {
94                 ret = -ENOMEM;
95                 goto matrix_alloc_err;
96         }
97
98         /* Fill in config info via PQAP(QCI), if available */
99         if (test_facility(12)) {
100                 ret = ap_qci(&matrix_dev->info);
101                 if (ret)
102                         goto matrix_alloc_err;
103         }
104
105         mutex_init(&matrix_dev->mdevs_lock);
106         INIT_LIST_HEAD(&matrix_dev->mdev_list);
107         mutex_init(&matrix_dev->guests_lock);
108
109         dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
110         matrix_dev->device.parent = root_device;
111         matrix_dev->device.bus = &matrix_bus;
112         matrix_dev->device.release = vfio_ap_matrix_dev_release;
113         matrix_dev->vfio_ap_drv = &vfio_ap_drv;
114
115         ret = device_register(&matrix_dev->device);
116         if (ret)
117                 goto matrix_reg_err;
118
119         ret = driver_register(&matrix_driver);
120         if (ret)
121                 goto matrix_drv_err;
122
123         return 0;
124
125 matrix_drv_err:
126         device_del(&matrix_dev->device);
127 matrix_reg_err:
128         put_device(&matrix_dev->device);
129 matrix_alloc_err:
130         bus_unregister(&matrix_bus);
131 bus_register_err:
132         root_device_unregister(root_device);
133         return ret;
134 }
135
136 static void vfio_ap_matrix_dev_destroy(void)
137 {
138         struct device *root_device = matrix_dev->device.parent;
139
140         driver_unregister(&matrix_driver);
141         device_unregister(&matrix_dev->device);
142         bus_unregister(&matrix_bus);
143         root_device_unregister(root_device);
144 }
145
146 static int __init vfio_ap_dbf_info_init(void)
147 {
148         vfio_ap_dbf_info = debug_register("vfio_ap", 1, 1,
149                                           DBF_MAX_SPRINTF_ARGS * sizeof(long));
150
151         if (!vfio_ap_dbf_info)
152                 return -ENOENT;
153
154         debug_register_view(vfio_ap_dbf_info, &debug_sprintf_view);
155         debug_set_level(vfio_ap_dbf_info, DBF_WARN);
156
157         return 0;
158 }
159
160 static int __init vfio_ap_init(void)
161 {
162         int ret;
163
164         ret = vfio_ap_dbf_info_init();
165         if (ret)
166                 return ret;
167
168         /* If there are no AP instructions, there is nothing to pass through. */
169         if (!ap_instructions_available())
170                 return -ENODEV;
171
172         ret = vfio_ap_matrix_dev_create();
173         if (ret)
174                 return ret;
175
176         ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
177         if (ret) {
178                 vfio_ap_matrix_dev_destroy();
179                 return ret;
180         }
181
182         ret = vfio_ap_mdev_register();
183         if (ret) {
184                 ap_driver_unregister(&vfio_ap_drv);
185                 vfio_ap_matrix_dev_destroy();
186
187                 return ret;
188         }
189
190         return 0;
191 }
192
193 static void __exit vfio_ap_exit(void)
194 {
195         vfio_ap_mdev_unregister();
196         ap_driver_unregister(&vfio_ap_drv);
197         vfio_ap_matrix_dev_destroy();
198         debug_unregister(vfio_ap_dbf_info);
199 }
200
201 module_init(vfio_ap_init);
202 module_exit(vfio_ap_exit);